一.从XML文件中获取Bean的方法
1.采用BeanFactory方式
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean helloBean = (HelloBean)factory.getBean( " helloBean " );
采用BeanFactory方式,可以利用XmlBeanFactory从xml配置文件中获得bean,也可以使用其它方式,比如利用PropertiesFactoryBean从.property文件中获得。
2.采用ApplicationContext方式
采用BeanFactory对简单的应用程序来说就够了,可以获得对象管理上的便利性。
但是要获取一些特色和高级的容器功能,可以采用ApplicationContext。
ApplicationContext提供了一些高级的功能,比如:
1.提供取得资源文件更方便的方法
2.提供文字消息解析的方法
3.支持国际化(i18n)消息
4.可以发布事件,对事件感兴趣的Bean可以接收这些事件
Spring创始者建议采用ApplicationContext取代BeanFactory。
在实现ApplicationContext借口的类中,常用的几个:
FileSystemXmlApplicationContext:
可以指定XML定义文件的相对路径或者绝对路径来读取定义文件。
ClassPathXmlApplicationCOntext:
可以从classpath中读取XML文件
XmlWebApplicationCOntext:
从Web应用程序的文件架构中,指定相对位置来读取定义文件。
一个简单的例子:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringDemo {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext( " beans-config.xml " );
HelloBean helloBean = (HelloBean)context.getBean( " helloBean " );
System.out.print( " Name: " );
System.out.println(helloBean.getName());
System.out.print( " Word: " );
System.out.println(helloBean.getHelloWord());
}
}
二.Type 2 IoC, Type 3 IoC
Type2是利用setter方法完成依赖注入,这是Spring鼓励的。但也允许使用Type 3注入,也就是利用构造函数完成注入。
例如:
public class HelloBean {
private String helloWord;
private String name;
public HelloBean() {
}
public HelloBean(String helloWord, String name) {
this .helloWord = helloWord;
this .name = name;
}
public String getHelloWord() {
return helloWord;
}
public void setHelloWord(String helloword) {
this .helloWord = helloword;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
}
配置文件:
beans-config.xml
<! DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="helloBean" class ="cn.blogjava.hello.HelloBean" >
< constructor-arg index ="0" >
< value > YYY! </ value >
</ constructor-arg >
< constructor-arg index ="1" >
< value > Hello! </ value >
</ constructor-arg >
</ bean >
</ beans >
三.属性参考
如果在Bean文件中已经有一个定义的Bean实例,可以让某个属性直接参考这个实例。
import java.util.Date;
public class HelloBean {
private String helloWord;
private Date date;
public HelloBean() {
}
public String getHelloWord() {
return helloWord;
}
public void setHelloWord(String helloword) {
this .helloWord = helloword;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this .date = date;
}
}
配置文件
beans-config.xml
<! DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="dateBean" class ="java.util.Date" />
< bean id ="helloBean" class ="cn.blogjava.hello.HelloBean" >
< property name ="helloWord" >
< value > Hello! </ value >
</ property >
< property name ="date" >
< ref bean ="dateBean" />
</ property >
</ bean >
</ beans >
如果某个Bean的实例只被参考一次,可以直接在属性定义时使用<bean>标签,并仅需要指定其"class"属性即可。
< property name ="date" >
< bean class ="java.util.Date" />
</ property >
四.自动绑定
byType
<! DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="dateBean" class ="java.util.Date" />
< bean id ="helloBean" class ="cn.blogjava.hello.HelloBean" autowire="byType" >
< property name ="helloWord" >
< value > Hello! </ value >
</ property >
< property name ="name" >
< value > YYY! </ value >
</ property >
</ bean >
</ beans >
这
里并没有指定helloBean的data属性,而是通过自动绑定,指定了"byType",所以会根据helloBean的setDate()方法所接
受的类型,来判断定义文件中是否有类似的对象,并将之设定给helloBean的setDate(),如果无法完成绑定,会抛异常。
byName
根据id属性上指定的别名是否与setter名称一致来绑定。无法绑定,仅维持未绑定状态,不抛异常。
<! DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="date" class ="java.util.Date" />
< bean id ="helloBean" class ="cn.blogjava.hello.HelloBean" autowire ="byName" >
< property name ="helloWord" >
< value > Hello! </ value >
</ property >
< property name ="name" >
< value > YYY! </ value >
</ property >
</ bean >
</ beans >
autowire="constructor"
根据构造方法上的参数类型,来匹配。无法绑定抛异常。
autowire="autodetect"
会试着用constructor,byType等方式来建立依赖关系。
加入依赖检查
依赖检查有4种方式:
simple只检查简单的属性,例如int或者String类型对象是否完成依赖。
objects检查对象类型的属性是否完成依赖。
all检查所有的属性,none是默认值,不检查依赖性。
< property name ="helloWord" >
< value > Hello! </ value >
</ property >
< property name ="name" >
< value > YYY! </ value >
</ property >
</ bean >
五.集合对象的注入
对于像数组、List、Set、Map等集合对象,在注入前若必须填充一些对象到集合中,然后再将集合对象注入到Bean中时,可以交给Spring的IoC容器自动维护或者生成集合对象,并完成依赖注入。
SomeBean.java
import java.util.List;
import java.util.Map;
public class SomeBean {
private String[] someStrArray;
private Some[] someObjectArray;
private List someList;
private Map someMap;
public List getSomeList() {
return someList;
}
public void setSomeList(List someList) {
this .someList = someList;
}
public Map getSomeMap() {
return someMap;
}
public void setSomeMap(Map someMap) {
this .someMap = someMap;
}
public Some[] getSomeObjectArray() {
return someObjectArray;
}
public void setSomeObjectArray(Some[] someObjectArray) {
this .someObjectArray = someObjectArray;
}
public String[] getSomeStrArray() {
return someStrArray;
}
public void setSomeStrArray(String[] someStrArray) {
this .someStrArray = someStrArray;
}
}
Some.java
public class Some {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String toString(){
return name;
}
}
beans-config.xml
<! DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="some1" class ="cn.blogjava.hello.Some" >
< property name ="name" >
< value > YYY </ value >
</ property >
</ bean >
< bean id ="some2" class ="cn.blogjava.hello.Some" >
< property name ="name" >
< value > BYF </ value >
</ property >
</ bean >
< bean id ="someBean" class ="cn.blogjava.hello.SomeBean" >
< property name ="someStrArray" >
< list >
< value > Hello! </ value >
< value > Welcome! </ value >
</ list >
</ property >
< property name ="someObjectArray" >
< list >
< ref bean ="some1" />
< ref bean ="some2" />
</ list >
</ property >
< property name ="someList" >
< list >
< ref bean ="some1" />
< ref bean ="some2" />
</ list >
</ property >
< property name ="someMap" >
< map >
< entry key ="MapTest" >
< value > Hello YYY! </ value >
</ entry >
< entry key ="someKey1" >
< ref bean ="some1" />
</ entry >
</ map >
</ property >
</ bean >
< bean id ="helloBean" class ="cn.blogjava.hello.HelloBean" >
</ bean >
</ beans >
测试类:SpringDemo.java
package
cn.blogjava.hello;
import
java.util.List;
import
java.util.Map;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.FileSystemXmlApplicationContext;
public
class
SpringDemo {
public
static
void
main(String[] args) {
ApplicationContext context
=
new
FileSystemXmlApplicationContext(
"
beans-config.xml
"
);
SomeBean someBean
=
(SomeBean)context.getBean(
"
someBean
"
);
//
取得数组类型依赖注入对象
String[] strs
=
(String[])someBean.getSomeStrArray();
Some[] somes
=
(Some[])someBean.getSomeObjectArray();
for
(
int
i
=
0
; i
<
strs.length; i
++
){
System.out.println(strs[i]
+
"
,
"
+
somes[i].getName());
}
System.out.println();
//
取得List类型依赖注入对象
List someList
=
someBean.getSomeList();
for
(
int
i
=
0
; i
<
someList.size(); i
++
) {
System.out.println(someList.get(i));
}
System.out.println();
//
取得Map类型依赖注入对象
Map someMap
=
someBean.getSomeMap();
System.out.println(someMap.get(
"
MapTest
"
));
System.out.println(someMap.get(
"
someKey1
"
));
}
}