Spring02:基本配置与依赖注入,贼好用的 Java 学习路线集合
<bean id="applicationDaoQQImp" class="sharm.dao.ApplicationDaoQQImp"/>
<bean id="appServiceImp" class="sharm.service.AppServiceImp">
<property name="appDaoImp" ref="applicationDaoQQImp"/>
</bean>
<alias name="appServiceImp" alias="alias"/>
</beans>
通过使用<import>
标签可以在一个配置文件中导入其它配置文件,因此也可以在其中使用其它配置文件的 bean。
<import resource="otherBean"/>
========================================================================
这里写上单例(Singleton)模式与原型(Prototype)模式两种,其它模式见官方文档。
单例模式表示在 Spring 容器中仅存在一个 Bean 实例,无论建立多少个 Bean 示例,它们都是同一个。这是 Spring 默认的模式。
<bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">
原型模式与单例模式刚好相反。其表示每次对容器中进行 getter 操作的时候,就会产生一个新的对象。
<bean id="account" class="com.foo.DefaultAccount" scope="prototype"/>
或者
<bean id="account" class="com.foo.DefaultAccount" singleton="false"/>
=============================================================================
Setter 方法注入即通过 Setter 方法为对象的属性注入值,是最常用的方式。
下面使用 Setter 注入来介绍不同类型的变量的注入方式。
首先书写测试的用例:
实体类:
package sharm.temp;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package sharm.temp;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public Student() {
}
public Student(String name, Address address, String[] books, List<String> hobbys, Map<String, String> card, Set<String> games, String wife, Properties info) {
this.name = name;
this.address = address;
this.books = books;
this.hobbys = hobbys;
this.card = card;
this.games = games;
this.wife = wife;
this.info = info;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public void show(){
System.out.println("name="+ name
",address="+ address.getAddress()
",books="
);
for (String book:books){
System.out.print("<<"+book+">>\t");
}
System.out.println("\n 爱好:"+hobbys);
System.out.println("card:"+card);
System.out.println("games:"+games);
System.out.println("wife:"+wife);
System.out.println("info:"+info);
}
}
1)常量注入
<bean id="address" class="sharm.temp.Address">
<property name="address" value="LanXi"/>
</bean>
2)Bean 注入
<bean id="Student" class="sharm.temp.Student">
<property name="name" value="Sharm"/>
<property name="address" ref="address"/>
</bean>
3)数组注入
<bean id="Student" class="sharm.temp.Student">
<property name="books">
<array>
<value>三体</value>
<value>人生</value>
<value>活着</value>
</array>
</property>
</bean>
**4)List
注入**
<bean id="Student" class="sharm.temp.Student">
<property name="hobbys">
<list>
<value>跑步</value>
<value>健身</value>
<value>篮球</value>
</list>
</property>
</bean>
5)Set 注入
<bean id="Student" class="sharm.temp.Student">
<property name="games">
<set>
<value>学习</value>
<value>学习+1</value>
</set>
</property>
</bean>
6)Map 注入
<bean id="Student" class="sharm.temp.Student">
<property name="card">
<map>
<entry key="中国银行" value="12323344"/>
<entry key="中国农业银行" value="23344444"/>
</map>
</property>
</bean>
7)Null 注入
<bean id="Student" class="sharm.temp.Student">
<property name="wife">
<null/>
</property>
</bean>
8)配置文件注入
评论