Spring 中Bean的自动装配六种模式其五
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。autowire一共有六种类型。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥![2] 在xml配置文件中,可以在<bean/>元素中使用autowire属性指定:
模式
|
说明
|
Antodetect
|
通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。
|
下来我们就用案例来证明一下:准备3个类:
public class AddressServiceImpl {
/**住址*/
private String address;
public void setAddress(String address){
this.address=address;
}
}
public class HomeAddressServiceImpl extends AddressServiceImpl {
private String address;
public void setAddress(String address){
this.address=address;
}
public HomeAddressServiceImpl() {
super();
}
public HomeAddressServiceImpl(String address){
this.address=address;
}
}
public class EmpServiceImpl {
/**公司地址*/
private AddressServiceImpl companyAddress;
public void setCompanyAddress(AddressServiceImpl companyAddress){
this.companyAddress=companyAddress;
}
}
Antodetect值的antodetect.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.0.xsd">
<!-- 配置bean -->
<bean id="addressServiceImpl" class="cn.csdn.service.AddressServiceImpl" scope="singleton">
<property name="address">
<value>北京</value>
</property>
</bean>
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton" autowire="autodetect"/>
</beans>
测试类:(junit测试)
public class App {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:byName.xml"); EmpServiceImpl emp = (EmpServiceImpl) ac.getBean("empServiceImpl");
}
}
结束语:这几期都是为大家提供自动装配Bean的属性值,为学习Spring打下了良好的基础还只的遗体的就是,*可以设置bean使自动装配失效:
采用xml格式配置bean时,将<bean/>元素的autowire-candidate属性设置为false,这样容器在查找自动装配对象时,将不考虑该bean,即它不会被考虑作为其它bean自动装配的候选者,但是该bean本身还是可以使用自动装配来注入其它bean的。
<!--EndFragment-->
分享到:
相关推荐
ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 <bean id="xxx" class="xxx" ></bean> (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 <bean...
众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...
主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。 自动装配(autowiring):Spring自动满足bean之间的依赖。 1 使用@Component定义bean 在类声明的前面使用@Component对类进行标注,这...
博客地址:https://blog.csdn.net/u010476739/article/details/76732201 spring装配bean的方式实例
NULL 博文链接:https://364232252.iteye.com/blog/2369853
目的:Spring容器已经成功获取了UserController实例,并通过调用实例中的方法执行了各层中的输出语句。 运行结果为: User [id=1, name=张三, password=123] userDao say hello world! UserService say hello world ...
spring中的自动装配的形式byType、byName、no、construtor、autodetect和default(beans标签中没有,而bean标签中有该属性)共6种装配的方式的举例说明
本篇文章主要介绍了浅谈Spring装配Bean之组件扫描和自动装配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring会在上下文中自动寻找,并自动给bean装配属性! 下面我们来做以下实验 建立如下测试环境:一个人一只猫一只狗 public class Dog { public void getshout(){ System.out.println("狗叫了三声"); } } public...
本篇文章主要介绍了Spring学习之Bean的装配三种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了 Spring的自动装配Bean的三种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
今天小编就为大家分享一篇关于在Spring中自动装配Bean的属性,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
主要介绍了spring在IoC容器中装配Bean详解,具有一定借鉴价值,需要的朋友可以参考下
请解释Spring Bean的自动装配 请解释自动装配模式的区别 如何开启基于注解的自动装配 请举例解释Required annotation 请举例解释Autowired注解 请举例说明Qualifier注解 构造方法注入和设值注入有什么区别 Spring...
Spring Boot技术知识点:Bean装配1
主要介绍了spring装配bean方法实例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Spring自动装配Bean实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring 自动装配及其注解 博客:https://blog.csdn.net/u010476739/article/details/76735241
1. Spring 介绍 1.1 Spring 的优点 1.2 Spring 的缺点 1.3 详细讲解一下核心容器(spring context应用上下文) 模块 ...Spring的自动装配 Spring框架中都用到了哪些设计模式? Spring框架中有哪些不同类型的事件?