@Configuration 传统方式: Java代码 <beans> <bean id="orderService" class="com.acme.OrderService"/> <constructor-arg ref="orderRepository"/> </bean> <bean id="orderRepository" class="com.acme.OrderRepository"/> <constructor-arg ref="dataSource"/> </bean> </beans> <beans> <bean id="orderService" class="com.acme.OrderService"/> <constructor-arg ref="orderRepository"/> </bean> <bean id="orderRepository" class="com.acme.OrderRepository"/> <constructor-arg ref="dataSource"/> </bean> </beans> Java代码 ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml"); OrderService orderService = (OrderService) ctx.getBean("orderService"); ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml"); OrderService orderService = (OrderService) ctx.getBean("orderService"); 注解方式: Java代码 @Configuration public class ApplicationConfig { public @Bean OrderService orderService() { return new OrderService(orderRepository()); } public @Bean OrderRepository orderRepository() { return new OrderRepository(dataSource()); } public @Bean DataSource dataSource() { // instantiate and return an new DataSource … } } @Configuration public class ApplicationConfig { public @Bean OrderService orderService() { return new OrderService(orderRepository()); } public @Bean OrderRepository orderRepository() { return new OrderRepository(dataSource()); } public @Bean DataSource dataSource() { // instantiate and return an new DataSource … } } Java代码 JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class); OrderService orderService = ctx.getBean(OrderService.class); JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class); OrderService orderService = ctx.getBean(OrderService.class); @Scope("prototype") Java代码 @Scope("prototype") public class PrintTask2 implements Runnable { String name; public void setName(String name) { this.name = name; } @Override public void run(){ System.out.println(name + " is running."); try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(name + " is running again."); } } @Scope("prototype") public class PrintTask2 implements Runnable { String name; public void setName(String name) { this.name = name; } @Override public void run(){ System.out.println(name + " is running."); try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(name + " is running again."); } } 如果action或Service中不@Scope("prototype"),有可能报找不到xxxaction或Service的错误!写上这个就表示每次请求都重新创建一个action或Service,与SINGALON对应,俗称“多例”。 @Component Java代码 @Component @Scope("prototype") public class PrintTask2 implements Runnable { String name; public void setName(String name) { this.name = name; } @Override public void run(){ System.out.println(name + " is running."); try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(name + " is running again."); } } @Component @Scope("prototype") public class PrintTask2 implements Runnable { String name; public void setName(String name) { this.name = name; } @Override public void run(){ System.out.println(name + " is running."); try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(name + " is running again."); } } 把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> @Repository Java代码 @Repository public class DaoImpl implements IDao{ } @Repository public class DaoImpl implements IDao{ } 应用于Dao层,把该类注册到Spring容器中 @Service Java代码 @Service public class ServiceImpl implements IService { @Autowired private IDao iDao; } @Service public class ServiceImpl implements IService { @Autowired private IDao iDao; } 应用于Service层,把该类注册到Spring容器中 @Controller Java代码 @Controller public class TestController { @Autowired private IService iService; } @Controller public class TestController { @Autowired private IService iService; } 应用于Action层,把该类注册到Spring容器中 @Autowired , @Qualifier("XXX") Java代码 @Service public class TestServiceImpl { @Autowired @Qualifier("iTestDao2") private ITestDao iTestDao; } @Service public class TestServiceImpl { @Autowired @Qualifier("iTestDao2") private ITestDao iTestDao; } 在ITestDao接口标上@Autowired和@Qualifier注释使得ITestDao接口存在两个实现类的时候必须指定其中一个来注入,使用实现类首字母小写的字符串("iTestDao2")来注入。否则可以省略,只写@Autowired
获取【下载地址】 【新技术】现在最流行的java后台框架组合java springmvc mybaits mysql oracle html5 后台框架源码
原文:http://my.oschina.net/u/2499144/blog/522688