

PrintBean.java //接口
/**
*
*/
package org.spring.aop.service;
/**
* @author
*
*/
public interface PrintBean {
public void pring(String xml) throws Exception;
public String getName(String xml) throws Exception;
}
PrintBeanImpl.java ///接口实现类
/**
*
*/
package org.spring.aop.service;
/**
* @author
*
*/
public class PrintBeanImpl implements PrintBean {
/**
* @throws Exception
*
*/
@Override
public void pring(String xml) throws Exception {
throw new Exception("这是print");
//System.out.println("打印的内容:\t"+xml);
}
/**
*
*/
@Override
public String getName(String xml) throws Exception {
throw new Exception("这是print");
//System.out.println("getName()方法");
//return "返回值"+xml;
}
}
PrintService.java //服务类
package org.spring.aop.service;
/**
*
*/
/**
* @author
*
*/
public class PrintService {
private PrintBean printBean;
public void print(String xml) throws Exception{
printBean.pring(xml);
}
public void getName(String xml) throws Exception{
printBean.getName(xml);
}
public void setPrintBean(PrintBean printBean) {
this.printBean = printBean;
}
}
CommonIntercptor.java //拦截器
/**
*
*/
package org.spring.aop.intercept;
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.transaction.annotation.Transactional;
/**
* @author
*
*/
@Aspect //声明切面
//@Component
public class CommonIntercptor {
//声明切入点
@SuppressWarnings("unused")
@Pointcut("execution (* org.spring.aop.service.PrintBeanImpl.*(..))")
private void anyMethod(){}; //
@Before("anyMethod() && args(name)")
public void beforeAdvise(String name){
System.out.println("前置通知"+name);
}
@AfterReturning (pointcut="anyMethod()",returning="name")
public void afterReturning(String name){
System.out.println("后置通知"+name);
}
@After ("anyMethod()")
public void after(){
System.out.println("最终通知");
}
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void afterThrowing(Exception e){
System.out.println("例外通知"+e.getMessage());
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕通知");
return pjp.proceed();
}
}
SpringAopTest.java //junit
/**
*
*/
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.spring.aop.service.PrintService;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 基于注解
* @author zhangcp
*
*/
public class SpringAopTest {
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void springAopTest() throws Exception{
AbstractApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
PrintService printService =(PrintService)app.getBean("printService");
printService.getName("语文");
app.close();
}
}
applicationContext.xml //spring xml
<?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-2.5.xsd ">
<!-- 启用aop自动代理 -->
<aop:aspectj-autoproxy/>
<!-- 注入拦截器 -->
<bean id="commonIntercptor" class="org.spring.aop.intercept.CommonIntercptor"/>
<bean id="printBean" class="org.spring.aop.service.PrintBeanImpl"/>
<bean id="printService" class="org.spring.aop.service.PrintService" autowire="byName">
</bean>
</beans>
Spring AOP与拦截器实现
1万+

被折叠的 条评论
为什么被折叠?



