Note that there are some explanatory texts on larger screens.

plurals
  1. POInjecting a Spring bean using CDI @Inject
    primarykey
    data
    text
    <p>I'm trying to inject a bean defined in a Spring context into a CDI managed component but I'm not successful. The bean is not injected, instead a new instance gets created each time the injection should be performed. My environment is Tomcat 7 with JBoss Weld.</p> <p>The Spring ApplicationContext is straighforward:</p> <pre><code>&lt;beans&gt; ... &lt;bean id="testFromSpring" class="test.Test" /&gt; ... &lt;/bean&gt; </code></pre> <p>The CDI managed bean looks like this:</p> <pre><code>@javax.inject.Named("testA") public class TestA { @javax.inject.Inject private Test myTest = null; ... public Test getTest() { return this.myTest; } } </code></pre> <p>This is my <code>faces-config.xml</code></p> <pre><code>&lt;faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"&gt; &lt;application&gt; &lt;el-resolver&gt;org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;/el-resolver&gt; &lt;/application&gt; &lt;/faces-config&gt; </code></pre> <p>However, when I access the <code>test</code> property from within a JSF page, a new <code>Test</code> instance is being created each time the access occurs. This is a simple example:</p> <pre><code>&lt;html&gt; ... &lt;p&gt;1: &lt;h:outputText value="#{testFromSpring}" /&gt;&lt;/p&gt; &lt;p&gt;2: &lt;h:outputText value="#{testA.test}" /&gt;&lt;/p&gt; ... </code></pre> <p>I get the following output:</p> <pre><code>1: test.Test@44d79c75 2: test.Test@53f336eb </code></pre> <p>After a refresh:</p> <pre><code>1: test.Test@44d79c75 2: test.Test@89f2ac63 </code></pre> <p>I can see that the first output is correct. No matter how often I refresh the page, the <code>testFromSpring</code> returns the value from the bean defined in the Spring context. However the second output clearly shows that each time the <code>getTest</code> method on the <code>test</code> components is invoked, a new <code>Test</code> instance is created and injected instead of using the instance from the Spring context as I would expect.</p> <p>So, what's the reason for this behaviour? </p> <p>How can I inject the bean from the Spring context into the CDI managed bean?</p> <p>I also tried using a qualifier using the name defined in the Spring context, but now an exception is thrown indicating, that the bean cannot be found:</p> <pre><code>org.jboss.weld.exceptions.DeploymentException: WELD-001408 Injection point has unsatisfied dependencies. Injection point: field test.TestA.myTest; Qualifiers: [@javax.inject.Named(value=testFromSpring)] </code></pre> <p>for the code</p> <pre><code>@javax.inject.Named("testA") public class TestA { @javax.inject.Inject @javax.inject.Named("testFromSpring") private Test myTest = null; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload