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

你不知道的Spring配置文件

2016-04-01 18:55 429 查看
  Spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Java EE程序员必须学会并灵活应用这份"图纸"准确地表达自己的"生产意图"。Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。

下面列举的是一份比较完整的配置文件模板,文档中各XML标签节点的基本用途也给出了详细的解释,这些XML标签节点在后续的知识点中均会用到,熟练掌握了这些XML节点及属性的用途后,为我们动手编写配置文件打下坚实的基础。

<?xml version="1.0" encoding="UTF-8"?>
<beans //整个配置的根节点,包含一个或者多个bean元素

//最基本的命名空间定义
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"

//与上述命名空间定义相配套的schema定义文件的装载路径
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<!-- 配置开启注解处理器 -->
<context:annotation-config/>

<!--开启组件自动扫描,扫描路径由base-package属性指定,代表扫描指定包名以及其子包 -->
<context:component-scan base-package="test"></context:component-scan>

<!--开启基于@AspectJ切面的注解处理器-->
<aop:aspectj-autoproxy/>

<!-- 引入配置文件,文件位置由location属性指定 -->
<context:property-placeholder location="xxx"/>

<!--使用class属性指定类的默认构造方法创建一个单实例Bean,名称由id属性指定
1.scope属性为
prototype时表示每次将生成新的实例,即原型模式
singleton时表示在每个Spring IOC容器中该bean定义只有一个对象实例
2.init-method:初始化时调用的方法名
3.destroy-method:对象销毁时调用的方法名
-->
<bean id="Bean实例名称" class="Bean类全名称" scope="prototype" init-method="init" destroy-method="close">

<property name="Bean类中的属性名称" value="直接指定属性值"/>

<property name="Bean类中的属性名称" ref="要引用的Bean名称"/>

<property name="Bean类中的属性名称">
<!--创建一个内部匿名Bean实例赋值给指定的属性,该匿名Bean实例无法被外界访问-->
<bean class="Bean类全名"/>
</property>

<property name="Bean类中的Set类型属性名称">
<!--
set标签用于创建一个Set类型的实例赋值给指定的Set类型属性,Set实例中的元素
通过value或ref子标签指定。对于基本数据类型的元素可由value标签生成,如果需
要引用其他Bean实例作为Set元素的话,可由ref标签指定。
-->
<set>
<value>要设置到set中的元素</value>
<ref bean="要引用的Bean名称"/>
</set>
</property>

<property name="Bean类中的List类型属性名称">
<!--
list标签用于创建一个List类型的实例赋值给指定的List类型属性,List实例中的元素
通过value或ref子标签指定。对于基本数据类型的元素可由value标签生成,如果需要引
用其他Bean实例作为List元素的话,可以由ref标签指定。
-->
<list>
<value>要设置到list中的元素</value>
<ref bean="要引用的Ben名称"/>
</list>
</property>

<property name="Bean类中的Map类型属性名称">
<!--
map标签用于创建一个Map类型的实例赋值给指定的Map类型属性,Map实例中的元素通过
entry标签指定。Map元素的键由entry标签的key属性直接指定,值则可由value或ref子
标签的key属性直接指定。对于基本数据类型的元素可由value标签生成,如果需要引用
其他Bean实例作为List元素的话,可以由ref标签指定。
-->
<map>
<entry key="map元素的key">
<value>map元素的value</value>
</entry>
<entry key="map元素的key">
<ref bean="要引用的Ben名称"/>
</entry>
</map>
</property>

<property name="Bean类中的Properties类型属性名称">
<!--
创建一个Properties类型的实例赋值给指定的Properties类型属性
-->
<props>
<!--Properties实例中的元素由prop标签生成,属性项元素的键由key属性指定,属性元素
的值可直接放置在prop标签中-->
<prop key="properties元素的key">properties元素的value</prop>
</props>
</property>

<property name="Bean类中要初始化为null的属性名称">
<null/><!--null标签用于给需要赋值为null值得属性进行赋null值-->
</property>

<!--
通过传入相应的构造参数进行Bean实例化,constructor-arg标签用于指定一个构造参数,其index
属性标明当前是第几个构造参数(从0开始),type属性声明构造参数的类型,构造参数的值如果是基
本类型可由value属性值直接指定,如果是对象的引用,则由ref属性指定。
-->
<constructor-arg index="从0开始的序号" type="构造参数的类型" value="构造参数的值"/>
<constructor-arg index="从0开始的序号" type="构造参数的类型" ref="要引用的Bean名称"/>
</bean>

<!--配置切面-->
<bean id="目标对象名称" class="目标对象全名"/>
<bean id="切面实例名称" class="切面类全名"/>
<aop:config>
<aop:aspect id="切面id" ref="要引用的切面实例名称">
<aop:pointcut id="切入点名称" expression="切入点正则表达式"/>
<aop:before pointcut-ref="切入点名称" method="切面类中用作前置通知的方法名"/>
<aop:after-returing pointcut-ref="切入点名称" method="切面类中用作后置通知的方法名"/>
<aop:after-throwing pointcut-ref="切入点名称" method="切面类中用作异常通知的方法名"/>
<aop:after pointcut-ref="切入点名称" method="切面类中用作最终通知的方法名"/>
<aop:around pointcut-ref="切入点名称" method="切面类中用作环绕通知的方法名"/>
</aop:aspect>
</aop:config>

<!-- 配置事务管理器 -->
<bean id="事务管理器实例名称" class="事务管理器类全名">
<property name="数据源属性名称" ref="要引用的数据源实例名称">
</bean>

<!-- 配置一个事务通知 -->
<tx:advice id="" transaction-manager="">
<tx:attributes>
<!-- 方法以get开头的,不使用事务 -->
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<!-- 其他方法以默认事务进行 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>

<!-- 使用AOP技术实现事务管理 -->
<aop:config>
<aop:pointcut id="事务切入点名称" expression="事务切入点正则表达式"/>
<aop:advisor advice-ref="事务通知名称" pointcut-ref="事务切入点名称"/>
</aop:config>

<!--使用基于注解方式配置事务, -->
<tx:annotation-driven transaction-manager="事务管理器Bean的id"/>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: