1.定义一个CompanyService.java
package cn.csdn.service;
public class CompanyService {
public CompanyService() {
System.out.println("------初始化companyService--------");
}
}
2.定义一个EmpService.java
package cn.csdn.service;
public class EmpService {
/*员工的名称*/
private String name;
/*公司对象*/
private CompanyService companyService;
public EmpService() {
System.out.println("-------初始化EmpService-----------");
}
public EmpService(String name, CompanyService companyService) {
super();
this.name = name;
this.companyService = companyService;
System.out.println(".......构造器完成的效果.....");
}
/* setXxx注入的方法 xxx简单类型*/
public void setName(String name) {
this.name = name;
System.out.println("-------set注入name成功!------");
}
/*setXxx注入的方式 xxx是一个bean对象 companyService*/
public void setCompanyService(CompanyService companyService) {
this.companyService = companyService;
System.out.println("----set注入companyService成功!-----");
}
/*setXxx xxx属性*/
}
/*
* 依赖检查:
* 在一个类中提供setXxx xxx属性,
* 此属性是否在在配置文件中进行配置的处理就称为:依赖检查
* none:不检查
* 就是如果一个类中有setXxx属性,xxx属性可以在配置文件的bean进行
* 配置,也可以不配置,程序无误。
* simple:执行的是对于原始类型及集合(除协作者外的一切东西)执行依赖检查
*
* objects:仅对协作者执行依赖检查
* 协作者 bean 与bean 关联系
*
* all:对协作者,原始类型及集合执行依赖检查
*
*/
3.配置文件 autowire.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-2.5.xsd"
default-dependency-check="none" default-autowire="no">
<!-- id=companyService -->
<!-- 员工的bean -->
<bean id="empService" class="cn.csdn.service.EmpService" scope="singleton"
dependency-check="objects" autowire="autodetect">
<property name="name" value="redarmy"/>
</bean>
<!-- 公司bean -->
<bean id="comService" class="cn.csdn.service.CompanyService"
scope="singleton"></bean>
</beans>
4.配置文件 applicationContext.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-2.5.xsd"
default-dependency-check="none" default-autowire="no">
<!-- 员工的bean -->
<bean id="empService" class="cn.csdn.service.EmpService" scope="singleton"
dependency-check="all">
<!--当dependency-check="simple" 必须为name加 property配置 -->
<property name="name" value="redarmy" />
<!-- 当dependency-check="objects" 必须为companyService加property配置 -->
<property name="companyService" ref="companyService"/>
</bean>
<!-- 公司bean -->
<bean id="companyService" class="cn.csdn.service.CompanyService"
scope="singleton"></bean>
</beans>
5.测试类 App.java
package cn.csdn.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
@Test
public void app() {
/* 初始化容器 */
ApplicationContext ac = new
ClassPathXmlApplicationContext("classpath:autowire.xml");
}
}
/*
* 初始化容器
* 搜索容器中scope="singleton"的bean,如果该bean没有实现延迟初始化,那么就
* 实例化该bean
* --------------执行-bean---构造器初始化bean
* --------------执行该bean中的属性注入的方法 set注入
* */
知识点讲解:
自动装配:Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系
针对的对象是:bean 是spring容器中配置的bean
对原始类型 String 不能够使用自动装配。
spring的配置文件中的beans标签中有一个 default-autowire 其默认值是"no"
在配置文件中的所有bean都默认采用此装配方式
no: 不自动装配、
采用ref="引入协作者"进行装配
byName:根据属性名自动装配。
此选项将检查容器并根据(xxx)名字查找与属性(setXxx xxx的名字)完全一致的bean,并将其与属性自动装配。
例如,在bean定义中将autowire设置为byName,而该bean包含master属性
(同时提供setMaster(..)方法),Spring就会查找名为master的bean定义,
并用它来装配给master属性。
byType:如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。
如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。
若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。
如果你不希望这样,那么可以通过设置dependency-check="objects"
让Spring抛出异常。
constructor:与byType的方式类似,不同之处在于它应用于构造器参数。
如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
autodetect:通过bean类的自省机制(introspection)
来决定是使用constructor还是byType方式进行自动装配。
如果发现默认的构造器,那么将使用byType方式。