标签:runtime scan blog extend 问控制 icp instance XML 执行
一、applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="personService" class="com.lovo.u34.service.impl.PersonServiceImpl" ></bean>
<bean id="myInterceptor" class="com.lovo.u34.service.MyInterceptor"></bean>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
二、PersonServiceImpl
public class PersonServiceImpl implements PersonService{
@Override
public void save(String name) {
System.out.println("我是save方法"+name);
//throw new RuntimeException("我爱例外");
}
@Override
public void update(String name, Integer personid) {
System.out.println("我是update方法");
}
@Override
public String getPersonName(Integer personid) {
System.out.println("我是getPersonName方法");
return "xxx";
}
}
三、MyInterceptor
@Aspect //切面
public class MyInterceptor {
@Pointcut("execution (* com.lovo.u34.service.impl.PersonServiceImpl.*(..))")
public void anyMethod (){ //声明一个切入点
}
@Before("anyMethod() && args(name)") //打印传入切入点的name
public void doAccessCheck(String name){
System.out.println("before前置方法"+"---"+name);
}
@After("anyMethod()")
public void doAfter(){
System.out.println("after后置方法");
}
@AfterReturning(pointcut="anyMethod()",returning="result") //打印返回值
//@AfterReturning(pointcut="anyMethod()")
public void doAfterCheck(String result){
System.out.println("afterReturning方法正常执行完毕之后执行"+"----"+result);
}
@AfterThrowing(pointcut="anyMethod()",throwing="e") //打印异常
public void doAfterThrow(Exception e){
System.out.println("例外通知"+"----"+e);
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("进入环绕通知");
Object object = pjp.proceed();//执行该方法
System.out.println("退出环绕方法");
return object;
}
}
方法访问控制修饰符 返回类型 包路径.类名.方法名(参数类型1,参数类型2。。。。)
1: public * com.lovo.daoimpl.UserDaoImpl.log(java.lang.String)
这句里面有一个*代表通配符,就是说这方法可以是任意的返回类型,参数里面呢,有一个String的类型,表示方法是一个带一个String类型的参数
2: * com.lovo.daoimpl.UserDaoImpl.log(java.lang.String,java.lang.String)
表示是一个任意修饰符,任意返回类型的,并且参数有两个String的方法
3: * com.lovo.daoimpl.UserDaoImpl.log(..)
表示是一个任意修饰符,任意返回类型的,并且参数也是任意个数与类型的方法。
4: * *.. UserDaoImpl.*(..)
表示任意修饰符,任意返回类型,任意包下面的所有UserDaoImpl类的所有的方法
注意,前面那个*..的两个点表示包的所有子包的意思。
5: * *..*.*(..)
表示 所有包下面的所有类的所有方法
四、Test
public class Test extends TestCase{
public void testSave(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService ps=(PersonService) ac.getBean("personService");
//ps.save("张三");
ps.getPersonName(12);
}
}
标签:runtime scan blog extend 问控制 icp instance XML 执行
原文地址:http://www.cnblogs.com/syj1993/p/7142429.html