Spring 學習歷程(六)
阿新 • • 發佈:2018-12-17
Bean管理註解實現
- Classpath掃描與元件管理
- 類的自動檢測與註冊Bean
- 類的註解@Component、@Service等作用是將這個例項自動裝配到Bean容器中管理
- 而類似於@Autowired、@Required等註解則是將所代表的例項Bean1註冊到需要這個例項的另一個Bean2中,在Bean2初始化時使其屬性Bean1值不為null,他們並不能使Bean裝配到Bean容器中。使用這些註解時,其代表的例項是要已經裝配到Bean容器中的,否則會報錯。
- <context:component-scan >
- 自動掃描類的註解:將使用註解的類註冊到IOC容器中
<context:component-scan base-package
- 自動掃描類的註解:將使用註解的類註冊到IOC容器中
- @Component(value=" ")
- 是所有註解的父註解,用於不確定類是具體應該使用哪種型別註解時使用。可以代表任何一種型別註解。
- @Scope
- Bean的作用域
- @Scope(value="singleton")
- @Scope("value=prototype")
- @Repository
- 註解資料DAO層
- @Service
- 註解Service層
- @Controller
- @Required
- 適用於Bean的setter方法,表示Bean初始化時,Bean的屬性必須被填充。
- @Autowired(!!!)
- @Autowired註解只是將Bean注入到具體屬性或引數中,它不具備將Bean裝配到Bean容器中的功能。
- 適用於Bean的setter方法,表示Bean初始化時,Bean的屬性必須被填充。(功能同@Required)
private BeanTest beanTest; @Autowired
- 適用於構造器
class BeanAnnotation{ private BeanTest beanTest; @Autowired public BeanTest ( BeanTest beanTest){ this.beanTest =beanTest; } }
- 適用於屬性
class BeanAnnotation{ @Autowired private BeanTest beanTest; public BeanTest ( BeanTest beanTest){ this.beanTest =beanTest; } }
- 在使用@Autowired時,如果找不到合適的Bean將會報錯。我們可以使用以下方法避免這個問題,代表這個Bean不是必要的
class BeanAnnotation{ @Autowired(required=false) private BeanTest beanTest; public BeanTest ( BeanTest beanTest){ this.beanTest =beanTest; } }
- 當使用@Autowired(required=true)時,代表這個Bean是必須已經裝配到Bean容器中,每個類只可以在一個構造器上使用。
- 當一個類中,有多個setter需要標記為必須裝配時,可以使用@Required代替@Autowired(required=true)。
- 註解常用的介面,比如BeanFactory、Application、ResourceLoader、MessageSource,用於在類中直接獲取例項。
public class TestAutowired{ @Autowired private ApplicationContext application; public TestAutowired{ application.getBean(""); } }
- 註解給需要該型別的陣列,使其不為null,如List、Set
public class TestAutowired{ //AutowiredBeanI為介面,實現這個介面的類都將注入到list中 @Autowired private List<AutowiredBeanI> list; }
- 註解給需要該型別的Map,使其不為null
public class TestAutowired{ //AutowiredBeanI為介面,實現這個介面的類都將注入到map中 @Autowired private Map<String,AutowiredBeanI> map; }
- Next
- @Qualifier
- @Qualifier註解是將具體Bean注入到屬性或引數中,它不具備將Bean裝配到Bean容器中的功能。使用這個註解的前提是這個Bean已經裝配到Bean容器中,否則會報錯。
- @Qualifier可以指定具體名稱的Bean,將Bean注入到屬性或引數中,如下程式碼:TestQualifierI介面有多個實現類,如果不通過@Qualifier指定具體Bean,這幾個實現類的Bean將都會注入到List中;指定具體Bean名稱後,只會注入指定的Bean。
@Component(value = "qualifierEntry") public class QualifierEntry { @Autowired @Qualifier(value = "qualifierOne") private List<TestQualifierI> qualifierIS; public void print(){ for (TestQualifierI qualifier:qualifierIS) { qualifier.say(); } } }
- 可以通過@Autowired和@Qualifier(value=" ")的方式指定Bean名字將Bean注入。但是這種方式一般不建議使用,可以使用[email protected]註解替代,這個註解是通過使用特定的名稱來定義和識別特定的目標(這個匹配過程與Bean宣告的型別無關)。
- @Configuration、@Bean、@Scope
- @Configuration標記類可以作為配置文件來使用,和配置檔案產生關聯
- 在使用@Configuration標記的類中,使用@Qualifier(value=" ")時,@Qualifier不能夠注入其他類的例項,只可以引用本類中使用@Bean裝配的例項。
- @Bean用於配置和初始化一個用於IOC容器管理的新例項,它可以用於標識具有特殊資料內容的例項,將這個例項儲存在IOC中,方便讀取。
- @Bean標記的類預設為singleton,可以和@Scope搭配使用,標記改變類的作用域
- @Bean只能應用於標記類的方法,這個類的作用是產生新的例項,並由@Bean標記交由IOC容器管理,和@Configuration註解一起使用。
package com.jing.spring.annotation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class Configure { @Bean(value = {"configure2","configure1"},initMethod = "init",destroyMethod = "destroy") public BeanConfigureI beanCon(){ return new BeanConfigureImpl(); } }
- @ImportResource、@Value
- @ImportResource載入本地資原始檔:xml,使用@Value將properties中得變數賦值到Java屬性中,配置使用
<!-- spring-config.xml -->
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<context:property-placeholder location="classpath:/config.properties"></context:property-placeholder>
</beans>#config.properties
url:127.0.0.1
name:jingguoliangpackage com.jing.spring.annotation; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource(locations = {"classpath:spring-config.xml"}) public class Configure { @Value(value = "url") private String url; @Value(value = "name") private String name; @Bean(value = {"configResource"}) public ValueAndReImportResource getConfigResource(){ ValueAndReImportResource valueAndReImportResource = new ValueAndReImportResource(url,name); return valueAndReImportResource; } }
- Next
- @ImportResource載入本地資原始檔:xml,使用@Value將properties中得變數賦值到Java屬性中,配置使用