<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName"
>
<!--类似于Mybatis时,配置文件中的引入配置文件<properties resource="jdbc.properties"/>-->
<context:property-placeholder location="classpath:database.properties"/>
<!--类似于Mybatis时,配置文件中的environment标签-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--类似于在Mybatis时: Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<value>classpath:com/lyx/mapper/*.xml</value>
</property>
<property name="typeAliasesPackage" value="com.lyx.entity"/>
</bean>
<!--把Mapper映射文件的配置信息放到SpringIOC容器中,
类似于在使用Mybatis时:sqlSessionFactory.openSession => sqlSession =>
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lyx.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
</bean>
<bean id="studentService" class="com.lyx.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txManager" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="query*" propagation="SUPPORTS" isolation="READ_COMMITTED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--完全通过注入bean实现前置后置通知...-->
<bean id="myBefore" class="com.lyx.aop.MyBeforeAdvice"></bean>
<bean id="myAfterAdvice" class="com.lyx.aop.MyAfterAdvice"></bean>
<bean id="myException" class="com.lyx.aop.LogException"/>
<bean id="myAround" class="com.lyx.aop.MyAround"/>
<aop:config proxy-target-class="true">
<aop:pointcut id="pc" expression="
execution(* com.lyx.service.StudentService.deleteStudent()) or
execution(* com.lyx.service.StudentService.showStudent())"/>
<aop:advisor advice-ref="myBefore" pointcut-ref="pc"/>
<aop:advisor advice-ref="myAfterAdvice" pointcut-ref="pc"/>
<aop:advisor advice-ref="myException" pointcut-ref="pc"/>
<aop:advisor advice-ref="myAround" pointcut-ref="pc"/>
</aop:config>
<!--注解的形式实现前置后置通知...-->
<!--只针对@Aspect注解-->
<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"></aop:aspectj-autoproxy>
<!--只针对@Component @Repository @Service @Controller修饰的类产生的对象,将其增加到IOC容器中-->
<context:component-scan base-package="com.lyx"></context:component-scan>
<!--Schema形式实现前置后置通知-->
<bean id="logSchema" class="com.lyx.aop.LogSchema"></bean>
<aop:config>
<!--切入点-->
<aop:pointcut id="pc" expression="execution(* com.lyx.service.impl.StudentServiceImpl.showStudent())"/>
<!--Schema方式-->
<aop:aspect ref="logSchema">
<!--连接线:连接业务showStudent() 和通知MyBefore()-->
<aop:before method="MyBefore" pointcut-ref="pc"/>
<!--连接线:连接业务showStudent() 和通知MyAfterReturning()-->
<aop:after-returning method="MyAfterReturning" pointcut-ref="pc" returning="returnValue"/>
</aop:aspect>
</aop:config>
<!--为某个类添加额外功能,但代码上不直接体现-->
<bean id="women" class="com.lyx.declareparents.impl.Women"/>
<bean id="jogging" class="com.lyx.declareparents.impl.Jogging"/>
<aop:config proxy-target-class="true">
<aop:aspect>
<aop:declare-parents types-matching="com.lyx.declareparents.impl.Women"
implement-interface="com.lyx.declareparents.WarmUp"
default-impl="com.lyx.declareparents.impl.Jogging"/>
</aop:aspect>
</aop:config>
</beans>
Spring,application.xml配置文件代码
于 2022-10-25 10:20:40 首次发布