使用WildFly进行Spring依赖注入 [英] Spring dependency injection with WildFly

查看:182
本文介绍了使用WildFly进行Spring依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序(war文件),该应用程序依赖于(Maven)另一个使用Spring进行依赖注入的项目(jar文件)。因此,在另一个项目中,我有几个xml文件来声明我的bean(在我的情况下是业务对象)。我开始使用WildFly代替Tomcat / Jetty,并且显然有一个叫做Weld的东西负责DI。我的网络应用程序暂时不使用Spring,它只是一个简单的Jersey RESTful API。

我希望业务对象可以在资源(控制器)中注入(@Inject)。

I have a web application (war file) that depends (Maven) on another project (jar file) that uses Spring for dependency injection. So in that other project I have a couple of xml files to declare my beans, in my case business objects. I started using WildFly instead of Tomcat/Jetty and apparently there is that thing called Weld in charge of DI. My web app doesn't use Spring (for now), it's just a simple Jersey RESTful API.
I want my business objects to be injectable (@Inject) in my resources (controllers).

如何使我的bean易于访问,这意味着我们如何混合Spring DI和WildFly DI?

How do I make my beans accessible, meaning how do we mix Spring DI and WildFly DI?

现在在我的Web应用程序项目中,我有一个WEB-INF / beans.xml文件,其内容如下:

Right now in my web app project I have a WEB-INF/beans.xml file with this:

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:weld="http://jboss.org/schema/weld/beans"
    xsi:schemaLocation="
    http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd
    http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd
    bean-discovery-mode="all">
</beans>

但是当我尝试部署此代码时:

But when I try to deploy, for this code:

@Inject SomeBO myBO;

我收到此错误:


WELD-001408:类型为SomeBO的不合格依赖项,带有限定符@Default

WELD-001408: Unsatisfied dependencies for type SomeBO with qualifiers @Default

谢谢。

编辑

我需要使用xml文件导入bean,我不想注释它们,例如,我有一个bo.xml文件(Spring bean),其中包含这样的声明:

EDIT
I need to import my beans with the xml files, I don't want to annotate them, for instance I have a bo.xml file (Spring beans) that contains such declaration:

    <bean id="com.xxx.bo.SomeBO" parent="com.xxx.bo._AbstractBO">
        <property name="target">
            <bean class="com.xxx.bo.SomeBOImpl">
                <property name="DAO" ref="com.xxx.dao.SomeDAO"/>
            </bean>
        </property>
    </bean>


推荐答案

WildFly实现了Java EE Spec,这意味着它提供了一个 CDI 的实施。在WildFly的情况下,它使用的参考实现是Weld。对于Java EE 6,有一些教程应该公平地解释CDI。

WildFly implements the Java EE Spec which means it provides an implementation for CDI. In WildFly's case it uses the reference implementation which is Weld. There are some tutorials for Java EE 6 that should explain CDI fairly well.

就您的错误而言,您可能只需要使用 @Named 注释您的SomBO实现即可;或者有效的CDI批注,使其成为CDI托管bean。

As far as your error, you probably just need to annotate your your SomBO implementation with @Named or a valid CDI annotation to make it a CDI managed bean.

这篇关于使用WildFly进行Spring依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