English 中文(简体)
Injecting a Spring bean using CDI @Inject
原标题:Injecting a Spring bean using CDI @Inject

我试图在春天向土发委会管理的构成部分注入一个灯塔,但我不会成功。 养蜂没有注入,而是每当注射时就开新陈词。 我的环境是Tomcat 7和JBoss Weld。

The Spring ApplicationContext is straighforward:

<beans>
  ...
  <bean id="testFromSpring" class="test.Test" />
  ...
</bean>

土发委会管理着好像:

@javax.inject.Named("testA")
public class TestA {

  @javax.inject.Inject
  private Test myTest = null;

  ...

  public Test getTest() {
    return this.myTest;
  }

}

这是我的<条码>。

<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">
  <application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
  </application>
</faces-config>

然而,当我查阅<代码>测试财产时,从共同财产基金网页上取出一个新的<代码>。 测试例在每次进入时都制作。 这是一个简单的例子:

<html>
  ...
  <p>1: <h:outputText value="#{testFromSpring}" /></p>
  <p>2: <h:outputText value="#{testA.test}" /></p>
  ...

我获得以下产出:

1: test.Test@44d79c75
2: test.Test@53f336eb

复习:

1: test.Test@44d79c75
2: test.Test@89f2ac63

我可以看到,第一个产出是正确的。 不管我怎样经常更新网页,testFrom Spring从春天确定的星体中恢复价值。 然而,第二个产出清楚表明,每一次<代码>就都有目标。 可在<条码>测试部分上采用测试方法,新的<条码>。 测试例的创建和投放,而不是像我所期望的那样从春季开始。

因此,这种行为的原因是什么?

我怎么能把春天的灯子带入土发委会管理的灯塔?

我也利用春天确定的名称使用一个限定词,但现在有人提出例外,指出无法找到:

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Injection point has unsatisfied dependencies.  Injection point:  field test.TestA.myTest;  Qualifiers:  [@javax.inject.Named(value=testFromSpring)]

准则

@javax.inject.Named("testA")
public class TestA {

  @javax.inject.Inject
  @javax.inject.Named("testFromSpring")
  private Test myTest = null;
最佳回答

Pascal is right that you can t inject something managed by spring into a weld bean (or vice-versa).

但是,你可以界定一个生产者,他们可以带上 spring子,并带给焊接。 这种声音像一个极端的黑板、 b,我不认为你应该在一个项目中使用这两个框架。 选择一并去除。 否则,你会遇到多种问题。

在这方面,它会如何看待。

@Qualifier
@Retention(Runtime)
public @interface SpringBean {
     @NonBinding String name();
}


public class SpringBeanProducer {

    @Produces @SpringBean
    public Object create(InjectionPoint ip) {
         // get the name() from the annotation on the injection point
         String springBeanName = ip.getAnnotations()....

         //get the ServletContext from the FacesContext
         ServletContext ctx = FacesContext.getCurrentInstance()... 

         return WebApplicationContextUtils
              .getRequiredWebApplication(ctx).getBean(springBeanName);
    }
}

然后,你可以:

@Inject @SpringBean("fooBean")
private Foo yourObject;

P.S. You can make the above more category-safe. 你们不能用名字来打造,而是可以通过思考获得一般类型的注射点,并在春天看到。

问题回答

我不认为,Weld(就你的情况而言是春季的)无法管理的东西。

There s also the JBoss Snowdrop project. I don t know if it ll work with JBoss Weld on Tomcat, the documentation describes only on JBoss 5, 6 and 7. According to http://docs.jboss.org/snowdrop/2.0.0.Final/html/ch03.html#d0e618 it will inject beans declared in jboss-spring.xml into locations marked with @Spring instead of @Inject. No experience myself though, YMMV.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签