Spring4 Bean管理的注解实现

日期: 2016-7-16


内容: 学习Spring Bean管理的注解实现;



主要内容:

1、ClassPath扫描与组件管理;


2、类的自动检测与注册Bean;

Spring可以自动检测类并注册Bean到ApplicationContext中

@Service
public class SimpleMovieLister{
    private MovieFinder movieFinder;

    @Autowired
    publicc SimpleMovieLister(MovieFinder movieFinder)
{
    this.movieFinder = movieFinder;
}

}


3、<context:annotation-config/>;

通过在基于XML的Spring陪住如下标签(请添加上下文的命名空间);

<context:annotation-config/>仅会查找在同一个applicationContext中的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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"    
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    
    xmlns:cache="http://www.springframework.org/schema/cache"    
    xsi:schemaLocation="    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context.xsd    
    http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx.xsd    
    http://www.springframework.org/schema/jdbc    
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    
    http://www.springframework.org/schema/cache    
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop.xsd    
    http://www.springframework.org/schema/util    
    http://www.springframework.org/schema/util/spring-util.xsd">
        
        <!-- 添加Spring类注解标签 -->
        <context:annotation-config></context:annotation-config>
        <bean id="injectionService" class="com.test.service.InjectionServiceImpl">
        	<property name="injectionDAO" ref="injectionDAO"></property>
        </bean>
        
        <bean id="injectionDAO" class="com.test.dao.InjectionDAOImpl"></bean>
    </beans>   
为了

让Spring能够检测到这些类并添加注册相应的Bean需要添加如下的内容

<!-- 为了让Spring能够检测并注册相应的Bean需要添加如下的内容 -->
        <context:component-scan base-package="org.example.hello(添加包,也可以使用正则表达式)"></context:component-scan>


但是在通常的情况下:<context:component-scan base-package="org.example.hello(添加包,也可以使用正则表达式)"></context:component-scan>

包含了<context:annotation-config></context:annotation-config>,所以通常在使用了前者时候就不再使用后者了。

AutowiredAnnotationbeanPostProcessor和CommonAnnotationbeanPostProcessor也会被包含进来。


其实意思是这样的,在applicationContext的配置文件中添加了:<context:component-scan base-package="org.example.hello(添加包,也可以使用正则表达式)">配置

之后,在启动Spring IOC容器的时候,就会自动去扫描指定的包上的注解,在扫描到符合Bean配置的类的时候就会将其添加在Spring

的配置文件中并将其注册(实例化)。


使用过滤器进行自定义扫描:

默认情况下,类被自动发现并注册bean的条件是: 使用@Component,@RePository,@Service,@Controller注解或者使用@Component的自定义注解


可以通过过滤器修改上面的行为,如:下面的例子的XML配置忽略所有的有@Repository注解并用“”Stub“”代替。

还可以使用use-default-filters="false"禁用自动发现并注册。
<!-- 为了让Spring能够检测并注册相应的Bean需要添加如下的内容 -->
        <context:component-scan base-package="org.example.hello(添加包,也可以使用正则表达式)">
        	<!-- 使用过滤器,过滤掉一些不需要注册Bean的类 -->
        	<context:exclude-filter type="regex" expression=""/> 使用正则表达式除去不需要的类 -->
        	
        	<!--<context:include-filter type="regex" expression=""/>
        </context:component-scan>

其中过滤的方式有:

Bean的定义:
在扫描过程中组件被自动检测,那么Bean名称是由BeanNameGenerator生成的(@Component,@Repository,@Service
@Controller都有一个name属性用于显示设置Bean Name):
@Service,@Repository实例:
@Service(""helloLUOt)
public class Test1
{
    //add somethings
}

@Repository("helloWOrld")
{
    //add somethings
}

你还可以定义Bean的命名策略,实现BeanNameGenerator接口,并一定要包含一个无参数的构造函数。

