Hello,
What is the proper way to inject the UserService and UserCache spring beans via spring annotations? This question: addresses it using xml configuration, but how does this translate to annotations?
Are there contexts I cannot inject the UserService and UserCache objects into my code? Are there best practices? I'm experiencing some inconsistency with injecting the activiti provided beans. I am able to inject the bean into a class that implements BootstrapConfigurer (as a private autowired instance member variable) but not in a class that lies within the com.actviti.extension.bean class (attempting to inject as as an autowired, protected member variable). The class is annotated with @Service but does not extend or implement any classes.
Any help would be appreciated.
There are two main annotations that prove useful when wiring services via annotations: @Inject and @Autowired. While they generally function in about the same manner, there are subtle differences. These two annotations have different origins, @Inject being based on JSR-330 - a standard for Java dependency injection - and @Autowired is a Spring proprietary annotation to have similar functionality. The main difference between the two is that @Inject does not have the ability to toggle whether a wiring is required or not - so on failure, an exception will be thrown.
In order to allow for annotations without following the XML method that is showed in the link given, I would recommend looking into the @ComponentScan annotation. When used in configuration files in the initialization of your project, @ComponentScan will search the packages listed in its declaration to find certain annotations and register the class as a valid bean. Those annotations are: @Repository, @Service, @Controller, @Configuration, @Component. This tutorial/walkthrough gives a fantastic example, with identical configurations done within XML to compare, of utilization @ComponentScan & other annotations in order to auto-wire classes.
I believe the issue you mentioned about problems autowiring things in certain locations probably has to do with where the components are being registered and not registered - but I'm not entirely certain based on what I know about the situation.
Honestly, I don't have any recommendations for best practices utilizing dependency injection; the internet doesn't seem to have many thoughts either. The general consensus seems to be that you shouldn't 'abuse' it - but it's there, and it works, and if constraints are necessary, those can be configured.
Jonathan Earles - thank you for your detailed response. Oddly enough, this appeared to be half an an eclipse issue and half a spring configuration issue. Before adding my containing package to the @ComponentScan, I added @Service annotations to the classes I wished to inject. After building the project and deploying locally to my tomcat instance within eclipse, the UserCache and UserService were not getting injected as expected. After completing the following steps, they were injected successfully:
While adding my package to @ComponentScan was not necessary (I suspect it is added elsewhere in this app) in my case, the @Service annotations were necessary to make spring aware of the objects I needed to inject.
Hope this helps someone else.
Ask for and offer help to other Alfresco Process Services and Activiti Users and members of the Alfresco team.
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.