Spring从两个角度实现自动化装配:
1、组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean
2、自动装配(autowiring):Spring自动满足bean之间的依赖
组件扫面和自动装配组合在一起能发挥强大的威力。
准备几个bean:
CompactDisc接口:
package spring.component; /** * @ClassName CompactDisc * @Date 2016年12月27日 下午3:26:21 */ public interface CompactDisc { void paly(); }
SgtPepers实现类:
package spring.component.impl; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import spring.component.CompactDisc; /** * @ClassName SgtPepers * @Date 2016年12月27日 下午3:30:04 */ @Component public class SgtPepers implements CompactDisc{ private String title = "Sgt. Pepper's Lonely Hearts Club Band"; private String artist = "The Beatles"; public void paly() { System.out.println("palying " + title + " by " + artist); } }
Spring组件扫描默认是不启用的。
所以显示配置一下Spring,从而命令它去寻找带有@Component注解类,并为其创建bean(有两种方法:1、xml配置;2、java配置【本人优先选这种】)
package spring.component.javaConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import spring.component.CompactDisc; import spring.component.impl.CDPlayer; import spring.component.impl.SgtPepers; /** * @ClassName ComponentScan * @Date 2016年12月27日 下午3:37:38 */ @Configuration @ComponentScan("spring.component") public class CDplayerConfig{ } }
如果扫描多个包使用@ComponentScan(basePackages={"spring.component","..."}) or @ComponentScan(basePackageClasses={....class ,....class})
Junit测试:
package spring.component; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import spring.component.javaConfig.CDplayerConfig; /** * @ClassName ComponentTest * @Date 2016年12月27日 下午4:03:28 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=CDplayerConfig.class) public class CDPlayerTest { @Autowired private CompactDisc cd; private CompactDisc cd2; @Test public void cdNotNull(){ assertNotNull(cd); } }
其中:@Named(java依赖注入规范)可以作为@Component注解的替代方案(有差异)
@Autowired 自动装配
不仅可以使用在构造器上
@Autowired public CDPlayer(CompactDisc cd){ this.cd = cd; }
还能用在属性的Setter方法上
@Autowired public void setCd(CompactDisc cd) { this.cd = cd; }
实际Setter方法并没有特殊之处,@Autowired可以用在类的任何方法上
@Autowired public void insertDisc(){ cd.paly(); }
@Autowired 的required属性设置为false,spring会尝试执行自动装配,但是如果没有匹配的bean,spring将会让这个bean处于未装配的状态(可能会出现NullPointerExcepiton)
其中:@Inject(java依赖注入规范)可以作为@Autowired注解的替代方案(有差异)
相关推荐
Spring Boot技术知识点:Bean装配1
ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 <bean id="xxx" class="xxx" ></bean> (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 <bean...
1.4 装配Bean-自动装配(重要!) Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。 自动装配(autowiring):Spring自动满足bean之间的依赖。 1 ...
众所周知Spring里面的bean就类似是定义的一个组件,而这个组件的作用就是实现某个功能的,下面这篇文章主要给大家介绍了关于spring入门教程之bean继承与自动装配的相关资料,需要的朋友可以参考借鉴,下面随着小编来...
主要介绍了spring在IoC容器中装配Bean详解,具有一定借鉴价值,需要的朋友可以参考下
主要介绍了Spring自动装配Bean实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring入门学习-3、Bean装配(XML).pdf
spring-beans:Bean工厂与装配 spring-context:上下文,即IOC容器 spring-context-support:对IOC的扩展,以及IOC子容器 spring-context-indexer:类管理组件和Classpath扫描 spring-expression:表达式语句 切面编程: ...
博客地址:https://blog.csdn.net/u010476739/article/details/76732201 spring装配bean的方式实例
主要介绍了spring boot中的条件装配bean的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
今天小编就为大家分享一篇关于在Spring中自动装配Bean的属性,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
主要介绍了Spring @Conditional条件化装配bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本篇文章主要介绍了 Spring的自动装配Bean的三种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了浅谈Spring装配Bean之组件扫描和自动装配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了Spring学习之Bean的装配三种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了JSP Spring 自动化装配Bean实例详解的相关资料,需要的朋友可以参考下
spring基础,主要讲解了 spring的autowire:自动装配 collections:属性配置的细节 properties:外部属性文件的使用 relati:bean之间的关系 scope:bean的作用域 spel:spel的使用
NULL 博文链接:https://364232252.iteye.com/blog/2369853
创建Spring配置文件,配置Bean; 创建测试类,测试程序。 @Repository("userDao") public class UserDaoImpl implements UserDao{ public void save(){ System.out.println("userdao...save..."); } } public class...