Spring整理系列(11)——@Configuration註解、@Bean註解以及配置自動掃描、bean作用域
1、@Configuration標註在類上,相當於把該類作為spring的xml配置檔案中的<beans>,作用為:配置spring容器(應用上下文)
package com.test.spring.support.configuration; @Configuration public class TestConfiguration { public TestConfiguration(){ System.out.println("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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false"> </beans>
主方法進行測試:
package com.test.spring.support.configuration; public class TestMain { public static void main(String[] args) { //@Configuration註解的spring容器載入方式,用AnnotationConfigApplicationContext替換ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); //如果載入spring-context.xml檔案: //ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml"); } }
從執行主方法結果可以看出,spring容器已經啟動了:
八月 11, 2016 12:04:11 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
資訊: Refreshing org.spring[email protected]203e25d3: startup date [Thu Aug 11 12:04:11 CST 2016]; root of context hierarchy
spring容器啟動初始化。。。
2、@Bean標註在方法上(返回某個例項的方法),等價於spring的xml配置檔案中的<bean>,作用為:註冊bean物件
bean類:
package com.test.spring.support.configuration;
public class TestBean {
public void sayHello(){
System.out.println("TestBean sayHello...");
}
public String toString(){
return "username:"+this.username+",url:"+this.url+",password:"+this.password;
}
public void start(){
System.out.println("TestBean 初始化。。。");
}
public void cleanUp(){
System.out.println("TestBean 銷燬。。。");
}
}
配置類:
package com.test.spring.support.configuration;
@Configuration
public class TestConfiguration {
public TestConfiguration(){
System.out.println("spring容器啟動初始化。。。");
}
//@Bean註解註冊bean,同時可以指定初始化和銷燬方法
//@Bean(name="testNean",initMethod="start",destroyMethod="cleanUp")
@Bean
@Scope("prototype")
public TestBean testBean() {
return new TestBean();
}
}
主方法測試類:
package com.test.spring.support.configuration;
public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
//獲取bean
TestBean tb = context.getBean("testBean");
tb.sayHello();
}
}
注:
(1)、@Bean註解在返回例項的方法上,如果未通過@Bean指定bean的名稱,則預設與標註的方法名相同;
(2)、@Bean註解預設作用域為單例singleton作用域,可通過@Scope(“prototype”)設定為原型作用域;
(3)、既然@Bean的作用是註冊bean物件,那麼完全可以使用@Component、@Controller、@Service、@Ripository等註解註冊bean,當然需要配置@ComponentScan註解進行自動掃描。
bean類:
package com.test.spring.support.configuration;
//添加註冊bean的註解
@Component
public class TestBean {
public void sayHello(){
System.out.println("TestBean sayHello...");
}
public String toString(){
return "username:"+this.username+",url:"+this.url+",password:"+this.password;
}
}
配置類:
@Configuration
//新增自動掃描註解,basePackages為TestBean包路徑
@ComponentScan(basePackages = "com.test.spring.support.configuration")
public class TestConfiguration {
public TestConfiguration(){
System.out.println("spring容器啟動初始化。。。");
}
//取消@Bean註解註冊bean的方式
//@Bean
//@Scope("prototype")
//public TestBean testBean() {
// return new TestBean();
//}
}
主方法測試獲取bean物件:
public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
//獲取bean
TestBean tb = context.getBean("testBean");
tb.sayHello();
}
}
sayHello()方法都被正常呼叫。
原文:https://blog.csdn.net/javaloveiphone/article/details/52182899