搜索Bean類:
Spring提供如下幾個(gè)Annotation來(lái)標(biāo)注Spring Bean:
- @Component:標(biāo)注一個(gè)普通的Spring Bean類站刑。
- @Controller:標(biāo)注一個(gè)控件器組件類际跪。
- @Service:標(biāo)注一個(gè)業(yè)務(wù)邏輯組件類屑彻。
- @Repository:標(biāo)注一個(gè)業(yè)務(wù)DAO組件類
Chinese.java
package entity;
import org.springframework.stereotype.Component;
import inter.Axe;
import inter.Persion;
@Component
public class Chinese implements Persion{
private Axe axe;
public void setAxe(Axe axe)
{
this.axe=axe;
}
@Override
public void useAxe() {
System.out.println(axe.chop());
}
}
SteelAxe.java
package entity;
import org.springframework.stereotype.Component;
import inter.Axe;
@Component
public class SteelAxe implements Axe{
@Override
public String chop() {
return "鋼斧砍柴真快撞蚕!";
}
}
beans.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:p="http://www.springframework.org/schema/p"
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">
<!-- 自動(dòng)掃描指定包及其子包下的所有Bean類 -->
<context:component-scan base-package="entity"/>
</beans>
BeanTest.java
package test;
import inter.Persion;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanTest {
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
System.out.println("------------"+java.util.Arrays.toString(ctx.getBeanDefinitionNames()));
}
}
輸出
------------[chinese, steelAxe,
org.springframework.context.annotation.internalConfigurationAnnotationProcessor,
org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor,
org.springframework.context.annotation.internalCommonAnnotationProcessor,
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]
在基于XML配置方式下润梯,每個(gè)Bean實(shí)例的名稱都是由其id屬性指定的;在這種基于Annotation的方式下甥厦,Spring采用約定的方式來(lái)為這些Bean實(shí)例指定名稱纺铭,這些Bean實(shí)例的名稱默認(rèn)是Bean類的首字母小寫(xiě),其他部分不變刀疙。
也可以在使用@Component標(biāo)注時(shí)指定Bean實(shí)例名稱:
@Component("axe")
public class SteelAxe implements Axe{
....................
}
默認(rèn)情況下舶赔,Spring會(huì)自動(dòng)搜索以@Component、@Controller谦秧、@Service竟纳、@Repository標(biāo)注的Java類撵溃,并將他們當(dāng)成Spring Bean來(lái)處理。還可以通過(guò)為<component-scan.../>元素添加<include-filter.../>或<exclude-filter.../>子元素來(lái)指定Spring Bean锥累。<include-filter.../>子元素指定滿足該規(guī)則的Java類會(huì)被當(dāng)成Bean類來(lái)處理缘挑,<exclude-filter.../>子元素指定滿足該規(guī)則的Java類不會(huì)被當(dāng)成Bean類來(lái)處理,使用這兩個(gè)元素需要指定如下兩個(gè)屬性:
- type:指定過(guò)濾器類型桶略。
- expression:指定過(guò)濾器所需要的表達(dá)式语淘。
Spring內(nèi)建支持如下4中過(guò)濾器:
- annotation。
- assignable际歼。
- regex惶翻。
- aspectj。
如下配置文件指定所有以Chinese結(jié)尾的類鹅心、以Axe結(jié)尾的類都將被當(dāng)成Spring Bean處理:
beans.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:p="http://www.springframework.org/schema/p"
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:component-scan base-package="entity">
<context:include-filter type="regex" expression=".*Chinese"/>
<context:include-filter type="regex" expression=".*Axe"/>
</context:component-scan>
</beans>
指定Bean的作用域:
使用@Scope來(lái)指定作用域
SteelAxe.java
//指定該Bean實(shí)例的作用域?yàn)閜ropertype
@Scope("prototype")
//指定該類作為Spring Bean吕粗,Bean實(shí)例名為axe
@Component("axe")
public class SteelAxe implements(){........}
另外的方法:
beans.xml
<beans>
...
<context:component-scan base-package="entity" scope-resolver="config.MyScopeResolver" />
...
</beans>
使用@Resource配置依賴:
使用@Resource與<property.../>元素的ref屬性有小童的效果。
Chinese.java
package entity;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import inter.Axe;
import inter.Persion;
@Component
public class Chinese implements Persion{
private Axe axe;
//axe的setter方法
@Resource(name="steelAxe")
public void setAxe(Axe axe)
{
this.axe=axe;
}
@Override
public void useAxe() {
System.out.println(axe.chop());
}
}
@Resource不僅可以修飾setter方法旭愧,也可以修飾實(shí)例變量颅筋,此時(shí)Spring將會(huì)直接使用Java EE規(guī)范的Field注入,此時(shí)了setter方法都可以不要榕茧。
Chinese.java
package entity;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import inter.Axe;
import inter.Persion;
@Component
public class Chinese implements Persion{
//執(zhí)行Field注入
@Resource(name="steelAxe")
private Axe axe;
@Override
public void useAxe() {
System.out.println(axe.chop());
}
}
使用@PostConstruct和@PreDestroy定制生命周期行為:
@PostConstruct和@PreDestroy都用于修飾方法垃沦,前者修飾的方法是Bean的初始化方法,而后者修飾的是Bean銷毀之前的方法用押。
Chinese.java
package entity;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import inter.Axe;
import inter.Persion;
@Component
public class Chinese implements Persion{
//執(zhí)行Field注入
@Resource(name="steelAxe")
private Axe axe;
@Override
public void useAxe() {
System.out.println(axe.chop());
}
//初始化方法
@PostConstruct
public void init(){
System.out.println("正在執(zhí)行初始化的init方法....");
}
//銷毀之前的方法
@PreDestroy
public void close()
{
System.out.println("正在執(zhí)行銷毀之前的close方法...");
}
}
Spring3.0新增的注解:
@DespendsOn和@Lazy肢簿,@DespendsOn用于強(qiáng)制初始化其他Bean,@Lazy用于指定該Bean是否取消預(yù)初始化蜻拨。
@DespendsOn可以修飾Bean類或方法池充,使用該注解時(shí)可以指定一個(gè)字符串?dāng)?shù)組做參數(shù),每個(gè)數(shù)組元素對(duì)應(yīng)一個(gè)強(qiáng)制初始化的Bean缎讼。
@DespendsOn({"steelAxe","abc"})
@Component
public class Chinese implements Persion{
.........
}
@Lazy修飾Spring Bean類用于指定該Bean的預(yù)初始化行為收夸,使用該注解可以指定一個(gè)boolean型的value屬相,用于決定是否要預(yù)初始化該Bean血崭。
//不會(huì)預(yù)初始化Chinese Bean
@Lazy(true)
@Component
public class Chinese implements Persion{
.........
}
Spring4.0增強(qiáng)的自動(dòng)裝配和精確裝配:
Spring提供了@Autowired注解來(lái)指定自動(dòng)裝配卧惜,@Autowired可以修飾setter方法、普通方法夹纫、實(shí)例變量和構(gòu)造器等咽瓷。當(dāng)使用@Autowired標(biāo)注setter方法時(shí),默認(rèn)采用byType自動(dòng)裝配策略舰讹。
@Component
public class Chinese implements Persion{
...
//axe的setter方法
public void setAxe(Axe axe){
this.axe=axe;
}
...
}
Spring將會(huì)自動(dòng)搜索容器中類型為Axe的Bean實(shí)例茅姜,并將該Bean實(shí)例作為setAxe()方法的參數(shù)轉(zhuǎn)入。
Spring還允許使用@Autowored來(lái)標(biāo)注多個(gè)參數(shù)的普通方法月匣。
@Component
public class Chinese implements Persion{
....
//可接受多個(gè)參數(shù)的普通方法
@Autowired
public void prepare(Axe axe,Dog dog){
this.axe=axe;
this.dog=dog;
}
}
當(dāng)使用@Autowored修飾帶多個(gè)參數(shù)的普通方法時(shí)钻洒,Spring會(huì)自動(dòng)到容器中尋找類型匹配的Bean奋姿,如果恰好為每個(gè)參數(shù)都找到一個(gè)類型匹配的Bean,Spring會(huì)自動(dòng)為這些Bean作為參數(shù)來(lái)調(diào)用該方法素标。
@Autowired用于修飾構(gòu)造方法和實(shí)例變量称诗。
@Component
public class Chinese implements Persion{
@Autowired
private Axe axe;
@Autowired
public Chinese(Axe axe,Dog dog){
......
}
}
當(dāng)使用@Autowired修飾一個(gè)實(shí)例變量是,Spring將會(huì)把容器中與該實(shí)例變量匹配的Bean設(shè)置為該實(shí)例的值头遭。
@Autowired用于修飾數(shù)組類型的成員變量粪狼。
@Component
public class Chinese implements Persion{
@Autowired
private Axe[] axe;
...........
}
Spring會(huì)自動(dòng)搜索容器中所有的Axe實(shí)例,并以這些Axe實(shí)例作為數(shù)組元素來(lái)創(chuàng)建數(shù)組任岸。
@Autowired也可標(biāo)注集合類型的實(shí)例變量。
@Component
public class Chinese implements Persion{
private Set<Axe> axes;
@Autowired
public void setAxes(Set<Axe> axes){
this.axes=axes;
}
}
Spring會(huì)自動(dòng)搜索容器中的所用Axe實(shí)例狡刘,并將這些實(shí)例注入到axes實(shí)例變量中享潜。
Spring提供了@Qualifier注解,通過(guò)使用@Qualifier嗅蔬,允許通過(guò)Bean的id來(lái)執(zhí)行自動(dòng)裝配剑按。