spring配置文件详解--注解方式

先来一段配置示例

<?xml version="1.0" encoding="UTF-8"?>
<beans 
	<!--最基本的命名空间定义 -->
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	<!--启用自动扫描或注解装配时的命名空间 -->
	xmlns:context="http://www.springframework.org/schema/context"
	<!--启用AOP功能时的命名空间-->
	xmlns:aop="http://www.springframework.org/schema/aop" 
	<!--启用声明事务时的命名空间-->
	xmlns:tx="http://www.springframework.org/schema/tx"
	<!--p名称空间,详解请看我的另一篇博客“Spring使用p名称空间配置属性”-->
	xmlns:p="http://www.springframework.org/schema/p"
	<!--与上述命名空间相配套的schema定义文件的装载路径-->
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
		
	<!--开启注解处理器-->
	<context:annotation-config />
	
	<!--开启组件自动扫描,扫描路径由base-package属性指定,配置了此项后,就无需配置<context:annotation-config>-->
	<context:component-scan base-package="com.yoon">
		<!--子节点表示要包含的目标类-->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
		<!--子节点表示要排除在外的目标类-->
		<context:exclude-filter>
	</context:component-scan>
	<!--开启基于@AspectJ切面的注解处理器-->
	<aop:aspectj-autoproxy />
	<!--支持事务注解的(@Transactional), 需要注意的是,spring 是使用 aop 通过 asm 操作java字节码的方式来实现对方法的前后事务管理的-->
	 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
	<!--事务管理器-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!--dataSourceId数据源beanId-->
		<property name="dataSource" ref="dataSourceId" />
	</bean>
</beans>
注意:
1、组件扫描(component scanning):Spring是从classpath下自动扫描,侦测和实例化具有特定注解的组件
特定的组件包括:
    -@Component:基本注解,标识了一个受Spring管理的组件
    -@Responsitory:标识持久层组件
    -@Service:标识服务层(业务层)组件
    -@Controller:标识表现层组件
2、对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写
3、<context:component-sacn>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点
<context:include-filter>和<context:exclude-filter>子节点支持多种类型的过滤表达式:
类别 示例 说明
annotation  com.yl.XxxAnnotation 所有标注了XxxAnnotation的类,该类型采用目标类是否标注了某个注解进行过滤
assinable  com.yl.XxxService 所有继承或扩展XxxService的类,该类型采用了目标类是否继承或扩展某个特定类进行过滤
aspectj  com.yl.*Service 所有类名义Service结束的类及继承或扩展它们的类,该类型采用AspectJ表达式进行过滤
regex  com.yl.anno.* 所有com.yl.anno包下的类。该类型采用正则表达式,根据类的类名进行过滤
custom  com.yl.XxxTypeFilter  采用XxxTypeFilter通过代码的方式定义过滤原则。该类必须实现org.springframework.core.type.TypeFilter接口

组件装配
<context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostProcessor实例,该实例可以自动装配具有@Autowired和@Resource注解的属性,还有其它的注解,不是重点,这里不讲解。

使用@Autowired、@Resource自动装配bean
在java代码中使用@Autowired或@Resource注解方式进行装配 ,这两个注解的区别是:@Autowired默认按类型装配,@Resource默认按名称装配,当找不到名称匹配的bean才会按类型装配。
@Autowired一般装配在set方法之上,也可以装配在属性上边,但是在属性上边配置,破坏了java的封装,所以一般不建议使用

@Autowired是根据类型进行自动装配的。如果当Spring上下文中存在不止一个所要装配类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在所要装配类型的bean,也会抛出BeanCreationException异常。



参考:http://blog.csdn.net/zzjjiandan/article/details/22922847/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章