标签:
整个类包如下:
package com.yuan.aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class MymethodBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { // TODO Auto-generated method stub } }
package com.yuan.service; public interface UserService { public Integer add(String abc); public void aduser(String abc); }
package com.yuan.service.impl; import org.springframework.stereotype.Service; import com.yuan.service.UserService; @Service public class UserServiceimpl implements UserService { @Override public Integer add(String abc) { System.out.println("添加用户信息1。"+abc); return 123; } @Override public void aduser(String abc) { // TODO Auto-generated method stub System.out.println("添加用户信息2。"); } }
package com.yuan.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect //切面类 public class Log { /*** * 配置切入点,该方法无方法体 * */ @Pointcut("execution(* com.yuan.service..ad*(..))") public void daddff(){}; /*** * 配置前置通知 * @param joinpoint */ @Before("daddff()") public void before(){ System.out.println("添加日志"); } }
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com" /> <aop:aspectj-autoproxy/> </beans>
package com.yuan.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yuan.service.UserService; public class AopTest { public static void main(String[] args){ ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ac.getBean("userServiceimpl"); userService.add("123oscar"); //userService.aduser("456"); } }
测试结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
添加日志
添加用户信息1。123oscar
标签:
原文地址:http://my.oschina.net/u/2308739/blog/414702