注入方式
1.屬性setter注入(需要無參構(gòu)造方法):
對依賴對象寫入setter方法莱革,通過setter的對象注入容器中
<!-- 屬性注入(setter注入)-->
<!--將容器中的messageService卑惜,注入給當(dāng)前Controller的messageService屬性(通過該屬性的setter)-->
<property name = "messageService" ref="messageServiceBean"/>
public MessageController(){
}
public void setMessageService(IMessageService messageService){
this.messageService = messageService;
}
2.構(gòu)造方法注入(有參構(gòu)造方法):
注入對象構(gòu)造方法中的參數(shù),使容器識別依賴對象
<bean id="msgControllerBean" class="com.apescource.web.MessageController">
<!-- 構(gòu)造注入(構(gòu)造方法注入)-->
<constructor-arg name="messageService" ref="messageServiceBean"/>
<constructor-arg name="defaultMsg" value="默認消息模板"/>
</bean>
public MessageController(String defaultMsg,IMessageService messageService){
System.out.println(defaultMsg+":");
this.messageService = messageService;
}
3.接口注入
接口注入模式因為具備侵入性啥繁,它要求組件必須與特定的接口相關(guān)聯(lián)府瞄,因此并不被看好托猩,實際使用有限印蓖。
注入類型
1.注入字符串,單個數(shù)值類型
<bean id = "example" class = com.example>
<property name = " " value = " "></property>
</bean>
2.注入bean京腥,引用類型
<bean id = "outerBean" class="com.outer">
<property name = "target">
<bean class = "com.Person">
<property name = "name" value = "Amy">
<property name = "age" value = "18">
</bean>
</property>
</bean>
3.注入集合赦肃,List、set公浪、Map他宛、Properties等
<bean id = "listBean" class = "com.rep.example01">
<property name = "list">
<list>
<value>一</value>
<value>二</value>
<value>三</value>
<ref bean = "dataSource"/>
</list>
</property>
</bean>
<bean id = "setBean" class = "com.rep.example02">
<property name = "set">
<set>
<value>張</value>
<value>王</value>
<value>李</value>
<ref bean = "dataSource"/>
</set>
</property>
</bean>
<bean id = "mapBean" class = "com.rep.example03">
<property name = "map">
<map>
<entry key = "an entry" value = "just some string"/>
<entry key = "a ref" value = "dataSource"/>
</map>
</property>
</bean>
<bean id = "propsBean" class = "com.rep.example04">
<property name = "props">
<props>
<prop key = "一">one</prop>
<prop key = "二">two</prop>
<prop key = "三">three</prop>
<props>
</property>
</bean>
Spring常用注解
1.@Component:類級別注解,標(biāo)注一個普通的spring Bean類欠气。表明該類會作為組件類厅各,并告知Spring要為這個類創(chuàng)建Bean。
@Componet
public class DataService{
private IRepostitory repostitory;
public boolean saveData(){...};
}
2.@ComponentScan:類級別注解预柒,使用在配置類队塘。啟用組件掃描,默認當(dāng)前配置類所在包為基礎(chǔ)包宜鸯。
// 基礎(chǔ)包
@ComponentScan(basePackages="com.codeup")
public class SystemSpringConfig{
}
//指定類所在包為基礎(chǔ)包
@ComponentScan(basePackageClasses = ICodeupMarker.class)
public class SystemSpringConfig{
}
3.@Autowired:方法級別注解憔古,自動注入一個符合類型要求的Bean。(參數(shù):required:是否為必須注入項)
@Autowired
public DataService(Irepostitory repostitory){
this.repostitory = repostitory;
}
4.@Primary:類級別的注解淋袖,一般與@Component配合使用鸿市,在自動裝備時候,設(shè)置某個bean為首選。
@Component
@Primary
public class FileRepostitoryImpl implements Irepostitory{
public boolean writeData(List<String> dataList){
}
}
5.@Qualifier:方法級別注解(一般使用于:構(gòu)造器焰情、Setter方法陌凳、普通方法),指定所注入的bean的ID内舟。(參數(shù):value:所注入的bean的ID)
@Autowired
@Qualifier("fileReopstitoryImpl")
public void setRepostitory(IRepostitory repostitory){
this.repostitory = repostitory;
}
6.@Scope:類級別聲明注解冯遂,定義Bean的作用域。(參數(shù):value:作用域參數(shù)值谒获、singleton、prototype等)
public class DataService{
}