Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to @autowire into jsf managed beans

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?

like image 329
mkoryak Avatar asked Dec 15 '10 21:12

mkoryak


People also ask

Can beans be Autowired?

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.

How do you Autowire a bean?

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.

What @autowired annotation uses for bean injection?

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.

Can a spring bean be Autowired?

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.


2 Answers

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).

like image 131
Bozho Avatar answered Oct 02 '22 10:10

Bozho


You can use @ManagedProperty(#{'someBean'}) for autowire other beans in jsf bean

like image 25
Emad Aghayi Avatar answered Oct 02 '22 11:10

Emad Aghayi