spring的配置信息
1、 spring的applicationContext.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:tx="http://www.springframework.org/schema/tx"
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">
<!-- 引入的子配置文件 -->
<import resource="子配置文件路径"/>
</beans>
----------------------------------------------------------------------------------
2、控制反转中创建bean
----------------------------------------------------------------------------------
<!-- 利用静太工厂模式创建bean对象 factory-method工厂方法: 工厂类中指定的方法 -->
<bean id="helloFactory" class="cn.itcast.ioc.createObject.method.HelloWorldFactory"
factory-method="getInstance"></bean>
<!--实例工厂方式(注: 了解即可) -->
<bean id="" factory-bean="" factory-method="" />
----------------------------------------------------------------------------------
3、spring的注解解析器配置
----------------------------------------------------------------------------------
<!-- spring注解需要的注解解析器 -->
<context:annotation-config></context:annotation-config>
<!-- 依赖注入的注解解析器 -->
<context:annotation-config></context:annotation-config>
<!-- 类扫描的注解解析器 -->
<context:component-scan base-package="要扫描的包"></context:component-scan>
<!-- AOP的注解解析器 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
----------------------------------------------------------------------------------
4、spring结合JDBC的配置
----------------------------------------------------------------------------------
springjdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名称
jdbc.username=root
jdbc.password=密码
applicationContext.xml:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:springjdbc.properties的路径</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
// 引入其他的bean
方式一:让自己写的一个dao类继承JdbcDaoSupport
<bean id="personDao1" class="cn.itcast.springjdbc.manyStyle.PersonDao1">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
方式2:让自己写的一个dao类继承JdbcTemplate
<bean id="personDao2" class="cn.itcast.springjdbc.manyStyle.PersonDao2">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
方式3:让自己写的一个dao类里有一个属性为JdbcTemplate
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="personDao3" class="cn.itcast.springjdbc.manyStyle.PersonDao3">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
----------------------------------------------------------------------------------
5、spring结合jdbc声明式事务处理配置
----------------------------------------------------------------------------------
springjdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springaop
jdbc.username=root
jdbc.password=123456
applicationContext.xml:
<!-- spring与jdbc整合的配置信息 注: 需要导入dbcp的数据源jar包和mysql的数据库驱动包 还有springjdbc.properties文件-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:springjdbc.properties的路径</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean>
<!-- 引入事务管理器的配置,并同时引入datasource -->
<bean id="transactionManager" class="
org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!--
通知:
1.告诉spring容器,采用什么样的方法处理事务
2.告诉spring容器,目标方法应该采取什么样的事务处理策略
-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!--
name: 规定指定方法采取声明式事务处理;
isolation 默认值为default,一般省略不写
propation 传播机制, 默认值为required,一般省略不写
-->
<tx:method name="save*" read-only="false"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 注: 是service层起事务 -->
<aop:pointcut expression="execution(* cn.itcast.service.impl.*.*(..))" id="perform"/>
<!-- 事务的通知 -->
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
----------------------------------------------------------------------------------
4、spring结合jdbc声明式事务处理配置
----------------------------------------------------------------------------------
springjdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springaop
jdbc.username=root
jdbc.password=123456
applicationContext.xml:
<!-- spring与jdbc整合的配置信息 注: 需要导入dbcp的数据源jar包和mysql的数据库驱动包 还有springjdbc.properties文件-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:springjdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean>
<bean id="myException" class="cn.itcast.exception.MyException"></bean>
<!-- 引入事务管理器的配置,并同时引入datasource -->
<bean id="transactionManager" class="
org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!--
通知:
1.告诉spring容器,采用什么样的方法处理事务
2.告诉spring容器,目标方法应该采取什么样的事务处理策略
-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!--
name: 规定指定方法采取声明式事务处理;
isolation 默认值为default,一般省略不写
propation 传播机制, 默认值为required,一般省略不写
-->
<tx:method name="save*" read-only="false"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 注: 是service层起事务 -->
<aop:pointcut expression="execution(* cn.itcast.service.impl.*.*(..))" id="perform"/>
<!-- 事务的通知 -->
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
<aop:aspect ref="myException">
<aop:after-throwing method="myException" pointcut-ref="perform" throwing="ex"/>
</aop:aspect>
</aop:config>
----------------------------------------------------------------------------------