假如没有指定的话,Spring IOC会根据BeanNameGenerator的BenaName生成策略去生成Bean的对应Name。通常的生成策略是将
这个Bean的首字母小写的这样一个名字。

如下添加一个实现BeanNamegenerator接口的实现Bean的命名策略:

实现BeanNameGenerator接口,编写bean的命名规则:
package com.test.implBeanNameGenerator;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;

public class GeneratorBeanName implements BeanNameGenerator {

	@Override
	public String generateBeanName(BeanDefinition arg0,
			BeanDefinitionRegistry arg1) {
		
		return null;
	}

}

在配置文件中添加配置:
        <!-- 添加beanName的自定义 -->
        <context:component-scan base-package="com.test.implBeanNameGenerator" name-generator="com.test.implBeanNameGenerator.GeneratorBeanName">
        	
        </context:component-scan>


作用域(Scope):

通常情况下自动查找的Spring组件,其scope是singleton的,Spring2.5提供了一个标志scope的注解 @Scope

@Repository
@Scope("prototype")
public class HelloWorld{
    //do sometingh
}
也可以自定义scope策略,实现ScopeMetadataResolver接口并提供一个无参数的构造
器。
     

代理方式:

可以使用scoped-proxy属性指定代理,有三个值可选:no,interfaces,targetClass.

<!-- 使用interfaces代理 -->
        <context:component-scan base-package="org.demo" scoped-proxy="interfaces"></context:component-scan>
        <!-- 不使用代理:no -->
        <context:component-scan base-package="org.demo" scoped-proxy="no"></context:component-scan>
        <!-- 使用targetClass方式代理 -->
        <context:component-scan base-package="org.demo" scoped-proxy="targetClass"></context:component-scan>


添加测试实例:
package com.test.annotation;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service("bean")
@Component(""beanname)//这个注解不OK
public class TestBeanAnnotation {
	
	public void say(String word)
	{
		System.out.println("BeanAnnotation: "+word);
	}
}


XML配置:
<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"    
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    
    xmlns:cache="http://www.springframework.org/schema/cache"    
    xsi:schemaLocation="    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context.xsd    
    http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx.xsd    
    http://www.springframework.org/schema/jdbc    
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    
    http://www.springframework.org/schema/cache    
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop.xsd    
    http://www.springframework.org/schema/util    
    http://www.springframework.org/schema/util/spring-util.xsd">
        
       <!-- 实现bean的扫描和注册 -->
       <context:component-scan base-package="com.test.annotation"></context:component-scan>
    </beans>   


测试类:

	@Test
	public void testSay()
	{
		//加载配置文件
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_BeanAnnotation.xml");
		//获得实例
		TestBeanAnnotation ta = (TestBeanAnnotation)ac.getBean("bean");
		
		ta.say("hello world!");
	}


运行结果:
开始执行: 2016年07月16日 03时:34分:36秒
七月 16, 2016 3:34:36 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 15:34:36 CST 2016]; root of context hierarchy
七月 16, 2016 3:34:37 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext_BeanAnnotation.xml]
BeanAnnotation: hello world!
执行完成: 2016年07月16日 03时:34分:36秒

4、@Component,@Repository,@Service,@Controller

在类中添加这几个注解之后,当Spring IOC容器启动的时候就会去检查并将这些类实例化为Bean提供调用。


5、@Required;

这个注解适用于bean属性的setter方法。这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在bean定义或自动装配一个明确的属性值。


6、@Autowired;

可以将这个注解理解为传统的setter方法,可以用于构造器或者成员变量。默认情况下,如果找不到合适的bean将会导致autowiring失败抛出异常,可以通过下面的方式避免。

在使用类的时候,每个类可能有很多个构造器,但是在使用Autowored的时候每一个类只能有一个构造器被标记为required=true,@Autowired的必要属性,建议使用@Required注解。


测试例子:第一种注入方式,在给属性添加@Autowired注解的情况下。

