程序中的之间的关系,不用代码控制,而彻底是由容器来控制。在运行阶段,容器会根据配置信息直接把他们的关系注入到组件中spring
强调程序依赖关系由Spring容器控制ide
之前的开发场景中,咱们每每经过硬编码的形式维护依赖关系。这种方式使得功能模块间耦合度极高(紧耦合),不易维护学习
使用依赖注入后,依赖关系及实例化由Spring容器帮咱们维护,并注入到须要使用的功能模块中测试
强调程序间依赖关系由Spring容器在程序运行时注入依赖关系Beanthis
程序间的依赖关系彻底由Spring容器管理(注解,XML配置)。使得系统模块间依赖度和耦合度有效下降,易维护,可扩展,松耦合编码
/**
* Created by Administrator on 2017/1/16/016.
* 构造器注入示例
*
*/spa
public class BraveKnight implements Knight {xml
private Quest quest;对象
public BraveKnight(Quest quest) {接口
this.quest = quest;
}
@Override
public void embarkOnQuest() {
quest.embark();
}
}
构造器依赖注入配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
XML配置建立SlayDragonQuest对象(Spring托管)
constructor-arg:构造方法注入参数属性
value:属性值(此配置中使用Spring表达式语言将System.out传入到构造器)
-->
<bean id="quest" class="springinaction.chapter1.SlayDragonQuest">
<constructor-arg value="#{T(System).out}"/>
</bean>
<!--
XML配置建立BraveKnight对象(Spring托管)
constructor-arg:构造方法注入参数属性(SlayDragonQuest对象)
ref:引用Bean注入对象(经过已声明bean的id查找并注入)
-->
<bean id="knight" class="springinaction.chapter1.BraveKnight">
<constructor-arg ref="quest"/>
</bean>
</beans>
测试类
/**
* Created by Administrator on 2017/1/16/016.
* 构造器依赖注入测试类
*/
public class KnightMain {
public static void main(String[] args) throws Exception
{
/**
*加载配置文件获取Spring上下文对象
*/
ApplicationContext context=new ClassPathXmlApplicationContext("conf/chapter1/IoCDemo.xml");
/**
*Spring上下文对象根据id建立Bean
*/
BraveKnight knight=(BraveKnight)context.getBean("knight");
/**
*调用Bean对应方法
*/
knight.embarkOnQuest();
}
}
注意事项:
构造方法传入参数为依赖接口
配置文件中依赖接口<bean></bean>构造方法依赖注入参数(constructor-arg),引用(ref)值为声明Bean的标识(id)
/**
* Created by Administrator on 2017/1/17/017.
* Set方法注入示例
*/
public class BraveKnightTestSet implements Knight {
private Quest quest;
public Quest getQuest() {
return quest;
}
public void setQuest(Quest quest) {
this.quest = quest;
}
@Override
public void embarkOnQuest() {
quest.embark();
}
Set方法配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
XML配置建立SlayDragonQuest对象(Spring托管)
constructor-arg:构造方法注入参数属性
value:属性值(此配置中使用Spring表达式语言将System.out传入到构造器)
-->
<bean id="quest" class="springinaction.chapter1.SlayDragonQuest">
<constructor-arg value="#{T(System).out}"/>
</bean>
<!--
XML配置建立BraveKnight对象(Spring托管)
property:set方法注入参数属性(SlayDragonQuest对象),name的vakue与Bean对应PropertyName一致
ref:引用Bean注入对象(经过已声明bean的id查找并注入)
-->
<bean id="knightSet" class="springinaction.chapter1.BraveKnightTestSet">
<property name="quest" ref="quest"/>
</bean>
</beans>
测试类
/**
* Created by Administrator on 2017/1/17/017.
*/
public class BraveKnightTestSetMain {
public static void main(String[] args)
{
/**
*使用ClassPathXmlApplicationContext加载应用上下文
*/
ApplicationContext context=new ClassPathXmlApplicationContext("conf/chapter1/IocSetDemo.xml");
BraveKnightTestSet knightTestSet=(BraveKnightTestSet)context.getBean("knightSet");
knightTestSet.embarkOnQuest();
}
}
注意事项
须要为注入属性提供setXXX()方法
配置文件中<bean></bean>添加<property name=”xxx” ref(value)=”xxx”/>
其中name为依赖属性名称
Ref为引用Bean标识(id)
Value为实际依赖值
/**
* Created by Administrator on 2017/1/18/018.
* 静态工厂
*/
public class AnimalStaticFactor {
/**
*
* @param type:动物类型
* @return Animal
*/
public static Animal getAnimal(String type)
{
if("cat".equalsIgnoreCase(type))
{
return new Cat();
}
else
{
return new Dog();
}
}
}
静态工厂依赖注入实例类
/**
* Created by Administrator on 2017/1/18/018
* 静态工厂依赖注入实例.
*/
public class Cat implements Animal {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void sayHello() {
System.out.println(message+",瞄~瞄");
}
}
静态工厂依赖注入实例类
/**
* Created by Administrator on 2017/1/18/018.
* 静态工厂依赖注入示例
*/
public class Dog implements Animal {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void sayHello() {
System.out.println(message+",汪~汪");
}
}
静态工厂依赖注入配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置AnimalFactor的getAnimal方法,使其建立Cat对象
class:工厂类
factor-method:静态工厂方法
constructor-arg value:静态工厂参数
property name:Cat实例须要注入的属性
-->
<bean id="catFactor" class="springinaction.chapter1.AnimalStaticFactor" factory-method="getAnimal">
<constructor-arg value="cat"/>
<property name="message" value="猫猫"/>
</bean>
<!--
配置AnimalFactor的getAnimal方法,使其建立Dog对象
class:工厂类
factor-method:静态工厂方法
constructor-arg value:静态工厂参数
property name:Dog实例须要注入的属性
-->
<bean id="dogFactor" class="springinaction.chapter1.AnimalStaticFactor" factory-method="getAnimal">
<constructor-arg value="dog"/>
<property name="message" value="狗狗"/>
</bean>
</beans>
静态工厂测试类
/**
* Created by Administrator on 2017/1/18/018.
*/
public class AnimalStaticFactorTest {
public static void main(String[] args){
/**
*使用ClassPathXmlApplicationContext加载应用上下文
*/
ApplicationContext context=new ClassPathXmlApplicationContext("conf/chapter1/IocStaticFactorDemo.xml");
/**
*经过上下文对象建立标识为catFactor的Bean并调用方法
*/
Animal cat=context.getBean("catFactor",Animal.class);
cat.sayHello();
/**
*经过上下文对象建立标识为dogFactor的Bean并调用方法
*/
Animal dog=context.getBean("dogFactor",Animal.class);
dog.sayHello();
}
}
注意事项
工厂类须要提供静态方法生成实例Bean(实现特定接口),返回值为接口类型
配置文件中class指向接口彻底名
Factor-method指定返回接口类型方法(实际返回的是根据参数创造的接口实现类)
依赖注入参数(constructor-arg)为静态工厂返回接口类型静态方法依赖的参数
<property name=”xxx” ref(value)=”xxx”/>
其中name为依赖属性名称
Ref为引用Bean标识(id)
Value为实际依赖值
/**
* Created by Administrator on 2017/1/18/018.
*实例工厂
*/
public class AnimalFactor {
/**
*
* @param type:动物类型
* @return 根据动物类型建立的动物实例对象
*/
public Animal getAnimal(String type)
{
if("cat".equalsIgnoreCase(type))
{
return new Cat();
}
else
{
return new Dog();
}
}
}
实例工厂依赖注入实例类
/**
* Created by Administrator on 2017/1/18/018
* 实例工厂依赖注入实例.
*/
public class Cat implements Animal {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void sayHello() {
System.out.println(message+",瞄~瞄");
}
}
实例工厂依赖注入实例类
/**
* Created by Administrator on 2017/1/18/018.
* 实例工厂依赖注入示例
*/
public class Dog implements Animal {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void sayHello() {
System.out.println(message+",汪~汪");
}
}
实例工厂配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--声明动物工厂类,id标识为animalFactor-->
<bean id="animalFactory" class="springinaction.chapter1.AnimalFactor"/>
<!--声明建立Cat的Bean,标识为catFactor
factor-bean:引用AnimalFactor
factor-method:AnimalFactor类生成Animal实例Bean的方法
property name:Cat类依赖注入属性名称
property value:Cat类依赖注入属性值
-->
<bean id="catFactor" factory-bean="animalFactory" factory-method="getAnimal">
<constructor-arg value="cat"/>
<property name="message" value="猫猫"/>
</bean>
<!--
声明建立Dog的Bean,标识为dogFactor
factor-bean:引用AnimalFactor
factor-method:AnimalFactor类生成Animal实例Bean的方法
property name:Dog类依赖注入属性名称
property value>Dog类依赖注入属性值
-->
<bean id="dogFactor" factory-bean="animalFactory" factory-method="getAnimal">
<constructor-arg value="dog"/>
<property name="message" value="狗狗"/>
</bean>
</beans>
实例工厂测试类
/**
* Created by Administrator on 2017/1/18/018.
*/
public class AnimalFactorTest {
public static void main(String[] args){
/**
*使用ClassPathXmlApplicationContext加载应用上下文,建立上下文对象
*/
ApplicationContext context=new ClassPathXmlApplicationContext("conf/chapter1/IocStaticFactorDemo.xml");
/**
*使用上下文对象根据标识catFactor创造并实例化Bean,并调用方法
*/
Animal cat=context.getBean("catFactor",Animal.class);
cat.sayHello();
/**
*使用上下文对象根据标识dogFactor创造并实例化Bean,并调用方法
*/
Animal dog=context.getBean("dogFactor",Animal.class);
dog.sayHello();
}
}
注意事项
工厂类须要提供非静态方法生成实例Bean(实现特定接口),返回值为接口类型。
因须要使用实例工厂建立实例Bean,需在配置文件中声明实例工厂Bean
依赖注入参数(constructor-arg)为静态工厂返回接口类型静态方法依赖的参数
<property name=”xxx” ref(value)=”xxx”/>
其中name为依赖属性名称
Ref为引用Bean标识(id)
Value为实际依赖值
本章主要对IoC,DI概念进行了介绍,使用简单示例说明了依赖注入的使用方式方法
经过Spring容器获取实例Bean相关:
同一配置文件与不一样配置文件的状况下
下一节将对Spring AOP和Aspects相关内容进行学习