SSM学习笔记之Spring之通知

、spring增强类型

       1.前置通知(org.springframe.aop.MethodBeforeAdvice)

       2.后置通知(org.springframe.aop.AfterReturningAdvice)

       3.环绕通知(org.aopalliance.intercept.MethodInterceptor):前后都增强

       4.异常抛出通知(org.springframe.aop.ThrowsAdvice)

       5.引介通知(org.springframe.aop.IntroductionInterceptor):在目标类 添加一些新的方法和属性。

二、spring切面类型

      1.Advisor:代表一般切面,本身就是切面,对目标类所有方法进行拦截。

      2.PointcutAdvisor:代表具有切点的切面,可以指定拦截目标类的那些方法。

      3.IntroductionAdvisor:引介切面。

三、一般切面案例

     1.在maven中导入spring-aop和aopalliance。

     2.创建用于测试的接口和相应的实现类:StudentDao和StudentDaoImp

public interface StudentDao {
    public void find();
    public void  save();
    public void update();
    public void delete();
}
public class StudentDaoImp implements StudentDao {
    @Override
    public void find() {
        System.out.println("学生查询。。。");
    }

    @Override
    public void save() {
        System.out.println("学生删除");
    }

    @Override
    public void update() {
        System.out.println("学生修改");
    }

    @Override
    public void delete() {
        System.out.println("学生删除");
    }
}

     3.创建配置文件applicationContex.xml

<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置目标类-->
    <bean id="studentDao" class="com.imooc.aop.demo3.StudentDaoImp"/>
<!--这是一个前置通知-->
    <bean id="myBeforeAdvice" class="com.imooc.aop.demo3.MyBeforeAdvice"/>
<!--Spring的aop 产生代理对象-->
    <bean id="studentDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--目标类-->
        <property name="target" ref="studentDao"/>
<!--实现的接口-->
        <property name="proxyInterfaces" value="com.imooc.aop.demo3.StudentDao"/>
<!--采用什么拦截-->
        <property name="interceptorNames" value="myBeforeAdvice"/>
    </bean>
</beans>

     4.进行增强,创建增强类MyBeforeAdvice

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("这是前置通知。。。。。");
    }
}

    5.测试,创建测试类springDemo3

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContex.xml")
public class SpringDemo3 {
    @Resource(name = "studentDaoProxy") //增强
    private StudentDao studentDao;
    @Test
    public void demo1(){
        studentDao.find();
        studentDao.save();
        studentDao.update();
        studentDao.delete();
    }
}

   6.ProxyFactoryBean的其它属性

      #ProxyTargetClass:是否对类代理而不是接口。当值为true时使用cglib的方式进行代理。

      #singleton:是否为单例的,默认为是。

      #optimize:值为true时强制使用cglib。

   7.配置有切入点的切面(PointcutAdvisor) 

     1.配置demo类customerDao

public class CustomerDao {
    public void find(){
        System.out.println("查询客户。。。。");
    }
    public void save(){
        System.out.println("保存客户。。。。");
    }
    public void update(){
        System.out.println("修改客户。。。。");
    }
    public void delete(){
        System.out.println("删除客户。。。。");
    }
}

        2.配置代理类MyAroundAdvice

public class MyAroundAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("环绕前的增强========");

       Object obj = methodInvocation.proceed();

        System.out.println("环绕后的增强========");
       return obj;
    }
} 

          3.创建配置文件applicationContext2.xml

<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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 目标类 -->
    <bean id="customerDao" class="com.imooc.aop.demo4.CustomerDao"/>
<!-- 配置通知-->
    <bean id="myAroundAdvice" class="com.imooc.aop.demo4.MyAroundAdvice"/>

<!-- 配置切入点-->
    <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="pattern" value=".*"/>
        <property name="advice" ref="myAroundAdvice"/>
    </bean>
<!-- 配置增强-->
    <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <!-- 配置目标-->
        <property name="target" ref="customerDao"/>
        <property name="proxyTargetClass" value="true"/>
        <property name="interceptorNames"  value="myAdvisor"/>
    </bean>
</beans>

          4.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class SpringDemo4 {
    @Resource(name = "customerDaoProxy")
    private CustomerDao customerDao;
    @Test
    public void demo1(){
        customerDao.find();
        customerDao.delete();
    }
}

     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值