Spring自动检测并装配bean
来源:互联网 发布:python定义json字符串 编辑:程序博客网 时间:2024/04/30 02:55
自动化装配bean
Spring从两个角度来实现自动化装配:
1. 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean。
2. 自动装配(autowiring):spring自动满足bean之间的依赖。
组件扫描和自动化装配组合在一起就能发挥出强大作用,能做到将显示配置做到最少。
1.自动扫描bean
(1)entity
厨师类:
package spring.ch2.topic7;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/** * Created by louyuting on 17/1/22. */@Component("jack")public class Chief { @Value("jack") private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; }}
上面的@Component 注解后面接的属性值就是制定的bean的id值,如果不加,bean的id值就是默认的首字母小写的类名。
其实还有其他标签可以作为bean的标注:
@Component-作为构件来配置bean
@Repository-标识数据仓库
@Service-标识服务
@Controller-标识MVC的控制器
但是上面的几个标签的作用大同小异,都是标注有这么一个bean。
(2)配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="spring.ch2.topic7" /></beans>
配置文件里面很简单,值添加了context:component-scan 标签,里面base-package指定了扫描组件的包。
(3)测试类:
package spring.ch2.topic7;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Created by louyuting on 17/1/20. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"/spring/ch2/topic7/ApplicationContext-test.xml"})public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief(){ Chief jack = (Chief)applicationContext.getBean("jack"); System.out.println(jack.getName()); }}
(4)运行结果:
jack
说明成功注入了bean,并获取到了对象。
2.过滤器 context:include-filter
这一节我们来讨论一下过滤器的使用。
(1)entity
Person接口
package spring.ch2.topic7.filter;/** * Created by louyuting on 17/1/23. */public interface Person {}
拳击手类:
package spring.ch2.topic7.filter;import org.springframework.beans.factory.annotation.Value;/** * Created by louyuting on 17/1/23. */public class Fighter implements Person { @Value("mike") private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; }}
厨师类:
package spring.ch2.topic7.filter;import org.springframework.beans.factory.annotation.Value;/** * Created by louyuting on 17/1/23. */public class Chief implements Person{ @Value("jack") private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; }}
在上面的类里面我们没有使用任何标记bean的注解,而且我们再配置文件里面也不会显示标记bean。
(2)配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="spring.ch2.topic7" > <context:include-filter type="assignable" expression="spring.ch2.topic7.filter.Person"/> </context:component-scan></beans>
在配置文件里面:
(1)我们配置自动扫描 context:component-scan
(2)在自动扫描里面我们在配置context:include-filter,然后type=”assignable”,这里的意思是,Person接口所派生出来的所有类,都自动注册bean。
type的四种参数:
assignable -指定扫描某个接口派生出来的类;
annotation -指定扫描使用某个注解的类;
aspectj -指定扫描AspectJ表达式相匹配的类;
custom -指定扫描自定义的实现了org.springframework.core.type.filter.TypeFilter接口的类;
regex-指定扫描符合正则表达式的类。
(3)测试类:
package spring.ch2.topic7.filter;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Created by louyuting on 17/1/20. * 注入List和Set */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"/spring/ch2/topic7/filter/ApplicationContext-test.xml"})public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief(){ Chief jack = (Chief)applicationContext.getBean("chief"); System.out.println(jack.getName()); Fighter mike = (Fighter)applicationContext.getBean("fighter"); System.out.println(mike.getName()); }}
(4)运行结果:
jackmike
使用场景:上面我们只是演示了第一种,因为有些时候我们是没有源码或者一个接口派生n多实现类的情况,这种场景使用这个最为合适。
3.过滤器context:exclude-filter
这个过滤器与上一节过滤器作用相反,这个过滤器主要用于过滤掉不装配的bean.下面通过一个例子来说明。
针对第二节的例子增加一些类:MyComponent类
package spring.ch2.topic7.filter;import org.springframework.stereotype.Component;/** * Created by louyuting on 17/1/23. */@Componentpublic class MyComponent implements Person {}
在这个类里面我们标记了@Component注解,一会儿我们会使用过滤器排除这个注解的所有bean
配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="spring.ch2.topic7" > <context:include-filter type="assignable" expression="spring.ch2.topic7.filter.Person"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component" /> </context:component-scan></beans>
在配置文件里面:
(1)我们配置自动扫描context:component-scan
(2)在自动扫描里面我们在配置context:include-filter,然后type=”assignable”,这里的意思是,Person接口所派生出来的所有类,都自动注册bean
(3)还配置了context:exclude-filter,所有被注解@Component标记的类我们都不自动注册。
测试类:
package spring.ch2.topic7.filter;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Created by louyuting on 17/1/20. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"/spring/ch2/topic7/filter/ApplicationContext-test.xml"})public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief(){ Chief jack = (Chief)applicationContext.getBean("chief"); System.out.println(jack.getName()); Fighter mike = (Fighter)applicationContext.getBean("fighter"); System.out.println(mike.getName()); System.out.println(applicationContext.containsBean("myComponent")); }}
运行结果: `
jack
mike
false
上面所有代码的github地址。
- Spring自动检测并装配bean
- spring的自动装配Bean与自动检测Bean
- spring的自动装配Bean与自动检测Bean
- Spring杂记之Bean自动检测与自动装配
- Spring自动注解装配、自动检测装配Bean配合过滤组件使用
- spring 自动检测bean
- Spring自动检测Bean
- spring(自动检测bean)
- Spring自动检测Bean
- Spring Bean装配-自动装配
- 【Spring学习16】自动检测Bean
- spring 基本Bean装配
- Spring Bean 初级装配
- Spring Bean 高级装配
- Spring中装配bean
- Spring bean装配
- spring装配bean
- Spring笔记---装配Bean
- 南邮离散数学实验三-偏序关系中盖住关系的求取及格论中有补格的判定
- 2016 总结记
- 关于mysql中由于类型转换导致索引无法使用的问题
- LeetCode 231. Power of Two
- CodeForces 59AWord 水题
- Spring自动检测并装配bean
- 远程调用的几种方式
- 【PAT】1004. Counting Leaves (30)
- 在linux环境中编译JNI的文件为.so包
- 积分
- CodeForces 59BFortune Telling
- Android嵌套滑动控件的冲突解决和ViewPager适配当前子控件高度不留空白的办法
- FreeRTOS之配置详解、裁剪(FreeRTOSConfig.h)
- 谈谈spring 事务同步 transactionSynchronization 的作用