last day (15 days later) » 

6:21 PM
0
Q: Not able to Autowire Spring Bean in Spring AOP aspect

saysivaI have a new Aspect created and its not loading the autowired bean @Aspect @Component public class LoggingAspect { private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class); @Autowired private EmployeeService employeeService; @Autowired private EmployeeDAO employeeDAO...

 
How have you declared the beans? Code?
 
All the beans are declared through annoation.
@Component public class EmployeeDAOImpl implements EmployeeDAO {
 
Does your component-scan includes the packages where these annotations are?
 
Both the bean and Aspect are in same module and using the same context file to do component scan
//@Configuration //@ComponentScan(basePackages={"com.test.example"}) //@EnableAspectJAutoProxy //@EnableLoadTimeWeaving public class SampleAppContext { }
 
Try changing the annotation Component in the EmployeeDAOImp to this @Component ("EmployeeDAO")
 
6:21 PM
Still I'm facing problem in getting that DAO as null. Is there POM dependency that I need to add??
 
That should be no problem, as long as you don't have any exception in the logs when you startup the server. It's a context problem.
 
This code is of simple and straight forward. What could be the issue??
 
Have you checked that no exceptions where thown during the startup?
 
No exception is thrown during startup... I'm using Maven for loading all the dependencies..
 
You could try to open an instance of the context and set the bean yourself to check that the problem is with the @Aspect annotated class.
 
6:26 PM
I tried getting the context again and able to get the bean. Its working fine.
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(SampleAppContext.class);
appContext.refresh();
 
I'm using annotated based coding.
Using @EnableAspectJAutoProxy in context loading class.. How to include the required beans through annotation for the same??
 
6:47 PM
I'm not sure, I'm used to the xml. Sorry.
Another thing that I can think of is adding @Configurable to the class.
And instead of @Component in the DAO you should use @Repository
 

last day (15 days later) »