- @Configuration标注在类上,相当于把该类作为spring的xml配置文件中的,作用为:配置spring容器
package com.dalingjia.spring;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("spring容器启动初始化。。。");
}
}
相当于:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
</beans>
测试类:
package com.dalingjia.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestClient {
/**
* 1, 解析applicationgContext.xml,将xml中定义的bean(如loginService和loginResource)解析成Spring内部的BeanDefinition,
* 并以beanName(如loginService)为key,BeanDefinition(如loginService相应的BeanDefinition)为value存储到DefaultListableBeanFactory中的beanDefinitionMap(其实就是一个ConcurrentHashMap)中,
* 2, 同时将beanName存入beanDefinitionNames(List类型)中,然后遍历beanDefinitionNames中的beanName,进行bean的实例化并填充属性,
* 在实例化的过程中,如果有依赖没有被实例化将先实例化其依赖,然后实例化本身,实例化完成后将实例存入单例bean的缓存中,
* 当调用getBean方法时,到单例bean的缓存中查找,如果找到并经过转换后返回这个实例(如LoginResource的实例),之后就可以直接使用了
*/
@Test
public void test() {
//1,加载配置文件,用ClassPathXmlApplicationContext
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// System.out.println("***********************************************");
//2,加载@Configuration注解,AnnotationConfigApplicationContext
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
}
}
从控制台可以看出,spring容器已经启动:
八月 11, 2016 12:04:11 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@203e25d3: startup date [Thu Aug 11 12:04:11 CST 2016]; root of context hierarchy
spring容器启动初始化。。。
- @Bean标注在方法上(返回某个实例的方法),等价于spring的xml文件中的,作用为:注册Bean对象。
package com.dalingjia.spring;
public class TestBean {
public void sayHello(){
System.out.println("TestBean sayHello...");
}
public void start(){
System.out.println("TestBean 初始化。。。");
}
public void cleanUp(){
System.out.println("TestBean 销毁。。。");
}
}
配置类:
package com.dalingjia.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("spring容器启动初始化。。。");
}
//@Bean注解注册bean,同时可以指定初始化和销毁方法
//@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域;
@Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
// @Bean
@Scope("prototype")
public TestBean testBean() {
return new TestBean();
}
}
既然@Bean的作用是注册bean对象,那么完全可以使用@Component, @Controller, @Service, @Respository等注解注册bean,当然需要配置@ComponentScan注解进行自动扫描。
Bean类:
package com.dalingjia.spring;
import org.springframework.stereotype.Component;
@Component
public class TestBean {
public void sayHello(){
System.out.println("TestBean sayHello...");
}
public void start(){
System.out.println("TestBean 初始化。。。");
}
public void cleanUp(){
System.out.println("TestBean 销毁。。。");
}
}
配置类:
package com.dalingjia.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
@ComponentScan(basePackages = "com.dalingjia.spring")
public class TestConfiguration {
public TestConfiguration() {
System.out.println("spring容器启动初始化。。。");
}
}
测试类:
package com.dalingjia.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestClient {
@Test
public void test() {
//1,加载配置文件,用ClassPathXmlApplicationContext
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// System.out.println("***********************************************");
//2,加载@Configuration注解,AnnotationConfigApplicationContext
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
TestBean tb = (TestBean) context.getBean("testBean");
tb.sayHello();
}
}
sayHello()方法都被正常调用。