spring使用aop 进行简单的应用
Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]:
application.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="ll"/>
<bean id="seq" class="ll.service.impl.SeImpl"/>
<bean id="pointCut" class="ll.aop.PointCut"/>
<aop:config>
<aop:aspect ref="pointCut">
<aop:pointcut id="point" expression="execution(* com.ll.service.impl.SeImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
aop:aspect>
aop:config>
<aop:aspectj-autoproxy/>
beans>
接口定义
```java
public interface Se {
public void tt();
}
实现类
public class SeImpl implements Se {
@Override
public void tt() {
System.out.println("aaa+bbbb");
}
}
切面类
//切面类
public class PointCut {
//前置通知
public void before(){
System.out.println("aaaa");
}
//后置通知
public void after(){
System.out.println("bbb");
}
}
测试类
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Se se1 =(SeImpl) context.getBean("seq");
se1.tt();
}
报错就是出现上面的内容:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation
我一开始以为是aspectjweaver 的版本不对 换了1.9几的 1.7.2 1.6.12 1.6.11 等都不行
后来仔细看个下面部分的代码 ,没有发现有啥问题 我看最后的报错是这个对象没有加到spring容器中 导致获取这个对象的时候没有找到
<aop:pointcut id="point" expression="execution(* com.ll.service.impl.SeImpl.*(..))"/>
可以帮我看看这个是哪的问题吗希望可以解决这个错误,弄了一天也没有解决
这个错误是说,Spring在创建'org.springframework.aop.aspectj.AspectJPointcut'的bean的时候发生了异常。有两个可能的原因:
1.AspectJPointcut类的定义不正确,请检查是否有语法错误
2.AspectJPointcut类的包名未在Spring配置文件component-scan标签中扫描,请确认是否包含这个包。
你这个报错应该是没有扫描到包吧,把扫描配置改成com.ll试一下
<context:component-scan base-package="com.ll" />