Spring知识点(使用注解实现5种通知)
-
组件的注解(使用这个注解可以向容器注册bean)
- repository
- service
- controller
- component
-
导入jar
建立一个dao包
package dao;
public interface SayDao {
String SayAWord();
}
建立daoimpl包
package daoimpl;
import org.springframework.stereotype.Repository;
import dao.SayDao;
//往applicationContent注册bean
@Repository("say")
public class Say implements SayDao {
public String SayAWord() {
return "你好aop";
}
}
建立Service包
package service;
import org.springframework.stereotype.Service;
public interface PeopleDao {
String PeopleSay();
}
建立ServiceImpl包
package serviceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.SayDao;
import daoimpl.Say;
import service.PeopleDao;
@Service("people")
public class People implements PeopleDao {
@Autowired
private SayDao say;
public String PeopleSay() {
return this.say.SayAWord();
}
public SayDao getSay() {
return say;
}
public void setSay(Say say) {
this.say = say;
}
}
建立测试类:
package test;
import java.lang.reflect.Proxy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.PeopleDao;
import serviceimpl.People;
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContent.xml");
PeopleDao people=(PeopleDao)applicationContext.getBean("people");
System.out.println(people.PeopleSay());
}
}
注意看关键的一句
//使用这用写法的aop会使用Jdk原生的动态代理
PeopleDao people=(PeopleDao)applicationContext.getBean("people");
//产生一个代理类
PeopleDao people1=(PeopleDao)Proxy.newProxyInstance(test.class.getClassLoader(), new Class[] {PeopleDao.class} ,new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("死");
return null;
}
});
people1.PeopleSay();
另外一种写法
//使用这用写法的aop会使用CGLIB代理
People people=(People)applicationContext.getBean("people");
关于@Asprect注解的使用
package aop;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
//声明是切面
@Aspect
public class UseServiceLog {
private Logger logger=Logger.getLogger(UseServiceLog.class);
//织入前置通知
@Before("execution(public String PeopleSay())")
public void before(JoinPoint p) {
logger.info("-----调用了"+p.getTarget()+"的"+p.getSignature().getName()+"方法");
System.out.println("前置");
}
/* @After("execution(public String PeopleSay())")
public void after(JoinPoint p) {
logger.info("-----调用了"+p.getTarget()+"的"+p.getSignature().getName()+"方法");
System.out.println("后置");
}
@AfterReturning("execution(public String PeopleSay())")
public void afterreturn(JoinPoint p,Object result) {
logger.info("-----调用了"+p.getTarget()+"的"+p.getSignature().getName()+"方法"
+result.toString()
);
System.out.println("返回通知");
}
@AfterThrowing("execution(public String PeopleSay())")
//添加异常通知方法
public void afterthrowing(JoinPoint p,RuntimeException exception) {
logger.info("-----调用了"+p.getTarget()+"的"+p.getSignature().getName()+"方法"
+exception.toString()
);
System.out.println("异常通知");
}
@Around("execution(public String PeopleSay())")
//环绕通知
public Object around(ProceedingJoinPoint pjd) {
Object result = null;//返回值
String methodName = pjd.getSignature().getName();//得到方法名
try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
result = pjd.proceed();//执行方法 //返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
//异常通知
System.out.println("The method " + methodName + " occurs exception:" + e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}*/
}