`
DXL_xiaoli
  • 浏览: 69601 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

体验spring(四)---Bean的自动装配1

阅读更多

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框架---IOC装配Bean

    本篇文章主要介绍了详解Spring框架---IOC装配Bean,提供了三种方式实例化Bean,具有一定的参考价值,有兴趣的可以了解一下。

    spring自动装配例子

    ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 &lt;bean id="xxx" class="xxx" &gt;&lt;/bean&gt; (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 &lt;bean...

    基于框架的Web开发-装配Bean自动装配.doc

    自动装配(autowiring):Spring自动满足bean之间的依赖。 1 使用@Component定义bean 在类声明的前面使用@Component对类进行标注,这个类可以被spring容器识别,spring容器将类转换为容器管理的bean。 项目分层之后...

    spring-framework-5.0.4.RELEASEspring-framework-5.0.4.RELEASE

    Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃...它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transaction Management

    spring入门学习-3、Bean装配(XML).pdf

    spring入门学习-3、Bean装配(XML).pdf

    spring-aop-4.0.0.RELEASE

    spring-aop-4.0.0.RELEASE工具类,用于spring注解装配bean

    完整Spring框架,包含源码文档等 spring-5.2.9.RELEASE-dist.zip

    springframework 是sping 里面的一个开源框架...它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transaction Management,等等......

    Spring Boot技术知识点:Bean装配1

    Spring Boot技术知识点:Bean装配1

    springIOC核心组件分析.vsdx

    spring-beans:Bean工厂与装配 spring-context:上下文,即IOC容器 spring-context-support:对IOC的扩展,以及IOC子容器 spring-context-indexer:类管理组件和Classpath扫描 spring-expression:表达式语句 切面编程: ...

    spring入门教程之bean的继承与自动装配详解

    众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...

    spring装配bean实例代码

    博客地址:https://blog.csdn.net/u010476739/article/details/76732201 spring装配bean的方式实例

    spring-beans.zip

    它们被Spring IOC容器初始化,装配,和管理。这些beans通过容器中配置的元数据创建。比如,以XML文件中&lt;bean/&gt; 的形式定义。Spring 框架定义的beans都是单件beans。在bean tag中有个属性”singleton”,如果它被赋为...

    Spring中的Bean的管理_Bean的装配方式_基于注解的装配_项目

    目的:Spring容器已经成功获取了UserController实例,并通过调用实例中的方法执行了各层中的输出语句。 运行结果为: User [id=1, name=张三, password=123] userDao say hello world! UserService say hello world ...

    day38 16-Spring的Bean的装配:注解的方式

    NULL 博文链接:https://364232252.iteye.com/blog/2369853

    spring装配bean的3种方式总结

    主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    Spring基于@Conditional条件化装配bean

    主要介绍了Spring @Conditional条件化装配bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    浅谈Spring装配Bean之组件扫描和自动装配

    本篇文章主要介绍了浅谈Spring装配Bean之组件扫描和自动装配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring自动装配Bean实现过程详解

    主要介绍了Spring自动装配Bean实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    spring在IoC容器中装配Bean详解

    主要介绍了spring在IoC容器中装配Bean详解,具有一定借鉴价值,需要的朋友可以参考下

    JSP Spring 自动化装配Bean实例详解

    主要介绍了JSP Spring 自动化装配Bean实例详解的相关资料,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics