SpringMVC使用注解配置AOP
来源:互联网 发布:为什么会有淘宝客 编辑:程序博客网 时间:2024/05/17 23:56
注解配置AOP(使用 AspectJ 类库实现的),大致分为三步:
- 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
- 开发需要被拦截的类。
- 将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式。这样的话,那就交由Spring AoP容器管理。
- 另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar
User.java
package com.bjsxt.model;public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
/***接口类*/package com.bjsxt.dao;import com.bjsxt.model.User;public interface UserDAO { public void save(User user);}
实现接口:
package com.bjsxt.dao.impl;import org.springframework.stereotype.Component;import com.bjsxt.dao.UserDAO;import com.bjsxt.model.User;@Component("u")public class UserDAOImpl implements UserDAO { public void save(User user) { System.out.println("user save11d!"); /*throw new RuntimeException("exception");*/ //抛异常 }}
操作类:
package com.bjsxt.service;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;import com.bjsxt.dao.UserDAO;import com.bjsxt.model.User;@Component("userService")public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Resource(name="u") public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); }}
加入aop
package com.bjsxt.aop;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Aspect@Componentpublic class LogInterceptor { @Pointcut("execution(public * com.bjsxt.service..*.add(..))") public void myMethod(){};/*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")*/ @Before("myMethod()") public void before() { System.out.println("method start"); } @After("myMethod()") public void after() { System.out.println("method after"); } @AfterReturning("execution(public * com.bjsxt.dao..*.*(..))") public void AfterReturning() { System.out.println("method AfterReturning"); } @AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))") public void AfterThrowing() { System.out.println("method AfterThrowing"); } }
配置文件
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsd"><!-- 要添加最后2行 --> <context:annotation-config /> <!-- 自动扫描 --> <context:component-scan base-package="com.bjsxt"/> <!-- 要添加本行 --> <aop:aspectj-autoproxy/> </beans>
测试类:
package com.bjsxt.service;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bjsxt.model.User;//Dependency Injection//Inverse of Controlpublic class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService service = (UserService)ctx.getBean("userService"); System.out.println(service.getClass()); service.add(new User()); System.out.println("###"); ctx.destroy(); }}
结果:
class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784method staetuser save11d!method AfterReturningmethod after
注意:
- @Aspect:意思是这个类为切面类
- @Componet:因为作为切面类需要 Spring 管理起来,所以在初始化时就需要将这个类初始化加入 Spring 的管理;
- @Before:切入点的逻辑(Advice)
- execution…:切入点语法
此内容转载自 偶my耶 的博客:
http://www.cnblogs.com/oumyye/p/4480196.html
如需转载请注明出处!
0 0
- SpringMVC使用注解配置AOP
- SpringMVC 使用 AOP注解
- 使用注解配置spring aop
- 使用注解配置Spring AOP
- 天易47----Springmvc注解方式实现aop配置简单示例
- 【Spring五】AOP之使用注解配置
- 使用注解配置spring中的aop
- Spring学习之使用注解配置AOP
- SpringMVC中使用aop注解无效的问题
- SpringMVC中使用aop注解无效的问题
- SpringMVC使用注解方式配置IOC
- (推荐使用)SpringMVC注解,基本配置
- AOP注解配置
- Spring AOP注解配置
- Spring AOP 注解配置
- Spring---AOP(注解配置)
- 注解配置AOP
- spring aop注解配置
- 学习IOS的第五天
- 拓扑排序
- Spring源码剖析——核心IOC容器原理
- Effective Java 学习笔记(四、五)
- 160.leetcode Intersection of Two Linked Lists(easy)[链表 公共节点]
- SpringMVC使用注解配置AOP
- 多线程-互斥锁
- 利用jQuery中的val()属性完成默认登陆框
- uva11419 SAM I AM
- 从源码角度分析嵌套滑动机制NestedScrolling
- 关于打开应用白屏或黑屏后进入引导页的问题
- Incorrect string value: '\xF0\x9F\x91\x93\xF0\x9F...' for column 'xxx' at row 1
- MySQL表分区(3)哈希分区-hash
- Android QQ、微信聊天消息界面设计原理与实现