我用Spring2.0注解释了一个类,运行时调用这个方法没反应。
@Aspect
public class LogAdvice{
private static Logger logger = Logger.getLogger(LogAdvice.class);
private static DateFormat sdf = new SimpleDateFormat(
"yyyy年MM月dd日 hh时mm分ss秒");
@Pointcut("execution(* com.datacomo.m6.business.NewMemberService.getMemberInfo(..))")
public void pointCut1(){
System.out.println("+++++++++++++++AAAAAAAAAAAAAAAAAAAAAAAA++++++++++++++++++++");
}
@Before("com.datacomo.m6.wap.util.LogAdvice.pointCut1()")
public void before(JoinPoint joinPoint){
System.out.println("前[系统日志][" + sdf.format(new Date()) + "]"
+ joinPoint.getTarget() + "("
+ Arrays.toString(joinPoint.getArgs()) + ")");
JoinPoint aJoinPoint=joinPoint;
Object[]arrayObjects=aJoinPoint.getArgs();
String sinatureName = joinPoint.getSignature().getName();
MemcachedClient mc = null;
String key = null;
StringBuffer sBuffer = new StringBuffer(sinatureName);
for (int i = 0; i < arrayObjects.length; i++) {
sBuffer.append("_");
sBuffer.append(arrayObjects[i]);
}
key = sBuffer.toString();
System.out.println(key);
Object object = null;
try {
mc = new MemcachedClient(new InetSocketAddress("192.168.1.237", 11211));
object = mc.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mc != null) {
mc.shutdown();
}
if (object != null) {
System.out.println("\n =============== 未读通知有缓存 =============== \n");
} else {
System.out.println("\n =============== 未读通知没有缓存 =============== \n");
}
}
}}
2.配置文件是:<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!--
启用Spring对@AspectJ的支持:
需要在你的应用程序的classpath中引入两个AspectJ库:aspectjweaver.jar 和 aspectjrt.jar。
这些库可以在AspectJ的安装包(1.5.1或者之后的版本)中的 lib 目录里找到,
或者也可以在Spring依赖库的 lib/aspectj 目录下找到。
另外,如果想强制使用CGLIB代理,需要将 <aop:aspectj-autoproxy> 的 proxy-target-class 属性设为true。
-->
<aop:aspectj-autoproxy/>
<!--
这个bean指向一个切面(使用了@Aspect注解的bean类)
在启用@AspectJ支持的情况下,在application context中定义的
任意带有一个@Aspect切面(拥有@Aspect注解)的bean都将被Spring自动识别并配置在Spring AOP。
-->
<bean id="logAdvice1" class="com.datacomo.m6.wap.util.LogAdvice"/>
</beans>结果调用方法没反应
@Aspect
public class LogAdvice{
private static Logger logger = Logger.getLogger(LogAdvice.class);
private static DateFormat sdf = new SimpleDateFormat(
"yyyy年MM月dd日 hh时mm分ss秒");
@Pointcut("execution(* com.datacomo.m6.business.NewMemberService.getMemberInfo(..))")
public void pointCut1(){
System.out.println("+++++++++++++++AAAAAAAAAAAAAAAAAAAAAAAA++++++++++++++++++++");
}
@Before("com.datacomo.m6.wap.util.LogAdvice.pointCut1()")
public void before(JoinPoint joinPoint){
System.out.println("前[系统日志][" + sdf.format(new Date()) + "]"
+ joinPoint.getTarget() + "("
+ Arrays.toString(joinPoint.getArgs()) + ")");
JoinPoint aJoinPoint=joinPoint;
Object[]arrayObjects=aJoinPoint.getArgs();
String sinatureName = joinPoint.getSignature().getName();
MemcachedClient mc = null;
String key = null;
StringBuffer sBuffer = new StringBuffer(sinatureName);
for (int i = 0; i < arrayObjects.length; i++) {
sBuffer.append("_");
sBuffer.append(arrayObjects[i]);
}
key = sBuffer.toString();
System.out.println(key);
Object object = null;
try {
mc = new MemcachedClient(new InetSocketAddress("192.168.1.237", 11211));
object = mc.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mc != null) {
mc.shutdown();
}
if (object != null) {
System.out.println("\n =============== 未读通知有缓存 =============== \n");
} else {
System.out.println("\n =============== 未读通知没有缓存 =============== \n");
}
}
}}
2.配置文件是:<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!--
启用Spring对@AspectJ的支持:
需要在你的应用程序的classpath中引入两个AspectJ库:aspectjweaver.jar 和 aspectjrt.jar。
这些库可以在AspectJ的安装包(1.5.1或者之后的版本)中的 lib 目录里找到,
或者也可以在Spring依赖库的 lib/aspectj 目录下找到。
另外,如果想强制使用CGLIB代理,需要将 <aop:aspectj-autoproxy> 的 proxy-target-class 属性设为true。
-->
<aop:aspectj-autoproxy/>
<!--
这个bean指向一个切面(使用了@Aspect注解的bean类)
在启用@AspectJ支持的情况下,在application context中定义的
任意带有一个@Aspect切面(拥有@Aspect注解)的bean都将被Spring自动识别并配置在Spring AOP。
-->
<bean id="logAdvice1" class="com.datacomo.m6.wap.util.LogAdvice"/>
</beans>结果调用方法没反应
import java.text.SimpleDateFormat;
import java.util.Date;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;/**
* 自动代理
* @author liyy 2009-12-08 10:32
*/
@Aspect
public class AnnotationHello {
@Pointcut("execution(void com.njtysoft.spring1x.proxy.Hello.display())")
public void liyy() {}
@Before("liyy()")
public void before() {
System.out.println("anno before: " + new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date()));
}
@After(value="liyy()")
public void after() {
System.out.println("anno after: " + new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date()));
}
}