美文网首页
2018-02-07-0.spring配置aop编程

2018-02-07-0.spring配置aop编程

作者: 简单coder | 来源:发表于2018-02-07 11:46 被阅读4次

aop基础配置

注册对象必须经过spring-config.xml这种文件的读取,可以使用注解配置(本人尚未成功,可能少了一些jar包)

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.itcast.bean.User;
import cn.itcast.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/d_springaop/applicationContext.xml")
public class Demo {
    @Resource(name="userService")
    private UserService us;
    
    @Test
    public void fun1(){
        us.save();
    }
    
}

我用的是servlet模拟了网络请求,为了拿到配置文件对象

protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        ServletContext servletContext = request.getServletContext();
        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        UserService us = (UserService) ac.getBean("userService");
        us.hello();
//        User user = (User) ac.getBean("user");

//        System.out.println(user);
    }

aop config文件配置如下

<?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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
    <bean name="userService" class="cn.itcast.user.service.UserService"></bean>
    <bean name="Advice" class="cn.itcast.user.service.UserServiceAdvice"></bean>
    <context:component-scan base-package="cn.itcast.user"></context:component-scan>

    <aop:config>
        <aop:pointcut id="pc" expression="execution(* cn.itcast.user.service.UserService.*(..))"></aop:pointcut>
        <aop:aspect ref="Advice">
            <aop:before method="before" pointcut-ref="pc"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

结果如下

使用aop注解编程

配置文件xml中:

xsi:schemaLocation添加

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
<aop:aspectj-autoproxy expose-proxy="true"></aop:aspectj-autoproxy>

通知类中:

    @Component("UserServiceAdvice")
    @Aspect
public class UserServiceAdvice {

    @Pointcut("execution(* cn.itcast.user.service.UserService.*(..))")
    public void pc(){}

    @Before("UserServiceAdvice.pc()")
    public void before() {
        System.out.println("我是before");
    }
}

目标对象中:

@Component("userService")
public class UserService {

    public void hello() {
        System.out.println("hello啊");
    }
}

附:环绕通知有点特殊

//环绕通知
    @Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }

相关文章

网友评论

      本文标题:2018-02-07-0.spring配置aop编程

      本文链接:https://www.haomeiwen.com/subject/gfuozxtx.html