AOP的使用及SSM框架整合

本文详细介绍了Spring AOP的两种实现方式,包括基于XML配置和注解的AOP操作,重点讲解了execution函数在切入点定义中的使用。同时,文章还阐述了Spring与Mybatis的整合思路,包括数据源配置、会话工厂创建以及Mapper的Spring管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、AOP的使用

1.1、基于XML配置形式实现AOP

1.1.4、execution函数介绍

1.2、基于注解形式实现AOP操作

2、SSM整合-Spring和mybatis框架整合

2.1、Spring和Mybatis的整合思路


1、AOP的使用

spring中整合AspectJ框架

AspectJ本身是一个单独的AOP框架,基于Java实现的,不是Spring的一部分,和SPring一块实现AOP操作

AOP的实现有两种形式:

基于XML配置形式实现

基于注解形式实现

1.1、基于XML配置形式实现AOP

1.1.1、引入依赖

除了AOP依赖,还需要将基础核心依赖引入

 <!--Spring的AOP jar包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>

1.1.2、模拟租房业务

给定一个房东类:(原始方法)

/**
 * 房东
 */
public class Landlord {
    public void service() {
        System.out.println("签合同");
        System.out.println("收钱");
    }
}

创建中介类:(增强)

/**
 * 中介
 */
public class Broker {

    public void  service1(){
        System.out.println("看房子");
    }

    public void  service2(){
        System.out.println("谈价钱");
    }

    public void  service3(){
        System.out.println("给钥匙");
    }

}

1.1.3、在XML引入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
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd">


</beans>

1.1.4、execution函数介绍

在通知中通过value属性定义切点,通过execution函数,可以定义切点的方法切入

1、切入点:实际增强的方法

2、常用的表达式

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

(1)execution(* com.tulun.bean.Book.show(..)) 表类里面的某一个方法

(2)execution(* com.tulun.bean.Book.*(..)) 表类某个包里类所有方法

(3)execution(* *.*(..)) 表示所有

例:

-匹配所有类public方法 execution(public *.*(..))

-匹配指定包下所有类方法 execution(* com.tulun.bean.*(..)) (不包含子包)

- execution(* com.tulun.bean..*(..)) (包含包、子包下所有类)

-匹配指定类所有方法 execution(* com.tulun.bean.Book.*(..))

-匹配实现特定接口所有类方法 execution(* com.tulun.bean.Book+.*(..))

-匹配所有com开头的方法 execution(* com*(..))

1.1.5、xml配置:

  <!--将类交给容器管理-->
    <bean id="landlord" class="com.tulun.Spring.AOP.Landlord"/>
    <bean id="broker" class="com.tulun.Spring.AOP.Broker"/>

    <!--配置AOP操作-->
    <aop:config >
        <!--
        aop:pointcut标签
        配置切入点
        对于要进行增强的连接点称之为切入点
        id属性:取名称
        expression:切入点表达式
        execution表达式
        -->
        <aop:pointcut id="pointcut1" expression="execution(* com.tulun.Spring.AOP.Landlord.service(..))"/>

        <!--
        aop:aspect标签
        配置切面
        把增强应用到切入点的过程
        ref属性:指定增强
         id属性:取名称
         order属性:给多个增强排序
        -->
        <aop:aspect  ref="broker">
            <!--配置增强类型:前置增强-->
            <aop:before method="service1" pointcut-ref="pointcut1"/>
        </aop:aspect>

    </aop:config>

增强类型:

1.1.6、测试使用:

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("springcontext3.xml");
        Landlord landlord = (Landlord)applicationContext.getBean("landlord");
        landlord.service();

执行结果:

各种类型增强如下:

<!--配置增强类型:前置增强(aop:before)-->
            <aop:before method="service1" pointcut-ref="pointcut1"/>
            <!--后置增强(aop:after )-->
            <aop:after method="service2" pointcut-ref="pointcut1"/>
            <!--最终增强(aop:after-returning )-->
            <aop:after-returning method="service3" pointcut-ref="pointcut1"/>
            <!--异常增强(aop:after-throwing)-->
            <aop:after-throwing method="service4" pointcut-ref="pointcut1"/>

            <!--环绕增强-->
            <aop:around method="service5" pointcut-ref="pointcut1"/>

1.2、基于注解形式实现AOP操作

1.2.1、在xml配置文件中引入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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--开启注解扫描-->
    <context:component-scan base-package="com.tulun.Spring.AOP"/>

    <!--开启AOP操作-->
    <aop:aspectj-autoproxy/>
</beans>


1.2.2、在增强类上添加注解

Java复制代码

/**
 * 中介
 */
@Component
@Aspect
//当前类开启AOP操作
public class Broker {

    //前置增强注解,@Before
    @Before(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")
    public void  service1(){
        System.out.println("看房子");
    }
}
@Aspect 注解添加在类上,表示当前类是增强类
@Before注解添加在方法上,表示前置增强
@Before(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")
关于增强注解:

Java复制代码

//前置增强注解,@Before
    @Before(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")
    //后置增强
    @After(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")
//最终增强
    @AfterReturning(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))") 
    @AfterThrowing(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))") //异常增强
    @Around(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")//环绕增强
   

2、SSM整合-Spring和mybatis框架整合

2.1、Spring和Mybatis的整合思路

在mybatis中,创建会话工厂通过SQLSessionFactoryBuilder类读取配置并创建会话工厂

,一般将会话工厂设置为单例形式,通过会话工厂创建会话SQLSession

在mybatis和spring整合后,当前的一些对象交给IOC容器管理

Spring可以荣国单例方式来管理SQLSessionFactory。

将mybatis中其他的创建对象交给容器管理

整合:mybatis提供了mybatis-spring框架jar,专门针对spring和mybatis的整合,

SQLSessionFactory会话工厂创建交给SQLSessionFactoryBean类来管理,

还需要有数据源,将数据源交给Spring管理,还有引入映射配置交给spring管理

2.2、整合步骤

2.2.1、引入依赖

需要引入spring依赖、mybatis依赖以及JDBC依赖,还需要引入新依赖

        <!--Mybatis和Spring整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        
                <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.propety}</version>
        </dependency>    

查询test库下的student表

2.2.2、创建pojo类、创建mapper.java、创建mapper.xml配置文件,详见前面的mybatis

2.2.3、Spring配置文件

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

    <!--数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--获取会话工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--映射关系-->
        <property  name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <!--通过代理对象获取SQLSession对象-->
    <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--注入会话工厂-->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <!--注入接口-->
        <property name="mapperInterface" value="com.tulun.Spring.SSM.mapper.StudentMapper"/>

    </bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值