Spring中@Configuration注解使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guyue35/article/details/84397711

在Spring中使用@Configuration注解加载JavaConfig配置-zifangsky的博客备份-51CTO博客  
http://blog.51cto.com/983836259/1889610

如题所示,从一些翻译至国外的书籍中可以看到,一些西方人喜欢使用没有XML文件的纯粹的JavaConfig配置。但是一方面在国内我们通常都习惯使用XML文件来配置SpringMVC、Ehcache、Shiro等组件的具体参数配置,另一方面我个人认为适当使用基于XML文件的配置是可以有效减少配置文件的代码量的。因此,如果想要在项目中加载一部分的JavaConfig应该如何做呢?

(1)新建一个测试用例:

package cn.zifangsky.config;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.zifangsky.model.User;

@Configuration
public class UserConfig {
	
	@Bean(autowire=Autowire.BY_NAME,name="u")
	public User user(){
		return new User("root","123456");
	}

}

这里使用了@Configuration注解表明这个类是一个配置类,同时使用了@Bean定义了一个名为“u”的User类型的bean

(2)在Spring的配置文件中添加需要自动扫描这个包中的注解:

<context:component-scan base-package="cn.zifangsky.config" />

这里也就是“cn.zifangsky.config”这个包

(3)测试Controller:

package cn.zifangsky.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.zifangsky.model.User;

@Controller
public class TestController {
	@Resource(name="u")
	User user;
	
	@RequestMapping("/test.html")
	public void test(){
		System.out.println("测试name:" + user.getName());
	}
	
}

可以看到,这里使用的@Resource注解按名字“u”注入了之前定义好的bean

(4)测试:

启动项目后访问:http://localhost:8080/Demo/test.html

控制台输出如下:

测试name:root

 这么做有什么好处呢?

     1.使用纯java代码,不在需要xml

     2.在配置中也可享受OO带来的好处

     3.类型安全对重构也能提供良好的支持

     4.依旧能享受到所有springIoC容器提供的功能

=============================================================

=============================================================

=============================================================

使用@Configuration注解来代替Spring的bean配置

下面是一个典型的Spring配置文件(application-config.xml):

1

2

3

4

5

6

7

8

<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

  然后你就可以像这样来使用是bean了:

1

2

ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml"); 

OrderService orderService = (OrderService) ctx.getBean("orderService"); 

  现在Spring Java Configuration这个项目提供了一种通过java代码来装配bean的方案:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@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 … 

        

  然后你就可以像这样来使用是bean了:

1

2

JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class); 

OrderService orderService = ctx.getBean(OrderService.class);

  

 这么做有什么好处呢?

     1.使用纯java代码,不在需要xml

     2.在配置中也可享受OO带来的好处

     3.类型安全对重构也能提供良好的支持

     4.依旧能享受到所有springIoC容器提供的功能

 

猜你喜欢

转载自blog.csdn.net/guyue35/article/details/84397711