Trying to use both AspectJ and @Configurable with a Spring application.
- If I load Spring with the
@Component annotation in the class, the AspectJ wrapper works and wraps all the target methods, and the @Autowired annotation causes dependency nesting. But a class cannot be created at run time with the new keyword and have dependencies entered. - If I load the
@Configurable class without an AspectJ bean, all the dependencies are entered correctly on new , but none of the methods are proxied through AspectJ.
How can I do both?
Here is my code. Configuration:
@Configuration @ComponentScan(basePackages="com.example") @EnableSpringConfigured @EnableAspectJAutoProxy @EnableLoadTimeWeaving public class TestCoreConfig { @Bean public SecurityAspect generateSecurityAspect(){ return new SecurityAspect(); } @Bean public SampleSecuredClass createSampleClass(){ return new SampleSecuredClass(); } }
Format:
@Aspect public class SecurityAspect { @Pointcut("execution(public * *(..))") public void publicMethod() {} @Around("publicMethod()") public boolean test (ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Here!"); joinPoint.proceed(); return true; } }
SampleClass:
//@Configurable @Component public class SampleSecuredClass { @Autowired public SecurityService securityService; public boolean hasSecurityService(){ return securityService != null; } public boolean returnFalse(){ return false; } }
And unit test:
@ContextConfiguration(classes={TestCoreConfig.class}) @RunWith(SpringJUnit4ClassRunner.class) public class SecurityAspectComponentTest { @Autowired private SampleSecuredClass sampleSecuredClass; @Test public void testSecurityRoles(){
- If I get rid of beans in
TestCoreConfig and create an instance of SampleSecuredClass in the test with new and change its annotation to @Configurable , then the service will be introduced, but the aspect does not apply. - If I started, like here (by entering
SampleSecuredClass as a bean), then the aspect works and the service is entered, but then all objects must be created when the environment starts. I would like to use the @Configurable annotation. - If I use both
@Configurable and Aspect annotations together, I get an illegal type in constant pool error and the context does not start.
Other pieces of information.
I tried several different java agents - both Spring toolkit and aspectjwrapper. without changes.
If I include the aop.xml file, then this aspect works, but not @Configurable.
source share