Spring AOP 之 HelloWorld(简单例子)
2015-06-28 23:36
429 查看
java代码
Java代码
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("benx.xml");
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
helloWorld.sayHelloWorld();
}
}
interface HelloWorld {
void sayHelloWorld();
}
interface HelloChina {
void sayHelloChina();
}
class HelloWorldImpl implements HelloWorld {
public void sayHelloWorld() {
System.out.println("Hello World!");
//制造异常
String str = null;
str.substring(1);
}
}
/**
* 日志拦截器
*
* @author jin.xiong
*
*/
class LogAdvice {
/**
* 执行方法前拦截器
*
* @param joinPoint
*/
public void methodBefore(JoinPoint joinPoint) {
System.out.println(joinPoint.getTarget().getClass().getName() + "."
+ joinPoint.getSignature().getName() + " Start");
}
/**
* 方法执行后拦截器
*
* @param joinPoint
*/
public void methodAfter(JoinPoint joinPoint) {
System.out.println(joinPoint.getTarget().getClass().getName() + "."
+ joinPoint.getSignature().getName() + " end");
}
/**
* 方法出现异常拦截器
*
* @param joinPoint
*/
public void methodException(JoinPoint joinPoint) {
System.out.println(joinPoint.getTarget().getClass().getName() + "."
+ joinPoint.getSignature().getName() + " mett Error");
}
/**
* 方法环绕拦截器,如果使用了这个,可以忽视上面的方法
* 注意该方法参数为ProceedingJoinPoint ,这是可以执行的,只有round可以使用
* @param joinPoint
* @return
*/
public Object methodRound(ProceedingJoinPoint joinPoint) {
methodBefore(joinPoint);
Object ob = null;
try {
ob = joinPoint.proceed();
} catch (Throwable error) {
methodException(joinPoint);
}
methodAfter(joinPoint);
return ob;
}
}
benx.xml
Java代码
<?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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="helloWorld" class="HelloWorldImpl"/>
<bean id="logAdvice" class="LogAdvice"/>
<aop:config>
<aop:aspect ref="logAdvice">
<aop:pointcut id="logPointcut" expression="execution(* *.*(..))" />
<aop:before method="methodBefore" pointcut-ref="logPointcut" />
<aop:after method="methodAfter" pointcut-ref="logPointcut" />
<aop:after-throwing method="methodException" pointcut-ref="logPointcut" />
<!-- aroun最好不要和他们同时使用 -->
<aop:around method="methodRound" pointcut-ref="logPointcut" />
</aop:aspect>
</aop:config>
</beans>
打印结果,之所以会重复,使用为使用了round,可以把benx.xml中的methodRound去掉
Java代码
HelloWorldImpl.sayHelloWorld Start
HelloWorldImpl.sayHelloWorld Start
Hello World!
HelloWorldImpl.sayHelloWorld end
HelloWorldImpl.sayHelloWorld mett Error
HelloWorldImpl.sayHelloWorld mett Error
HelloWorldImpl.sayHelloWorld end
Java代码
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("benx.xml");
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
helloWorld.sayHelloWorld();
}
}
interface HelloWorld {
void sayHelloWorld();
}
interface HelloChina {
void sayHelloChina();
}
class HelloWorldImpl implements HelloWorld {
public void sayHelloWorld() {
System.out.println("Hello World!");
//制造异常
String str = null;
str.substring(1);
}
}
/**
* 日志拦截器
*
* @author jin.xiong
*
*/
class LogAdvice {
/**
* 执行方法前拦截器
*
* @param joinPoint
*/
public void methodBefore(JoinPoint joinPoint) {
System.out.println(joinPoint.getTarget().getClass().getName() + "."
+ joinPoint.getSignature().getName() + " Start");
}
/**
* 方法执行后拦截器
*
* @param joinPoint
*/
public void methodAfter(JoinPoint joinPoint) {
System.out.println(joinPoint.getTarget().getClass().getName() + "."
+ joinPoint.getSignature().getName() + " end");
}
/**
* 方法出现异常拦截器
*
* @param joinPoint
*/
public void methodException(JoinPoint joinPoint) {
System.out.println(joinPoint.getTarget().getClass().getName() + "."
+ joinPoint.getSignature().getName() + " mett Error");
}
/**
* 方法环绕拦截器,如果使用了这个,可以忽视上面的方法
* 注意该方法参数为ProceedingJoinPoint ,这是可以执行的,只有round可以使用
* @param joinPoint
* @return
*/
public Object methodRound(ProceedingJoinPoint joinPoint) {
methodBefore(joinPoint);
Object ob = null;
try {
ob = joinPoint.proceed();
} catch (Throwable error) {
methodException(joinPoint);
}
methodAfter(joinPoint);
return ob;
}
}
benx.xml
Java代码
<?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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="helloWorld" class="HelloWorldImpl"/>
<bean id="logAdvice" class="LogAdvice"/>
<aop:config>
<aop:aspect ref="logAdvice">
<aop:pointcut id="logPointcut" expression="execution(* *.*(..))" />
<aop:before method="methodBefore" pointcut-ref="logPointcut" />
<aop:after method="methodAfter" pointcut-ref="logPointcut" />
<aop:after-throwing method="methodException" pointcut-ref="logPointcut" />
<!-- aroun最好不要和他们同时使用 -->
<aop:around method="methodRound" pointcut-ref="logPointcut" />
</aop:aspect>
</aop:config>
</beans>
打印结果,之所以会重复,使用为使用了round,可以把benx.xml中的methodRound去掉
Java代码
HelloWorldImpl.sayHelloWorld Start
HelloWorldImpl.sayHelloWorld Start
Hello World!
HelloWorldImpl.sayHelloWorld end
HelloWorldImpl.sayHelloWorld mett Error
HelloWorldImpl.sayHelloWorld mett Error
HelloWorldImpl.sayHelloWorld end
相关文章推荐
- .JavaCore/HeapDump文件及其分析方法
- 如何产生javacore和heapdump文件
- SpringMVC获取POST参数值时中文乱码
- java抓取糗事百科内容
- Android Eclipse开发环境配置
- 【Java.Core】Enum
- 18.Eclipse console 中文乱码解决 ?
- myeclipse 显示行数
- @suppressWarnings("unchecked") java 中是什么意思
- Eclipse Java注释模板设置详解
- spring装配集合
- java实现三种冒号算法的效率分析
- java中调整字体
- Spring 配置log4j和简单介绍Log4J的使用
- Java 中基本类型和包装类之间的转换
- 使用JDBC和JavaBean操作数据库
- Struts1与Struts2的区别和对比
- java 诞生的背景
- java中的匿名内部类
- 2015062812 - EffactiveJava笔记 - 第45条 将局部变量的作用域最小化