Spring配置文件详解

来源:互联网 发布:八爪鱼淘宝营销大师 编辑:程序博客网 时间:2024/06/04 19:16

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

2.配置文件模板

<?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"CDATA[启动AOP功能时的命名空间:xmlns:context="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-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/context/spring-aop-4.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/context/spring-tx-4.1.xsd"--><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"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">         <!-- 开启注解处理器 -->    <context:annotation-config/>        <!-- 读取.properties配置文件 文件路径由location属性指定(属性值以classpath代表类路径下) -->    <context:property-placeholder location="classpath:路径"/>        <!-- 开启组件自动扫描,扫描路径由base-package属性指定-->    <context:component-scan base-package="*"></context:component-scan><!-- 开启基于@Aspectj切面的注解处理器 --><aop:aspectj-autoproxy/><!-- 使用class属性指定类(类全名)的默认构造方法创建一个单实例Bean,名称由id属性指定(不写id属性,名称则为首字母小写的类名与#编号组成,多个bean的class属性值相同时,编号从0开始递增) --><bean class="Bean类全名"/><bean id="Bean实例名称1" class="Bean类全名"/><!-- scope属性为singleton时表示单实例,为prototype时表示多例(每次将生成新的实例) --><bean id="Bean实例名称2" class="Bean类全名" scope="prototype"/><!-- init-method 属性用于指定对象实例化后要调用的初始化方法;destroy-method属性用于指定对象在销毁时要调用的方法 --><bean id="Bean实例名称3" class="Bean类全名" init-method="初始化时调用的方法名" destroy-method="对象销毁时调用的方法名"/><bean id="Bean实例名称4" class="Bean类全名"><!-- property标签用于对Bean实例中属性进行赋值,对于基本数据类型的值可由value属性直接指定,而ref则表示其他Bean实例的引用 --><property name="Bean类中的属性名称" ref="要引用的Bean名称"></property><property name="Bean类中的属性名称" value="直接指定属性值"></property><property name="Bean类中的属性名称"><!-- 创建一个内部匿名Bean实例赋值给指定的属性,该匿名Bean实例无法被外界访问 --><bean class="Bean类全名"/></property><property name="Bean类中的属性名称"><!-- set标签用于创建一个Set类型的实例赋值给指定的Set类型属性,Set实例中元素通过valu或ref子标签指定。对于基本的数据类型可由value标签生成,如果需要引用其他Bean实例作为Set元素的话,可由ref标签指定--><set><value>set中的元素</value><ref bean="要引用的Bean名称"/></set></property><property name="Bean类中的属性名称"><!-- list标签用于创建一个List类型的实例赋值给指定的List类型属性,List实例中元素通过valu或ref子标签指定。对于基本的数据类型可由value标签生成,如果需要引用其他Bean实例作为List元素的话,可由ref标签指定--><list><value>list中的元素</value><ref bean="要引用的Bean名称"/></list></property><property name="Bean类中的属性名称"><!-- map标签用于创建一个类型的实例赋值给指定的Map类型属性,Map实例中的元素通过entry子标签指定。Map元素的键由entry标签的key属性直接指定,值则可由value或ref子标签指定--><map><entry key="map元素的key"><value>map元素的value</value></entry><entry key="map元素的key"><ref bean="要引用的Bean名称"/></entry></map></property><property name="Bean类中的属性名称"><!-- 创建一个Properties类型的实例赋值给指定的Properties类型属性 --><props><!--Properties实例中属性项元素由prop标签生成,属性项元素由key属性指定,属性项的值可直接放置在prop标签体中 --><prop key="properties元素的key">properties元素的value</prop></props></property><property name="Bean类中要初始化为null值的属性名称"><!-- 给属性赋null值 --><null/></property></bean><bean id="Bean实例名称5" class="Bean类全名"><!-- 通过传入相应的构造参数进行Bean实例化,constructor-arg标签用于指定一个构造参数,其index属性标明当前是第几个参数,type属性声明构造参数的类型 --><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 expression="切入点正则表达式" id="切入点名称"/><aop:before pointcut-ref="切入点名称" method="切面类中用做前置通知的方法名"/><aop:after-returning 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="要引用的数据源实例名称"></property></bean><!-- 配置一个事物通知  --><tx:advice id="事物通知名称" transaction-manager="事物管理器实例名称"><tx:attributes><!-- 方法以get开头,不使用事物 --><tx:method name="get*" read-only="true" propagation="REQUIRED"/><!-- 其他方法默认事务进行 --><tx:method name="*"/></tx:attributes></tx:advice><!-- 使用AOP技术实现事务管理 --><aop:config><aop:pointcut id="事务切入点名称" expression="事务切入点正则表达式"/> <aop:advisor advice-ref="事务通知名称" pointcut-ref="事务切入点名称"/></aop:config></beans>

原创粉丝点击