Bean的屬性注入
1、構造方法注入
通過構造方法注入Bean的屬性值或宜蘭的對象,它保證了Bean執行個體在執行個體化後就可以使用。
構造器注入在元素裡聲明的屬性
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
配置檔案
<!--bean屬性注入構造方法注入-->
<bean id="person" class="com.ioc.demo4.Person">
<constructor-arg name="name" value="張三"/>
<constructor-arg name="age" value="23"/>
</bean>
2、set方法的屬性注入
spring配置檔案中通過設定注入的屬性
對象類型值用ref
<!--bean屬性注入set注入-->
<bean id="person" class="com.ioc.demo4.Person">
<property name="name" value="李四"/>
<property name="age" value="25"/>
<property name="cat" ref="cat"/>
</bean>
<bean id="cat" class="com.ioc.demo4.Cat" >
<property name="name" value="大黃"
</bean>
3、p名稱空間屬性注入
需要在配置加上
xmlns:p="http://www.springframework.org/schema/p"
<!--p:名稱空間注入-->
<bean id="person" class="com.ioc.demo4.Person" p:age="25" p:name="李四" p:cat-ref="cat"/>
<bean id="cat" class="com.ioc.demo4.Cat" p:name="大黃"/>
4、SpEl注入
spring expression language,spring表達式語言,對依賴注入進行簡化
文法:#{表達式}
<bean id="person" class="com.ioc.demo4.Person">
<property name="name" value="#{'李四'}"/>
<property name="age" value="#{25}"/>
<property name="cat" value="#{cat}"/>
</bean>
<bean id="cat" class="com.ioc.demo4.Cat" >
<property name="name" value="#{'大黃'}"/>
</bean>
5、複雜類型注入
建立一個類叫CollectionBean,這裡省去了get和set,以及toString方法。記得加上。
public class CollectionBean {
private String[] arrs;//數組類型
private List<String> list;//List集合類型
private Set<String> set;//Set集合類型
private Map<String,Integer> map;//Map集合類型
private Properties properties;
}
配置檔案
<!--集合(複雜)類型的屬性注入-->
<bean id="collectionBean" class="com.ioc.demo5.CollectionBean">
<!--數組類型-->
<property name="arrs">
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
<!--list集合的屬性注入,基本類型用value,對象類型用ref-->
<property name="list">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<!--Set集合的屬性注入-->
<property name="set">
<set>
<value>ddd</value>
<value>eee</value>
<value>fff</value>
</set>
</property>
<!--map集合的屬性注入-->
<property name="map">
<map>
<entry key="aa" value="11"></entry>
<entry key="bb" value="22"></entry>
<entry key="bb" value="33"></entry>
</map>
</property>
<!--Property類型的屬性注入-->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
6、使用注解屬性注入
使用注解管理Bean
@Component(“beanId”)
可細分為以下三類,更清晰
@Repository用于對DAO實作類進行标注
@Service用于對Service實作類進行标注
@Controller用于對Controller實作類進行标注
Spring屬性注入的注解
簡單類型用value
對象類型
@Autowired自動比對相同類名
強制要求按照注解id需要使用Qualifier(“beanId”)
@Resource(name=“beanId”)
相當于
@Autowired
@Qualifier(“beanId”)
Spring的其他注解
@Scope注解用于指定Bean的作用範圍,預設單例singleton
@PostConstruct相當于配置檔案中init-method="init_method_name"當bean被載入到容器的時候調用
@PreDestroy當bean從容器中删除的時候調用(scope=singleton有效)
XML與注解整合
XML方式的優勢:結構清晰,易于閱讀
注解方式的優勢:開發便捷,屬性注入友善
XML與注解的整合開發(将Bean管理交給XML,屬性注入使用注解開發)
1、引入context命名空間
2、在配置檔案中添加context:annotation-config标簽