Bean的自动装配--NO模式
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系. 在xml配置文件中,autowire一共有五种类型,可以在<bean/>元素中使用autowire属性来指定装配模式,自动装配可以减少配置的数量。
Autowire=’defalut’
autowire:在每个bean中都一个autowire=default的默认配置它的含义是:
采用beans和跟标签中的default-autowire="属性值"一样的设置。
配置文件中的代码
<?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-autowire="no">
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" autowire="default " />
</beans>
红色字体含义:默认装配模式为no模式即EmpServiceImpl类不使用自动装配
第一种:
No模式:
Autoeire=’no’
不使用自动装配。必须通过ref元素指定依赖,这是默认设置。由于显式指定协作者可以使配置更灵活、更清晰,因此对于较大的部署配置,推荐采用该设置。而且在某种程度上,它也是系统架构的一种文档形式。
案例:
<bean id="teacherServiceImpl" class="cn.csdn.service.TeacherServiceImpl"
scope="singleton" autowire="no">
<property name="studentServiceImpl" ref="studentServiceImpl"></property>
</bean>
<bean id="studentServiceImpl" class="cn.csdn.service.StudentServiceImpl"
scope="singleton" />
Student类
public class StudentServiceImpl {
public StudentServiceImpl() {
System.out.println("studnetService初始化完成...");
}
}
Teacher类
public class TeacherServiceImpl {
private StudentServiceImpl studentServiceImpl;
public TeacherServiceImpl() {
System.out.println("teacherservice初始化完毕...");
}
public void setStudentServiceImpl(StudentServiceImpl studentServiceImpl) {
this.studentServiceImpl = studentServiceImpl;
}
public void insert(){
System.out.println("测试no解析。。。。");
}
}
Spring NO模式自动装配
本文介绍Spring框架中NO模式自动装配的基本概念与使用方法。详细解释了如何在XML配置文件中设置autowire属性,并通过具体示例展示了不使用自动装配的情况及其实现方式。
2234

被折叠的 条评论
为什么被折叠?



