您的位置:首页 > 编程语言 > Java开发

Spring自动检测并装配bean

2017-01-23 16:43 417 查看

自动化装配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)运行结果:

jack
mike


使用场景:上面我们只是演示了第一种,因为有些时候我们是没有源码或者一个接口派生n多实现类的情况,这种场景使用这个最为合适。

3.过滤器context:exclude-filter

这个过滤器与上一节过滤器作用相反,这个过滤器主要用于过滤掉不装配的bean.下面通过一个例子来说明。

针对第二节的例子增加一些类:MyComponent类

package spring.ch2.topic7.filter;

import org.springframework.stereotype.Component;

/**
* Created by louyuting on 17/1/23.
*/
@Component
public 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