- package org.advice;
- import java.lang.reflect.Method;
- import org.springframework.aop.MethodBeforeAdvice;
- public class logBeforeAdvice implements MethodBeforeAdvice{
- public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
- System.out.println("方法前写日志"+arg2.toString());
- }
- }
- ---------------------------------------
- package org.advice;
- import java.lang.reflect.Method;
- import org.springframework.aop.AfterReturningAdvice;
- public class logAfterAdvice implements AfterReturningAdvice{
- public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
- System.out.println("方法调用后开始写日志="+arg0);
- }
- }
- ---------------------------------------------
- package org.advice;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- public class logAdvice implements MethodInterceptor{
- public Object invoke(MethodInvocation arg0) throws Throwable {
- //arg0.getMethod() //得到方法
- // arg0.getArguments(); //得到参数值
- // arg0.getStaticPart(); //得到静态方法;
- // arg0.getThis(); //得到 targer 对象
- System.out.println("方法之前写日志。。。");
- Object o=arg0.proceed(); //执行方法;
- System.out.println("方法后写日志。。。");
- return o;
- }
- }
- -----------------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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans spring-beans-2.0.xsd">
- <bean name="UserDao" class="org.dao.UserDao"></bean>
- <bean name="DeptDao" class="org.dao.DeptDao"></bean>
- <bean name="beforAfter" class="org.advice.logAdvice"/>
- <bean name="after" class="org.advice.logAfterAdvice"/>
- <bean name="before" class="org.advice.logBeforeAdvice"/>
- <!--自动匹配-->
- <bean name="autoproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <list>
- <value>*Dao</value> <!-- 以dao结尾的注册bean的name -->
- </list>
- </property>
- <property name="interceptorNames">
- <list>
- <!--<value>before</value>-->
- <value>regpx</value><!-- 正则表达式 -->
- </list>
- </property>
- </bean>
- <!--代理工厂 好用-->
- <bean name="daoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces">
- <list>
- <value>org.IDao</value>
- </list>
- </property>
- <property name="target" ref="UserDao"></property>
- <property name="interceptorNames">
- <list>
- <value>regpx</value> <!-- 用正则表达式的灵活 -->
- <!-- <value>before</value>-->
- <!-- <value>after</value>-->
- <!-- <value>beforAfter</value>-->
- </list>
- </property>
- </bean>
- <!--指定方法名太挨次了 不采用 -->
- <!-- <bean name="namematch" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">-->
- <!-- <property name="advice" ref="before"></property>-->
- <!-- <property name="mappedNames">-->
- <!-- <list>-->
- <!-- <value>save</value>-->
- <!-- </list>-->
- <!-- </property>-->
- <!-- </bean>-->
- <!-- -->
- <!-- 正则表达式-->
- <bean name="regpx" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
- <property name="advice" ref="after"></property>
- <property name="patterns">
- <value>.*up.*</value>
- </property>
- </bean>
- </beans>
- -------------------------IDao 接口 中代码 ----------------
- /**
- *
- */
- package org;
- /**
- * @author Administrator
- *
- */
- public interface IDao {
- public void save(Object o) throws Exception;
- public void update(Object o) throws Exception;
- public void delete(Object o)throws Exception;
- }
- ------------------- Dao 中的 代码-----------------------------------
- package org.dao;
- import org.IDao;
- public class UserDao implements IDao {
- public void delete(Object o) throws Exception {
- System.out.println("删除用户。。。");
- }
- public void save(Object o) throws Exception {
- System.out.println("新增用户。。。");
- }
- public void update(Object o) throws Exception {
- System.out.println("修改用户资料。。。");
- }
- }
- ---------------------------测试 中的 代码-----------------------
- package org.test;
- import org.IDao;
- import org.dao.UserDao;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class testLogAdvice {
- public static void main(String[] args) throws Exception {
- testLogAdvice tla= new testLogAdvice();
- tla.before();
- System.out.println("---------------");
- tla.sysAdvice();
- }
- public void before() throws Exception{
- ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
- //前后 中 都可以在这里调度
- IDao dao=(IDao)ac.getBean("daoProxy");
- dao.save("保存动作");
- dao.update("baocun");
- }
- public void sysAdvice() throws Exception{
- //autoproxy
- ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
- IDao dao=(IDao)ac.getBean("UserDao");
- dao.save(null);
- dao.update("sssssssssssss");
- }
- }
spring AOP 部分代码
最新推荐文章于 2024-07-25 16:47:20 发布