In order to use the @Autowire annotation, the object where you use the annotation must come from the spring context.
JSF managed beans are created by JSF's IOC not Springs, therefor i cannot use @Autowire inside of them must must use faces-config.xml and managed properties.
I already setup an EL resolver that lets be have spring beans as managed properties, i want to take it one step further and get rid of the need to go into the faces-config.xml every time i need to autowire something. Is this possible?
In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file.
Enabling @Autowired annotation By declaring beans, you provide metadata to the Spring Container to return the required dependency object at runtime. This is called Spring Bean Autowiring. In java based configuration, all the bean methods are defined in the class with @configuration annotation.
The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.
Enabling @Autowired Annotations The Spring framework enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, Spring container can autowire relationships between collaborating beans. This is called Spring bean autowiring.
Just annotate your managed beans with @Controller
(or @Component
), and @Scope("request")
(or session
) and add <context:component-scan>
(if you haven't), and managed beans will automatically be detected as spring beans. And since you are already using the ELResolver, that should be it - you should be able to use @Autowired
(or better - @Inject
, if using spring 3.0).
You can use @ManagedProperty(#{'someBean'}) for autowire other beans in jsf bean
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With