当我添加注释@EnableAspectJAutoProxy时,我的项目将无法加载applicationcontext.But如果我删除@EnableAspectJAutoProxy,将不会发生异常。当然,如果我删除它,AOP不起作用。我使用spring3和junit4在我的app.Here是一些代码片段:
我的配置类:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "com.*" })
public class CenterConfigration
{}
我的测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CenterConfigration.class)
public class CenterTest
{
@Autowired
private BeanTest beanTest;
@Test
public void test()
{
System.out.println(beanTest.name);
System.out.println(beanTest.value);
System.out.println("--------");
beanTest.print();
}
我的方面代码:
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class PNNAspectHandler
{
@Around("@annotation(com.judex.annotation.ParamNotNull)")
public Object validatePNN(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
{
System.out.println("check begin...");
Method method = null;
Object[] method_args = null;
Object target = null;
String method_name = null;
target = proceedingJoinPoint.getTarget();
method_name = proceedingJoinPoint.getSignature().getName();
method = getSpecMethodByClassName(target.getClass(), method_name);
System.out.println(method.getName());
method_args = proceedingJoinPoint.getArgs();
boolean validateResult = validateArgs(method_args);
if (validateResult)
{
return proceedingJoinPoint.proceed();
} else
{
throw new RuntimeException("method:" + method_name + "err!");
}
}
public boolean validateArgs(Object[] method_args)
{
for (Object object : method_args)
{
if (object == null || "".equals(object.toString()))
{
return false;
}
}
return true;
}
public Method getSpecMethodByClassName(Class<?> clazz, String method_name)
{
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods)
{
method.setAccessible(true);
if (method.getName().equals(method_name))
{
return method;
}
}
return null;
}
}
如果我添加@ EnableAspectJAutoProxy,会发生一些异常,这里有一些关于例外的信息。
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3e6fa38a: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,centerConfigration,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0,PNNAspectHandler,beanTest,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy
十月 26, 2017 5:43:25 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@23986957] to prepare test instance [com.judex.test.CenterTest@23f7d05d]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'centerConfigration': Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.aspectj.util.LangUtil.is15VMOrGreater()Z...
Caused by: java.lang.NoSuchMethodError: org.aspectj.util.LangUtil.is15VMOrGreater()Z
答案 0 :(得分:0)
我重新配置了jar包,我解决了它!