package com.test.dao;

public interface InjectionDAO {

	public void save(String arg);
}


package com.test.dao;

import org.springframework.stereotype.Repository;

@Repository
public class InjectionDAOImpl implements InjectionDAO {

	@Override
	public void save(String arg) {
		
		System.out.println("保存的数据: "+arg);
	}

}

package com.test.service;

public interface InjectionService {

	public void save(String arg);
}

package com.test.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.dao.InjectionDAO;

@Service
public class InjectionServiceImpl implements InjectionService {

	@Autowired
	InjectionDAO injectionDAO;

	@Override
	public void save(String arg) {

		System.out.println("接收的数据: "+arg);
		//处理数据
		arg = arg+":"+this.hashCode();
		
		//调用InjectionDAO的save方法处理数据
		injectionDAO.save(arg);//--->配置xml配置文件
	}

}


XML配置文件:

<?xml version="1.0" encoding="UTF-8"?>    
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"    
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    
    xmlns:cache="http://www.springframework.org/schema/cache"    
    xsi:schemaLocation="    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context.xsd    
    http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx.xsd    
    http://www.springframework.org/schema/jdbc    
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    
    http://www.springframework.org/schema/cache    
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop.xsd    
    http://www.springframework.org/schema/util    
    http://www.springframework.org/schema/util/spring-util.xsd">
        
        <!-- 配置注解自动扫描 Bean-->
       	<context:component-scan base-package="com.test"></context:component-scan>
    </beans>   

测试方法:

	@Test
	public void testAutowired()
	{
		//加载配置文件
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//获得实例
		InjectionService is = (InjectionService)ac.getBean("injectionServiceImpl");
		
		//调用保存方法保存数据
		is.save("@Autowired");
	}

测试结果:

开始执行: 2016年07月16日 08时:27分:54秒
七月 16, 2016 8:27:55 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 20:27:55 CST 2016]; root of context hierarchy
七月 16, 2016 8:27:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
接收的数据: @Autowired
保存的数据: @Autowired:1848415041
执行完成: 2016年07月16日 08时:27分:54秒

测试例子:第二种注入方式,在给setter方法添加@Autowired注解的情况下。

package com.test.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.dao.InjectionDAO;

@Service
public class InjectionServiceImpl implements InjectionService {

	//@Autowired: 给属性添加注解
	InjectionDAO injectionDAO;

	@Autowired//给setter方法添加注解
	public void setInjectionDAO(InjectionDAO injectionDAO) {
		this.injectionDAO = injectionDAO;
	}

	@Override
	public void save(String arg) {

		System.out.println("接收的数据: "+arg);
		//处理数据
		arg = arg+":"+this.hashCode();
		
		//调用InjectionDAO的save方法处理数据
		injectionDAO.save(arg);//--->配置xml配置文件
	}

}


运行结果:

开始执行: 2016年07月16日 08时:35分:35秒
七月 16, 2016 8:35:35 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 20:35:35 CST 2016]; root of context hierarchy
七月 16, 2016 8:35:35 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
接收的数据: @Autowired
保存的数据: @Autowired:518522822
执行完成: 2016年07月16日 08时:35分:35秒

得到的结果是一样的。



测试例子:第三种注入方式,在构造器上使用@Autowired注解的方式。

package com.test.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.dao.InjectionDAO;

@Service
public class InjectionServiceImpl implements InjectionService {

	//@Autowired: 给属性添加注解
	InjectionDAO injectionDAO;

	//@Autowired//给setter方法添加注解
	public void setInjectionDAO(InjectionDAO injectionDAO) {
		this.injectionDAO = injectionDAO;
	}
	
	@Autowired//使用构造器添加注解注入
	public InjectionServiceImpl(InjectionDAO injectionDAO)
	{
		this.injectionDAO = injectionDAO;
	}

