Spring 配置文件详解
2016-03-05 14:37
671 查看
Spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Java EE程序员必须学会并灵活应用这份"图纸"准确地表达自己的"生产意图"。Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。
下面列举的是一份比较完整的配置文件模板,文档中各XML标签节点的基本用途也给出了详细的解释,这些XML标签节点在后续的知识点中均会用到,熟练掌握了这些XML节点及属性的用途后,为我们动手编写配置文件打下坚实的基础。
下面列举的是一份比较完整的配置文件模板,文档中各XML标签节点的基本用途也给出了详细的解释,这些XML标签节点在后续的知识点中均会用到,熟练掌握了这些XML节点及属性的用途后,为我们动手编写配置文件打下坚实的基础。
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <description>1、组件信息</description> <!-- 引入其它资源文件 --> <import resource="app-content.xml"/> <!-- 开启注解处理器 --> <context:annotation-config /> <!-- 开启组件自动扫描,扫描路径由base-package属性指定 --> <context:component-scan base-package="www.lingmm.com.study.impl" /> <!-- 开启基于@Aspctj切面的注解处理器 --> <aop:aspectj-autoproxy /> <!-- 使用class属性指定类的默认构造方法创建一个单实例Bean,名称由id属性指定 init-method初始化时调用的方法名 destroy-method对象在销毁时调用的方法 --> <description>2、注入各类型值</description> <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl" init-method="sayHello" destroy-method="sayHello"> <!-- 类属性引用的javaBean --> <property name="propName" ref="referencedBean"> <!-- 直接指定属性值 --> <property name="propName" value="value"> </bean> <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl"> <!-- 类属性为list对象 --> <property name="listProperty"> <list> <value>指定list对象的value</value> <ref bean="要引用的bean名称" /> </list> </property> </bean> <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl"> <!-- 类属性为set对象 --> <property name="setProperty"> <set> <value>指定set对象的value</value> <ref bean="要引用的bean名称" /> </set> </property> </bean> <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl"> <!-- 类属性为map对象 --> <property name="mapProperty"> <map> <value>指定set对象的value</value> <ref bean="要引用的bean名称" /> </map> </property> </bean> <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl"> <!-- 类属性为props对象 --> <property name="propProperty"> <props> <prop key="propKey">propValue</prop> </props> </property> </bean> <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl"> <!-- bean类中要初始化为Null的属性名称 --> <property name="nullProperty"> <null /> </property> </bean> <description>3、切面</description> <aop:config> <aop:aspect id="切面id" ref="要引用的切面实例名称"> <aop:pointcut id="切入点名称" expression="切入点正则表达式"/> <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> <description>4、事务</description> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务通知 --> <tx:advice id="transaction" transaction-manager="transactionManager"> <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> </beans>引用地址:http://book.51cto.com/art/201004/193743.htm
相关文章推荐
- JAVA编程指南 --继承
- eclipse默认编码为GBK,修改为UTF8的方法
- Eclipse 如何设置注释的模板
- 20145325张梓靖 《Java程序设计》第1周学习总结
- java调用天气预报webservice
- 一些小例子程序,用于说明JDBC的使用步骤。
- [疯狂Java笔记]事件处理:事件类型、监听器接口、事件适配器
- Java 多线程(七) 线程间的通信——wait及notify方法
- JSP与JavaBean
- spring+mybatis连接access数据库
- Java入门:Java环境变量PATH、CLASSPATH、JAVA_HOME
- leetcode:238. Product of Array Except Self(Java)解答
- java几种常见的对话框整理汇总
- Java 多线程(六) synchronized关键字详解
- Spring、Hibernate的通用DAO层与Service层的实现
- Java之HashMap类实用方法大全
- Ubuntu jdk报Picked up JAVA_TOOL_OPTIONS信息解决
- JavaSE入门学习25:Java异常处理(上)
- JAVA 多进程 概念 创建 通信
- Java中String类的内存分配