zoukankan      html  css  js  c++  java
  • spring注解1

    1. src下目录结构如下:

    2.测试类

    public class App {
    
    	// 创建容器对象
    	private ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/e_anno/bean.xml");
    
    	@Test
    	public void testExecuteAction() {
    		// 从容器中获取Action
    		UserAction userAction = (UserAction) ac.getBean("userAction");
    		userAction.execute();
    	}
    }
    

     3.控制层

    @Controller  // 加入IOC容器
    public class UserAction {
    	@Autowired
    	private UserService userService;
    
    	public String execute() {
    		userService.save();
    		return null;
    	}
    }
    

      4.业务逻辑层

    @Service // userService加入ioc容器
    public class UserService {
    	@Autowired
    	private UserDao userDao;
    
    	public void save() {
    		userDao.save();
    	}
    }
    

      5.持久层

    @Repository //  相当于bean.xml 【<bean id=userDao class=".." />】
    public class UserDao {
    	public void save() {
    		System.out.println("DB:保存用户!!!");
    	}
    }
    

      6. spring的配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    	
    	<!-- 开启注解扫描 -->
    	<context:component-scan base-package="cn.itcast.e_anno"></context:component-scan>
    </beans>  
    

      

  • 相关阅读:
    数据库设计
    Android入门
    Java
    深入理解计算机
    Python
    JS
    powerdesigner
    计算机程序员+研一生活总结
    影视剧里程序员使用的双显示屏,在生活中真的需要么?
    性质太恶劣,紧张时期竟有人开发假冒健康码软件,幸已下架!
  • 原文地址:https://www.cnblogs.com/bravolove/p/6820500.html
Copyright © 2011-2022 走看看