Spring框架的控制反转 (IOC)和依赖注入以及配置
2018-03-09 16:34
405 查看
1.Spring的控制反转(IOC):
传统的JAVA开发,比如你需要对象A,对象B和对象C,需要我们创建被调用对象,耦合度很高,当需要修改A的属性时,可能还要修改对象C里面关于A的属性,两个对象过于耦合,不利于变化和拓展。在spring中,直接操控的对象的调用权交给容器,通过容器来实现对象组件的装配和管理,从而实现对象之间的松耦合。所谓的“控制反转”概念就是对组件对象控制权的转移,从程序代码本身转移到了外部容器。
2.Spring的依赖注入:
举个例子,类A拥有name,id属性,类C拥有name,id和对象A属性,初始化A对象和C对象时,需要我们在程序代码中调用各个属性的set方法,甚是麻烦。在Spring中,对象无需自行创建或管理它们的依赖关系,IoC容器在运行期间,动态地将某种依赖关系注入到对象之中。依赖注入能让相互协作的软件组件保持松散耦合。
引用一个例子:http://how2j.cn/k/spring/spring-injection/88.html#nowhere
3.Spring的xml配置
首先准备两个pojo类
package com.how2java.pojo;
public class Category {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
} package com.how2java.pojo;
public class Product {
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
在src目录下新建applicationContext.xml文件,applicationContext.xml是Spring的核心配置文件<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--创建Category类-->
<!--将字符串category2注入name属性-->
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 2" />
</bean>
<!--将对象category注入Product的category属性-->
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
</beans>4.Spring的注解配置
假设类Apple和类Pear要注入Fruit类,可以使用@Autowired注释
假设已经创建了类Apple和类Pear以及类Fruit的bean
public class Fruit {
@Autowired
private Apple apple;
@Autowired
private Pear pear;
} 除此之外,还可以将@Autowired写在Fruit的SetApple和SetPear方法上,或者写在Fruit的构造方法上,这里不再赘述。
如果容器中还没创建或者没有类型匹配的bean的话,则使用@Autowired(required=false)
如果容器中配置了两个或者多个类型为Pear类的bean,当对Fruit的Pear成员变量进行自动注入时,可以使用@Qualifier注释指定注入bean的名称(假设一个叫pear1,一个叫pear2)
public class Fruit {
@Autowired
private Apple apple;
@Autowired
@Qualifier("pear1")
private Pear pear;
}
除@Autowired之外,还有@Resource注解,@Autowired是按byType自动注入的,@Resource是按byName自动注入的public class Fruit {
@Resource
private Apple apple;
@Resource(name = "pear1")
private Pear pear;
}在配置文件上写上<context:annotation-config/>表示告诉Spring要用注解的方法进行配置<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 2" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<!-- <property name="category" ref="c" /> -->
</bean>
</beans>
在类定义处,使用@Component注解就可以将一个类定义成Spring容器中的bean,@Component有一个可选的入参,用于指定bean的名称@Component("fruit")
public class Fruit {
@Resource
private Apple apple;
@Resource(name = "pear1")
private Pear pear;
}
在配置文件中写入<context:component-scan base-package="com.mypackage.pojo/">,其作用是告诉Spring,bean都放在com.mypackage.pojo这个包下。<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org
4000
/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mypackage.pojo"/>
</beans>
传统的JAVA开发,比如你需要对象A,对象B和对象C,需要我们创建被调用对象,耦合度很高,当需要修改A的属性时,可能还要修改对象C里面关于A的属性,两个对象过于耦合,不利于变化和拓展。在spring中,直接操控的对象的调用权交给容器,通过容器来实现对象组件的装配和管理,从而实现对象之间的松耦合。所谓的“控制反转”概念就是对组件对象控制权的转移,从程序代码本身转移到了外部容器。
2.Spring的依赖注入:
举个例子,类A拥有name,id属性,类C拥有name,id和对象A属性,初始化A对象和C对象时,需要我们在程序代码中调用各个属性的set方法,甚是麻烦。在Spring中,对象无需自行创建或管理它们的依赖关系,IoC容器在运行期间,动态地将某种依赖关系注入到对象之中。依赖注入能让相互协作的软件组件保持松散耦合。
引用一个例子:http://how2j.cn/k/spring/spring-injection/88.html#nowhere
3.Spring的xml配置
首先准备两个pojo类
package com.how2java.pojo;
public class Category {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
} package com.how2java.pojo;
public class Product {
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
在src目录下新建applicationContext.xml文件,applicationContext.xml是Spring的核心配置文件<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--创建Category类-->
<!--将字符串category2注入name属性-->
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 2" />
</bean>
<!--将对象category注入Product的category属性-->
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
</beans>4.Spring的注解配置
假设类Apple和类Pear要注入Fruit类,可以使用@Autowired注释
假设已经创建了类Apple和类Pear以及类Fruit的bean
public class Fruit {
@Autowired
private Apple apple;
@Autowired
private Pear pear;
} 除此之外,还可以将@Autowired写在Fruit的SetApple和SetPear方法上,或者写在Fruit的构造方法上,这里不再赘述。
如果容器中还没创建或者没有类型匹配的bean的话,则使用@Autowired(required=false)
如果容器中配置了两个或者多个类型为Pear类的bean,当对Fruit的Pear成员变量进行自动注入时,可以使用@Qualifier注释指定注入bean的名称(假设一个叫pear1,一个叫pear2)
public class Fruit {
@Autowired
private Apple apple;
@Autowired
@Qualifier("pear1")
private Pear pear;
}
除@Autowired之外,还有@Resource注解,@Autowired是按byType自动注入的,@Resource是按byName自动注入的public class Fruit {
@Resource
private Apple apple;
@Resource(name = "pear1")
private Pear pear;
}在配置文件上写上<context:annotation-config/>表示告诉Spring要用注解的方法进行配置<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 2" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<!-- <property name="category" ref="c" /> -->
</bean>
</beans>
在类定义处,使用@Component注解就可以将一个类定义成Spring容器中的bean,@Component有一个可选的入参,用于指定bean的名称@Component("fruit")
public class Fruit {
@Resource
private Apple apple;
@Resource(name = "pear1")
private Pear pear;
}
在配置文件中写入<context:component-scan base-package="com.mypackage.pojo/">,其作用是告诉Spring,bean都放在com.mypackage.pojo这个包下。<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org
4000
/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mypackage.pojo"/>
</beans>
相关文章推荐
- Spring.net 控制反转(IOC)依赖注入(DI)的使用 以及Config文件的配置
- Spring配置过程(一)反转控制/依赖注入IOC
- 大数据WEB阶段Spring框架(一)IOC控制反转、DI注入依赖
- Spring学习(1):控制反转(IoC)和依赖注入(DI)的详解以及注解(annotation)开发入门案例
- Spring框架中IOC控制反转和DI依赖注入区别
- Spring学习(1):控制反转(IoC)和依赖注入(DI)的详解以及注解(annotation)开发入门案例
- 控制反转(IoC)以及依赖注入(DI)的解释
- spring IOC(控制反转)和DI(依赖注入)以及三种依赖注入方式的比较
- 码农小汪-spring框架学习之2-spring IoC and Beans 控制反转 依赖注入 ApplicationContext BeanFactory
- Spring框架中IOC控制反转和DI依赖注入区别
- Spring框架中IOC控制反转和DI依赖注入区别
- 码农小汪-spring框架学习之2-spring IoC and Beans 控制反转 依赖注入 ApplicationContext BeanFactory
- spring框架的控制反转(IoC)和注入依赖(DI)
- Spring学习,依赖注入(DI)、控制反转(IoC)和面向切面(AOP)
- IoC模式(依赖、依赖倒置、依赖注入、控制反转)
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- 控制反转(IoC)与依赖注入(DI)
- IoC模式(依赖、依赖倒置、依赖注入、控制反转)
- IOC控制反转 & DI依赖注入 & Service Locator服务定位器
- Spring.Net-DI依赖注入和Ioc控制反转