Spring,application.xml配置文件代码

<?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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

素心如月桠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值