1.@Configuration注解
该注解是使用在class上的一个注解,等价于XML中配置beans,相当于Ioc容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean,与xml中配置的bean意思一样。
2.@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。被@Bean注解的方法返回值应为一个类,通过@Bean可以将方法返回的类注册到spring的Context中。
Bean的初始化方法和销毁方法,可以在 @Bean(initMethod = "init",destroyMethod = "destory")注解指定
1、写一个类Wanzi.class
package beans;
import interfaces.WanziInterfece;
public class Wanzi{
String wanzi = "wanzi";
public Wanzi(){
System.out.println("Wanzi.class 的构造方法");
}
public String getWanzi(){
return wanzi;
}
}
2、写一个WanziConfig.java
package springAnnotions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.annotation.Scope;
import beans.Wanzi;
/**
* @Configuration注解相当于Ioc容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean,与xml中配置的bean意思一样。
* @Configuration注解的类必需在spring.xml中使用<context:component-scanbase-package="XXX"/>扫描该文件所在的包
* @author qiaozhong
*/
@Configuration
public class WanziConfig {
@Bean(name="wanzi")
@Scope("prototype")
@Description("这是一个对@Bean注解的测试")
public Wanzi getWanzi(){
return new Wanzi();
}
}
3、配置spring-annotion.xml扫描WanziConfig.java所在的包
<?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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">
<!-- 支持@Value获取properties中的值 -->
<bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:application.properties</value>
</array>
</property>
</bean>
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="springAnnotions"></context:component-scan>
</beans>
4、写测试类WanziConfigTest.java
package springAnnotions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import beans.Wanzi;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/springConfig/spring-all.xml")
@TransactionConfiguration
public class WanziConfigTest {
@Autowired
private Wanzi wanzi;
@Test
public void testConfigurationAnnotion(){
System.out.println(wanzi.getWanzi());
}
}
测试结果:
Wanzi.class 的构造方法 wanzi
可以看到,在执行测试的时候,打印了Wanzi.java的构造函数,同时返回了wanzi.getWanzi()的值。
说明通过@configuration和@Bean已经将Wanzi.class注入到spring的上下文中。
@configuration和@Bean实现了spring的<bean>标签的作用