spring---aop 配置
第一种:注解配置AOP
注解配置AOP(使用 AspectJ 类库实现的),大致分为三步:
1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2. 开发需要被拦截的类。
3. 将切面配置到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
@Component
public 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 staet");
}
@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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://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 Control
public 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$$7b201784
method staet
user save11d!
method AfterReturning
method after
###
注意:
@Aspect:意思是这个类为切面类
@Componet:因为作为切面类需要 Spring 管理起来,所以在初始化时就需要将这个类初始化加入 Spring 的管理;
@Befoe:切入点的逻辑(Advice)
execution…:切入点语法
第二种: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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
"><!-- 要添加最后2行 --> <context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config>
<aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))"
id="servicePointcut"/>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut-ref="servicePointcut" />
</aop:aspect> </aop:config>
</beans>
下面的<beans>是Spring的配置标签,beans里面几个重要的属性:
xmlns:
是默认的xml文档解析格式,即spring的beans。地址是http://www.springframework.org/schema/beans。
通过设置这个属性,所有在beans里面声明的属性,可以直接通过<>来使用,比如<bean>等等。
xmlns:xsi:
是xml需要遵守的规范,通过URL可以看到,是w3的统一规范,后面通过xsi:schemaLocation来定位所有的解析文件。
xmlns:aop:
这个是重点,是我们这里需要使用到的一些语义规范,与面向切面AOP相关。
xmlns:tx:
Spring中与事务相关的配置内容。
一个XML文件,只能声明一个默认的语义解析的规范。
例如上面的xml中就只有beans一个是默认的,其他的都需要通过特定的标签来使用,比如aop,它自己有很多的属性,如果要使用,前面就必须加上aop:xxx才可以。比如上面的aop:config。
类似的,如果默认的xmlns配置的是aop相关的语义解析规范,那么在xml中就可以直接写config这种标签了。
spring---aop 配置的更多相关文章
- Spring AOP配置方式
AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...
- Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理
一.看一下简单的通过XML的AOP配置 1.首先创建一个简单的Student类 public class Student { private Integer age; private String n ...
- spring aop配置文档部分翻译
欢迎转载交流: http://www.cnblogs.com/shizhongtao/p/3476973.html 下面的文字来自官方文档的翻译,具体事例以后奉上. Advisors "ad ...
- spring aop配置及用例说明(2)
欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3473362.html 这里先介绍下几个annotation的含义, @Before:表示在切入点之前执行. ...
- spring aop配置及用例说明(1)
欢迎转载交流,博客地址http://www.cnblogs.com/shizhongtao/p/3469776.html 首先,什么是aop,其实通俗一点讲就是,再方法执行时候我们加入其它业务逻辑.比 ...
- Spring——AOP配置时的jar包异常
首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...
- perf4j+spring+aop 配置 注解方式
今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单.具体配置如下: logback.xml <!--perf4j配置--> <appender name= ...
- Spring学习笔记之二----基于XML的Spring AOP配置
在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...
- Spring AOP配置与应用
1. 两种方式: a) 使用Annotation b) 使用xml 2. Annotation a) 加上对应的xsd文件spring-aop.xsd b) ...
随机推荐
- jquery的offset与position的区别
这里offset取得是屏幕影藏的y轴的距离➕元素距离屏幕的y轴的距离. 而postion取得的则是,上一个父元素(包含postion定位的)的距离
- 东大OJ-麦森数
1064: 麦森数 时间限制: 1 Sec 内存限制: 128 MB 提交: 52 解决: 9 [提交][状态][讨论版] 题目描述 形如2P-1的素数称为麦森数,这时P一定也是个素数.但反过来不 ...
- Sublime轻量级编辑器
对于从事计算机的小伙伴,好用的编辑器等效于手里的利器!可说为,砍柴不误,磨刀工! 手有神器,游走四方! sublime,记得好像是支持跨平台的 家乡的情绪 http://pan.baidu.com/s ...
- CentOS6.6搭建LNMP环境
CentOS6.6搭建LNMP环境 1.设置yum源,本地安装依赖包 1 yum -y install gcc gcc-c++ automake autoconf libtool make 2.下载依 ...
- Beta版本冲刺——day1
No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 站立式会议 培侨走的第4天,想他~( ...
- TouchSlop与VelocityTracker认识
TouchSlop是处理触摸事件中的一个常量,被系统认为滑动和点击事件的临界点.理 解这个touchSlop是一个滑动距离值的常量,也就是说当我们手触摸在屏幕上滑动时,如果滑动距离没有超过touchS ...
- mysql数据库行级锁的使用(二)
项目上的另外一个需求是: 在做统计的时候需要将当前表锁定不能更新当前表记录 直接上代码 package com.robert.RedisTest; import java.sql.Connection ...
- 【BZOJ 3879】SvT
http://www.lydsy.com/JudgeOnline/problem.php?id=3879 SvT的中文是后缀虚树? 反正本蒟蒻不懂,还是$O(nlogn)$的后缀数组和单调栈维护来做, ...
- java-Date、String、Calendar转化
1.Calendar 转化 String //获取当前时间的具体情况,如年,月,日,week,date,分,秒等 Calendar calendat = Calendar.getInstance(); ...
- Fiddler环境配置教程
原理:安装Fiddler的电脑和将要进行检测的手机(iPhone.Android)加入同一局域网,这样手机上APP的请求就可以被电脑通过Fiddler抓取到. 局域网布置教程: 在将要布置局域网的电脑 ...