您的位置:首页 > 编程语言 > Java开发

Spring 切面 AOP基础 之一

2015-05-02 13:00 435 查看
时至五月,北京的天气大大的热起来了,为了凉爽和舒适,呆在家里撸代码才是正事。光自己撸代码不乐,众撸代码才是趣事。Spring刚离开不久,我们就来温习回味一下吧。

1、从基础讲起

切面有助于实现交叉事物的模块化,交叉事物是指会影响到多个程序位置的地方。因此得采用重用技术,常见的重用就是继承和代理,切面提供了一种取代继承和委托的方式,使之更简洁。使用切面,我们仍然在一个地方定义公用功能,但是可以明确指定这个功能在哪里,以何种方式应用,而不用修改被影响的类。这样,交叉事物被模块化到特殊的对象里,这些对象被称为 切面。 这样有2个好处,一是每个事物的逻辑位于一个位置,而不是分散到多处,其次服务模块更加简洁,因为辅助事物的代码被转移到切面了。

2、从一个简单的例子讲开来

搭建spring的maven工程

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>SpringExamples</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringExamples</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>

<!-- Proxy class need this library -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>

</dependencies>
</project>


建立服务类:CustomService

public class CustomerService {
private String name;
private String url;

public void setName(String name) {
this.name = name;
}

public void setUrl(String url) {
this.url = url;
}

public void printName() {
System.out.println("Customer name : " + this.name);
}

public void printURL() {
System.out.println("Customer website : " + this.url);
}

public void printThrowException() {
throw new IllegalArgumentException();
}

}


写一个AOP的前置通知:HijackBeforeMethod

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class HijackBeforeMethod implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("HijackBeforeMethod : Before method hijacked!");
}
}


我们把前置通知配置到spring的配置文件,在里面把service和advice组装起来:Spring-Custom.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-2.5.xsd"> 
<bean id="customerService" class="CustomerService">
<property name="name" value="Yong Mook" />
<property name="url" value="http://www.baidu.com" />
</bean>

<bean id="hijackBeforeAdviceBean" class="HijackBeforeMethod"/>

<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="customerService" />

<property name="interceptorNames">
<list>
<value>hijackBeforeAdviceBean</value>
</list>
</property>
</bean>
</beans>


然后,我们在客户端调用把,首先加载这个xml文件,用代理获取服务类,调用服务类:App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mkyong.customer.services.CustomerService;

public class App {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "Spring-Customer.xml" });

CustomerService cust = (CustomerService) appContext
.getBean("customerServiceProxy");

System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {

}

}
}


附,这里讲了前置通知,其实后置通知,前后通知,异常通知都是一样的用法。写法也只有一点不同

后置通知:HijackAfterMehod.java

import java.lang.reflect.Method;
import java.util.Date;

import org.springframework.aop.AfterReturningAdvice;

public class HijackAfterMethod implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
long time = System.currentTimeMillis();
System.out.println("currentTime is:"+new Date()+" times:"+time);
}
}
前后通知:HijackAroundMethod

import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class HijackAroundMethod implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {

System.out.println("Method name : "
+ methodInvocation.getMethod().getName());
System.out.println("Method arguments : "
+ Arrays.toString(methodInvocation.getArguments()));

// same with MethodBeforeAdvice
System.out.println("HijackAroundMethod : Before method hijacked!");

try {
// proceed to original method call
Object result = methodInvocation.proceed();

// same with AfterReturningAdvice
System.out.println("HijackAroundMethod : Before after hijacked!");

return result;

} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out
.println("HijackAroundMethod : Throw exception hijacked!");
throw e;
}
}
}
异常通知:HijavaThrowException.java

import org.springframework.aop.ThrowsAdvice;

public class HijackThrowException implements ThrowsAdvice {
public void afterThrowing(IllegalArgumentException e) throws Throwable {
System.out.println("HijackThrowException : Throw exception hijacked!");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: