Spring5.2.8常用的XML配置

Spring5.2.8常用的XML配置

1. 使用Spring的Maven依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

2. IOC容器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

3. 注解实现自动装配

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启注解支持-->
    <context:annotation-config/>
    <!--扫描指定包让注解生效-->
    <context:component-scan base-package="com.cokebot"/>

</beans>

4. AOP的XML配置

  • 方式一:使用原生的Spring API接口
  • 方式二:自定义类
  • 方式三:注解实现AOP
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 注册bean -->
    <bean id="userService" class="com.cokebot.service.UserServiceImpl"/>
    <bean id="beforeLog" class="com.cokebot.log.BeforeLog"/>
    <bean id="afterLog" class="com.cokebot.log.AfterLog"/>

    <!-- 方式一:使用原生的Spring API接口 -->
    <!-- 配置AOP,需要导入约束 -->
    <!--<aop:config>
        &lt;!&ndash;
            切入点
            expression:表达式
            execution(要执行的位置)
        &ndash;&gt;
        <aop:pointcut id="pointcut" expression="execution(* com.cokebot.service.UserServiceImpl.*(..))"/>
        &lt;!&ndash;执行环绕增强&ndash;&gt;
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>-->


    <!--方式二:自定义类-->
    <!--注册bean-->
    <!--<bean id="diy" class="com.cokebot.diy.DiyPointCut"/>

    <aop:config>
        &lt;!&ndash;自定义切面,ref:要引用的类&ndash;&gt;
        <aop:aspect ref="diy">
            &lt;!&ndash;切入点&ndash;&gt;
            <aop:pointcut id="point" expression="execution(* com.cokebot.service.UserServiceImpl.*(..))"/>
            &lt;!&ndash;通知&ndash;&gt;
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>-->

    <!--方式三:注解实现AOP-->
    <!--注册bean-->
    <bean id="annotationAOP" class="com.cokebot.anno.AnnotationAOP"/>
    <!--
        开启注解支持
        默认:proxy-target-class="false" 原生jdk实现
        proxy-target-class="true" cglib实现
    -->
    <aop:aspectj-autoproxy proxy-target-class="false"/>

</beans>

5. Spring整合MyBatis导入的Maven依赖

<dependencies>

    <!--junit 4.12-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <!-- mysql 5.1.46 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>

    <!-- mybatis 3.5.5 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.5</version>
    </dependency>

    <!-- spring-webmvc 5.2.8 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>

    <!-- Spring操作数据库还需要一个spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>

    <!-- aspectjweaver 1.9.4 -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>

    <!-- mybatis-spring 2.0.5 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.5</version>
    </dependency>
    
</dependencies>

6. Spring整合MyBatis的XML配置

6.1 手写SqlSessionTemplate(入门推荐)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
        1.dataSource:使用Spring的数据源替换Mybatis的配置
        使用org.springframework.jdbc.datasource下的数据源
    -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--
        2.sqlSessionFactory
        指定类:org.mybatis.spring.SqlSessionFactoryBean
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 绑定mybatis -->
        <property name="configLocation" value="classpath:mybatis-3-config.xml"/>
        <!-- 绑定mapper -->
        <property name="mapperLocations" value="com/cokebot/mapper/UserMapper.xml"/>
    </bean>

    <!-- 3.sqlSessionTemplate:等同sqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!-- 没有setter方法,只能用构造器注入 -->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

</beans>

6.2 mapper实现类继承SqlSessionDaoSupport(熟练推荐)

<!--
    方式二:SqlSessionDaoSupport
    本质就是方式一,可以省略SqlSessionTemplate的配置
-->
<bean id="userMapper2" class="com.cokebot.mapper.UserMapperImpl2">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

7. 声明式事务(AOP)

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--
        1.dataSource:使用Spring的数据源替换Mybatis的配置
        使用org.springframework.jdbc.datasource下的数据源
    -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--
        2.sqlSessionFactory
        指定类:org.mybatis.spring.SqlSessionFactoryBean
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 绑定mybatis -->
        <property name="configLocation" value="classpath:mybatis-3-config.xml"/>
        <!-- 绑定mapper -->
        <property name="mapperLocations" value="com/cokebot/mapper/UserMapper.xml"/>
    </bean>

    <!-- 声明式事务配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 结合AOP实现事务的织入 -->
    <!-- 配置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 给哪些方法配置事务 -->
        <!-- 事务的传播特性:propagation 默认为 REQUIRED -->
        <tx:attributes>
            <tx:method name="get" read-only="true"/>
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!-- 事务的切入 -->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.cokebot.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>

</beans>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值