	@Override
	public void save(String arg) {

		System.out.println("接收的数据: "+arg);
		//处理数据
		arg = arg+":"+this.hashCode();
		
		//调用InjectionDAO的save方法处理数据
		injectionDAO.save(arg);//--->配置xml配置文件
	}

}


运行结果:

开始执行: 2016年07月16日 09时:10分:00秒
七月 16, 2016 9:10:00 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 21:10:00 CST 2016]; root of context hierarchy
七月 16, 2016 9:10:00 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
接收的数据: @Autowired
保存的数据: @Autowired:1738236591
执行完成: 2016年07月16日 09时:10分:00秒

总结: 使用@Autowired可以完成成员变量注入,setter注入和构造器注入三种方式的注入,最后的运行结果都是相同的。


第二部分:


可以使用@Autowired来注解那些众所周知的解析依赖性接口,比如: BeanFactory,ApplicationContext,Environment,ResourLoader,ApplicationEvent,和MessageSource。


如果希望数组有序,可以让bean实现org.springframework.core.Ordered接口或者使用@Order注解。


@Autowired注解是由Spring的BeanPostProcessor处理的所以不能再自己的BeanPostProcessor类型运用这些注解,这些类型必须通过XML或者@Bean注解加载。


例子:数组及Map的自动注入:

package com.test.multibean;

public interface BeanInterface {

}


package com.test.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(value=1)
@Component
public class BeanImplOne implements BeanInterface {

}

package com.test.multibean;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(value=2)
@Component
public class BeanImplTwo implements BeanInterface {

}


package com.test.multibean;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//调用类
@Component
public class BeanInvoker {
	
	//List的方式
	@Autowired
	private List<BeanInterface> list;
	
	//Map的方式
	@Autowired
	private Map<String,BeanInterface> map;
	
	public void say()
	{
		System.out.println("List方式: ");
		if(null != list &&list.size() != 0)
		{
			for (BeanInterface bean : list) {
				System.out.println(bean.getClass().getName());
			}
		}else
		{
			System.out.println("List<BeanInterface> is null!!!!");
		}
		
		
		
		System.out.println("Map方式: ");
		if(null != map &&map.size() !=0)
		{
			//遍历map
			for(Map.Entry<String, BeanInterface> entry:map.entrySet())
			{
				System.out.println(entry.getKey()+"    "+entry.getValue().getClass().getName());
			}
		}else
		{
			System.out.println("Map<String,BeanInterface> map is null!");
		}
	}

}

测试类:

	@Test
	public void testBeanInvoke()
	{
		//加载配置文件
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//获得实例
		BeanInvoker bi = (BeanInvoker)ac.getBean("beanInvoker");
		
		bi.say();
	}


测试结果:

开始执行: 2016年07月16日 09时:58分:50秒
七月 16, 2016 9:58:51 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7960847b: startup date [Sat Jul 16 21:58:51 CST 2016]; root of context hierarchy
七月 16, 2016 9:58:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
List方式: 
com.test.multibean.BeanImplOne
com.test.multibean.BeanImplTwo
Map方式: 
beanImplOne    com.test.multibean.BeanImplOne
beanImplTwo    com.test.multibean.BeanImplTwo
执行完成: 2016年07月16日 09时:58分:50秒

7、@Aualifier;

8、@Resource;


从Spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用Java而不是XML配置文件定义Bean。比如:

@Component,@Bean,@Import,@DependsOn.其中@Component是一个通用注解,可以使用于任何Bean。

@Repository,@Service,@Controller是更有针对性的注解

--@Repository通常用于注解DAO类,即持久层;

--@Service通常用于注解Service类,即服务层;

--@Controller通常用于Controller类,即控制层(MVC);


元注解: 许多Spring提供的注解可以作为自己的代码,即“元数据注解”,元注解是一个简单的注解,可以应用到了一个注解

除了value(),元注解还可以有其他的属性,允许定制。














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值