如何在.sar项目中注入CDI Bean

时间:2019-07-04 08:57:52

标签: java cdi jboss-weld mbeans

我的.ear内有一个.sar,内容如下:

  • src / main / resources / META-INF / jboss-service.xml
<server xmlns="urn:jboss:service:7.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:jboss:service:7.0 
    http://www.jboss.org/schema/jbossas/jboss-service_7_0.xsd">
    <mbean code="com.org.services.listener.MyService"
      name="com.org.services.listener:service=MyService">
    </mbean>
</server>
  • src / main / resources / META-INF / beans.xml
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.2" bean-discovery-mode="annotated">
    <!-- some content -->
</beans>
  • MyServiceMBean:
public interface MyServiceMBean {
    public void start();
    public void stop();
}
  • 我的服务:
@ApplicationScoped
public class MyService implements MyServiceMBean {

    private final CustomScopeTests customScopesThread;

    public MyService() {
        super();
        this.customScopesThread = null;
        System.out.println("This gets executed from jboss-service.xml");
    }

    @Inject
    public MyService(final CustomScopeTests customScopesTrhead) {
        this.customScopesThread = customScopesTrhead;
        System.out.println("This never gets executed");
    }

    public void start() {
        customScopesThread.startThread(); // Null pointer since it was not instantiated via CDI
    }

    public void stop() {
        if(customScopesThread != null)
            customScopesThread.stopThread();
    }
}

问题:

无论如何,我无法使jboss-service使用CDI来实例化此初始@ApplicationScoped MyService,因此,没有任何效果。

0 个答案:

没有答案