1、小案例
这里我们定义两个类方法,分别是MathDemo和EnglishDemo,分别执行学习数学和学习英语的方法;、
package com.test2;
public class MathDemo {
public void StudyMath(){
System.out.println("学习数学。。。。");
}
}
package com.test2;
public class EnglishDemo {
public void StudyEnglish(){
System.out.println("学习英语。。。。");
}
}
我们再定义一个学习类,包括MathDemo和EnglishDemo类的两个属性,和一个学习方法StudyDemo:
package com.test2;
public class StudyDemo {
private MathDemo mathDemo;
private EnglishDemo englishDemo;
public StudyDemo(){
}
public StudyDemo(MathDemo mathDemo,EnglishDemo englishDemo){
this.mathDemo=mathDemo;
this.englishDemo=englishDemo;
}
//学习方法
public void study(){
mathDemo.StudyMath();
englishDemo.StudyEnglish();
}
}
这是我们配置的study.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="math" class="com.test2.MathDemo"/>
<bean id="english" class="com.test2.EnglishDemo"/>
<!--构造注入MathDemo和StudyDemo-->
<bean id="StudyDemo" class="com.test2.StudyDemo">
<constructor-arg ref="math"/>
<constructor-arg ref="english"/>
</bean>
</beans>
我们再新建测试类StudyTest:
package com.test2;
import com.test1.IntrduceDemo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStudy {
public static void main(String[] args) {
//创建Spring上下文(加载study.xml)
ApplicationContext acx= new ClassPathXmlApplicationContext("study.xml");
//获取StudyDemo实例并赋值
StudyDemo sd=acx.getBean("StudyDemo",StudyDemo.class);
//调用方法
sd.study();
//销毁实例对象
((ClassPathXmlApplicationContext) acx).close();
}
}
运行结果:
2、注入方式
1)构造注入
上例中我们应用的是构造注入,这种注入方式需要在被注入的Javabean中重载一个有参的构造方法:
package com.test2;
public class StudyDemo {
…
…
public StudyDemo(MathDemo mathDemo,EnglishDemo englishDemo){
this.mathDemo=mathDemo;
this.englishDemo=englishDemo;
}
….
…
}
在study.xml中配置:
<bean id="math" class="com.test2.MathDemo"/>
<bean id="english" class="com.test2.EnglishDemo"/>
<bean id="StudyDemo" class="com.test2.StudyDemo">
<!--构造注入MathDemo和StudyDemo-->
<constructor-arg ref="math"/>
<constructor-arg ref="english"/>
</bean>
2)Setter方法注入(属性注入)
set方法的注入需要在被注入的javaBean中有setter方法
package com.test2;
public class StudyDemo {
private MathDemo mathDemo;
private EnglishDemo englishDemo;
public void setMathDemo(MathDemo mathDemo) {
this.mathDemo = mathDemo;
}
public void setEnglishDemo(EnglishDemo englishDemo) {
this.englishDemo = englishDemo;
}
public void study(){
mathDemo.StudyMath();
englishDemo.StudyEnglish();
}
}
在study.xml中配置:
<bean id="math" class="com.test2.MathDemo"/>
<bean id="english" class="com.test2.EnglishDemo"/>
<!--属性注入MathDemo和StudyDemo-->
<bean id="StudyDemo" class="com.test2.StudyDemo">
<property name="mathDemo" ref="math"/>
<property name="englishDemo" ref="english"/>
</bean>
*注以上两种注入方式是最常用的,还有工厂注入的方法,感兴趣可自行了解!
3、自动装配
set注入和构造注入有时在做配置时比较麻烦。所以框架为了提高开发效率,提供自动装配功能,简化配置。Spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean>标签的autowire属性
自动装配autowire属性有6个值可选,分别代表不同的含义。
no | 不支持自动装配功能 |
byName | 从Spring环境中获取目标对象时,目标对象中的属性会根据名称在整个Spring环境中查找<bean>标签的id属性值。如果有相同的,那么获取这个对象,实现关联。
整个Spring环境:表示所有的spring配置文件中查找,那么id不能有重复的。 |
byType | 从Spring环境中获取目标对象时,目标对象中的属性会根据类型在整个spring环境中查找<bean>标签的class属性值。如果有相同的,那么获取这个对象,实现关联。 |
constructor | 使用构造方法完成对象注入,其实也是根据构造方法的参数类型进行对象查找,相当于采用byType的方式。 |
autodetect | 自动选择:如果对象没有无参数的构造方法,那么自动选择constructor的自动装配方式进行构造注入。如果对象含有无参数的构造方法,那么自动选择byType的自动装配方式进行setter注入。 |
default | 表示默认采用上一级标签的自动装配的取值。如果存在多个配置文件的话,那么每一个配置文件的自动装配方式都是独立的。 |
我们这里用constructor举例:StudyDemo如下
package com.test2;
public class StudyDemo {
private MathDemo mathDemo;
private EnglishDemo englishDemo;
public StudyDemo(){
}
public StudyDemo(MathDemo mathDemo,EnglishDemo englishDemo){
this.mathDemo=mathDemo;
this.englishDemo=englishDemo;
}
public void study(){
mathDemo.StudyMath();
englishDemo.StudyEnglish();
}
}
配置文件如下:
<bean id="math" class="com.test2.MathDemo"/>
<bean id="english" class="com.test2.EnglishDemo"/>
<!--构造注入自动装配MathDemo和StudyDemo-->
<bean id="StudyDemo" class="com.test2.StudyDemo" autowire="constructor">
</bean>