首页 > 编程语言 > 详细

spring xml配置相关

时间:2020-06-16 10:25:43      阅读:36      评论:0      收藏:0      [点我收藏+]

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
    <!-- 加载资源文件jdbc.properties -->
<!--    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
<!--        <property name="location" value="jdbc.properties"></property>-->
<!--    </bean>-->
    <!-- 开启aspectJ的自动代理功能 -->
    <aop:aspectj-autoproxy/>
    <!-- 开启注解 -->
    <context:component-scan base-package="com.csh.testcomponent"/>
    <!-- 导入外部文件,properties -->
    <context:property-placeholder location="jdbc.properties"/>
    <!-- 导设置数据库连接池,druid -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>

    </bean>
    <!-- 使用bean元素定义一个由IOC容器创建的对象 -->
    <!-- class属性指定用于创建bean的全类名 -->
    <!-- id属性指定用于引用bean实例的标识 -->
    <bean id="student" class="com.csh.pojo.Student">
        <property name="studentId" value="1001"/>
        <property name="studentName" value="Tom2015"/>
        <property name="age" value="18"/>
    </bean>



<!--    <bean id="teacher" class="com.csh.pojo.Teacher">-->
<!--        <property name="tid" value="1"/>-->
<!--        <property name="tname" value="李先生"/>-->
<!--        <property name="students">-->
<!--            <list>-->
<!--               <ref bean="student"/>-->
<!--            </list>-->
<!--        </property>-->
<!--    </bean>-->

    <bean id="teacher02" class="com.csh.pojo.Teacher">
        <property name="tid" value="1111"/>
        <property name="tname" value="玉兔"/>
        <property name="students">
            <list>
                <value>sksjfksj</value>
                <value>看到附近</value>
                <value>士大夫</value>
                <value>OK见效快</value>
            </list>
        </property>
    </bean>
</beans>

 

properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

 



aop:
<?xml version="1.0" encoding="UTF-8"?>

-<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">

<context:component-scan base-package="com.atguigu.spring.aopxml"/>


-<aop:config>


-<aop:aspect ref="myLogger">

<aop:pointcut id="cut" expression="execution(* com.atguigu.spring.aopxml.*.*(..))"/>

<!-- <aop:before method="before" pointcut="execution(* com.atguigu.spring.aopxml.*.*(..))"/> -->


<aop:before pointcut-ref="cut" method="before"/>

</aop:aspect>

</aop:config>

</beans>

 

添加事务:

<?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.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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <context:component-scan base-package="com.atguigu.book_xml"></context:component-scan>
    
    <!-- 引入属性文件 -->
    <context:property-placeholder location="db.properties"/>

    <!-- 创建数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 通过数据源配置JdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置事务管理器,不管是用注解方式或xml方式配置事务,一定要有DataSourceTransactionManager事务管理器的支持 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置事务通知 -->
    <tx:advice id="tx" transaction-manager="dataSourceTransactionManager">
        <tx:attributes>
            <!-- 在设置好的切入点表达式下再次进行事务设置 -->
            <tx:method name="buyBook"/>
            <tx:method name="checkOut"/>
            
            <!-- 只有select开头的方法才会被事务处理 -->
            <tx:method name="select*" read-only="true"/>
            <tx:method name="insert*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            
            <tx:method name="*"/>
            
        </tx:attributes>
    </tx:advice>

    <!-- 配置切入点表达式 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.atguigu.book_xml.service.impl.*.*(..))" id="pointCut"/>
        <aop:advisor advice-ref="tx" pointcut-ref="pointCut"/>
    </aop:config>
    
</beans>

 

spring xml配置相关

原文:https://www.cnblogs.com/appium/p/13139186.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!