1.项目

Spring3 context:component-scan和aop:aspectj-autoproxy联合使用时,无法检测到bean

Spring3 context:component-scan和aop:aspectj-autoproxy联合使用时,无法检测到bean

package com.cl.aop;

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;

//切面类
@Component
@Aspect
public class AopLog {

	// 前置通知
	@Before("execution(* com.cl.service.UserService.add(..))")
	public void begin() {
		System.out.println("前置通知");
	}

	//
	// 后置通知
	@After("execution(* com.cl.service.UserService.add(..))")
	public void commit() {
		System.out.println("后置通知");
	}

	// 运行通知
	@AfterReturning("execution(* com.cl.service.UserService.add(..))")
	public void returning() {
		System.out.println("最终通知");
	}

	// 异常通知
	@AfterThrowing("execution(* com.cl.service.UserService.add(..))")
	public void afterThrowing() {
		System.out.println("异常通知");
	}

	// 环绕通知
	@Around("execution(* com.cl.service.UserService.add(..))")
	public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		System.out.println("环绕通知开始");
		proceedingJoinPoint.proceed();
		System.out.println("环绕通知结束");
	}
}
package com.cl.service;

public interface UserService {

	void add();
}
package com.cl.service.impl;

import org.springframework.stereotype.Service;

import com.cl.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	public void add() {
		System.out.println("add User");
	}

}

完成。

运行报错

信息: Loading XML bean definitions from class path resource [spring.xml]
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy12 cannot be cast to com.cl.service.impl.UserServiceImpl
	at com.cl.test.TestAop.main(TestAop.java:13)

修改:执行成功,bean正确初始化了,但是不能使用注解AOP

Spring3 context:component-scan和aop:aspectj-autoproxy联合使用时,无法检测到bean

Spring3 context:component-scan和aop:aspectj-autoproxy联合使用时,无法检测到bean

 于是修改:成功使用注解AOP

Spring3 context:component-scan和aop:aspectj-autoproxy联合使用时,无法检测到bean

 

相关文章: