Spring dependency injection with WildFly

2019-07-07 05:09发布

问题:

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

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

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;

I get this error:

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

Thanks.

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>

回答1:

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.

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.