在应用程序对象之间创建和管理关联的过程构成了DI的核心,通常称为连接。
将依赖项注入应用程序对象,需要在基于XML的配置文件中配置它们。
在XML文件中,你需要先对想要调用的对象定义一个Bean。在其他文件里即可调用这个Bean,来实现调用该对象。
创建一个Java例子:Sports
首先写一个想要调用的类(对象):team.java
文件地址为“sports/team”
public class team {
private int number1=15;
public team(){
}
public team(int number){
number=this.number1;
}
public void shownumber(){
System.out.println("the number is "+number1);
}
}
再创建一个XML文件来配置Bean :XMLConfig.xml
文件地址为“sports/XMLConfig”
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
">
<!--以上的内容都在创建XML文件时自动引入的-->
<bean id="teamnumber" class="sports.team"></bean>
<!--在上一句定义Bean的id,声明要注入的对象的地址。成功定义了一个bean元素。-->
</beans>
在bean元素里也可以定义一些属性
<beans ... >
<bean id="teamnumber" class="sports.team">
<property name="property_name" value="property_value"/>
</bean>
</beans>
最后调用这个bean元素
我选择在main函数里调用,main函数所在文件:sports.java
文件所在位置:“sports/sports.java”
public class Sports {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Resource res=(Resource) new ClassPathResource("sports/XMLConfig.xml");
//创建一个资源类型对象,调用bean所在的XML文件
//要调用bean,而bean是在xml配置文件里声明的,当然要调用bean对应的XML配置文件
BeanFactory factory=new XmlBeanFactory(res);
//通过已经引用的资源,创建与资源所匹配类型的Bean工厂,来存储各个Bean元素
team ta=(team) factory.getBean("teamnumber");
//调用Bean元素,以此来调用原本想要调用的目标对象team类
//team类型的对象最终要以team类的实例引用
//team类的实例(对象)ta最终等于引用team类的bean
//从而为ta赋予了值,让ta可以使用team类里的方法和变量。
//如果不用bean注入的话,需要给ta再new一个team类,才能使用team类里面的方法和变量。
ta.shownumber();
//使用team类里的方法
}
}
除了通过reasource调用资源,也可以使用ApplicationContext的方法
新建一个方法:
public void getBean(){
/*以下是配置文件地址在项目根目录里的资源声明情况*/
String xmlPath="sports/XMLConfig.xml":
//预备好bean所在XML文件的地址
ApplicationContext ac = new ClassPathXmlApplicationContext(xmlpath);
//通过ApplicationContext调用xml的方法
/*以下是配置文件地址不放在项目根目录下的资源声明情况*/
String xmlPath = "WebContent/WEB-INF/config/base/applicationContext.xml";
//假设了一个配置文件地址,不是放在根目录下
ApplicationContext ac = new FileSystemXmlApplicationContext(xmlPath);
// 这里由于我的配置文件写的目录不是根目录,所以用FileSystemXmlApplicationContext
/*资源声明完成(applicationContext对象创建完成)*/
/*下面开始调用*/
ac.getBean("beanName");
//调用bean方法:
//getBean(String name)
//getBean(Class<T> type)
//getBean(String name,Class<T> type)
//getBean(String name,Object[] args)
//前两种调用方法都需要保证Bean的id或类型在容器中唯一
}
以上都是手动获取ApplicationContext对象
这种获取application资源方法一般用在写junit测试的时候, 实际项目中强烈不推荐使用,因为Spring容器在服务器加载的时候,就已经被初始化了,这里相当于又重新加载了一次,没有必要而且耗资源,并且非常慢。