2

I have created a new web project from scratch using JSF 2.3 and Weld as CDI implementation running on Tomcat 9. This works all fine. Now I would like to add a dependency to a service library (Spring 4 + Hibernate 5) which implements e.g. the user authentication against a database. This is all existing code, so it cannot be modified. I have already read many articles, but unfortunately no one really helped me. I am facing the issue that I do not know how to inject the Spring-Bean (4.3.12.RELEASE) into a JSF-CDI managed bean.

@Named
@RequestScoped
public class AuthenticationController extends AbstractBean {

    @Inject
    transient private IUserService _springUserService;

    @PostConstruct
    public void postConstruct() {
        // _springUserService is null
    }
}

The service implementation class is annotated as

@Repository(IUserService.BEAN_NAME)
public class UserService implements IUserService {
 //...
}

In web.xmlI have configured the spring context loader:

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:spring-config-domain.xml
    </param-value>
</context-param>

And in faces-config.xml I have configured the Spring-EL-Resolver

<application>
  <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
  • So which annotation do I need to have the Spring Bean injected?
  • Am I missing any additional configuration for CDI in order to have it finding the Spring bean?

Thanks in advance and best regards!

Selaron
  • 6,105
  • 4
  • 31
  • 39
MichaelRS
  • 51
  • 3
  • 1
    Does this answer your question? [Injecting a Spring bean using CDI @Inject](https://stackoverflow.com/questions/4144039/injecting-a-spring-bean-using-cdi-inject) – Selaron Nov 28 '19 at 08:44
  • @Selaron Thanks for your comment, but unfortunately this does not solve the problem. During container start I still get the error message "Unsatisfied dependencies for type IUserService ...." – MichaelRS Nov 28 '19 at 09:37
  • Then you most likely made an error in code that we cannot see. The duplicate contains all relevant information – Kukeltje Nov 28 '19 at 15:14
  • Or it does not work anymor in newer versions of CDI(Weld?) where it did before looking at the comment below the answer. Or you need to have multiple producers with explicit return types and not `Object' – Kukeltje Nov 28 '19 at 15:22
  • @Kukeltje Yes, the problem was that I do not have to return an 'Object' from the producer. When I explicitly declare the return type as the type used in the Inject annotation, it works. But does this mean that I need for every spring object a separate producer method? – MichaelRS Dec 02 '19 at 14:43

0 Answers0