Spring AOP AspectJ注释示例 使用@Before
使用 Java 1.8 eclipse版本:2019-06(4.12.0)
操作类别
package com.rks.beans;
public class Operation
{
public void msg()
{
System.out.println("msg method invoked");
}
public int m()
{
System.out.println("m method invoked..");
return 2;
}
public void k()
{
System.out.println("k method invoked..");
}
}
TrackOperation.java类
package com.rks.beans;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackOperation
{
@Pointcut("execusion(* com.rks.beans.Operation.*(..))")
public void k() //poincut name
{
}
@Before("k()")//applying poincut on before advice
public void myadvice(JoinPoint jp)
{
System.out.println("Addition concern");
}
}
spring.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="opBean" class="com.rks.beans.Operation"> </bean>
<bean id="trackMyBean" class="com.rks.beans.TrackOperation"></bean>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
</beans>
测试类
package com.rks.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.rks.beans.Operation;
public class Test
{
public static void main(String[] args)
{
ApplicationContext context=new ClassPathXmlApplicationContext("/com/rks/resources/spring.xml");
Operation e=(Operation)context.getBean("opBean");
System.out.println("Calling msg");
e.msg();
System.out.println("Calling m");
e.m();
System.out.println("Calling k");
e.k();
}
}
错误
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator#0' defined in class path resource [com/rks/resources/spring.xml]: Instantiation of bean failed; nested exception is java.lang.NoSuchFieldError: INSTANCE
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:892)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:408)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.rks.test.Test.main(Test.java:13)
Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.springframework.aop.aspectj.autoproxy.AspectJPrecedenceComparator.<init>(AspectJPrecedenceComparator.java:65)
at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.<clinit>(AspectJAwareAdvisorAutoProxyCreator.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:939)
... 12 more
答案 0 :(得分:0)
java.lang.NoSuchFieldError:落入2个案例中的1个案例引起的INSTANCE错误:
-没有豆子
-超过1个bean(仅1个类为2、3或4个bean)
我看到您同时使用了 spring-aop-4.3.9 和 aopalliance-1.0jar 。尝试删除 aopalliance-1.0jar 并再次运行。
另一件事看起来很像版本不匹配。您确定所有的Spring jar都是相同的版本吗?