【问题标题】:Can't make weld scanning beans无法制作焊缝扫描豆
【发布时间】:2016-06-07 20:50:22
【问题描述】:

我一定错过了什么,但我无法让 Weld 工作! 这是一个简单的 webapp,一个 servlet,一个服务(我想在 servlet 中注入)

这里是文件:

pom.xml

    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet-core</artifactId>
        <version>2.3.4.Final</version>
    </dependency>

context.xml

<Context>
       <Resource name="BeanManager"
          auth="Container"
          type="javax.enterprise.inject.spi.BeanManager"
          factory="org.jboss.weld.resources.ManagerObjectFactory"/>
    </Context>

我的服务

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class ServiceTest {


    public String test(){
        return "hello world";
    }


}

我的小服务程序:

public class Hello extends HttpServlet {

    @Inject
    private ServiceTest service;

    @Override
       public void doGet(HttpServletRequest request, HttpServletResponse response)
                   throws IOException, ServletException {
          // Set the response message's MIME type.
          response.setContentType("text/html;charset=UTF-8");
          // Allocate a output writer to write the response message into the network socket.
          PrintWriter out = response.getWriter();

             try {
             out.println("<!DOCTYPE html>");  // HTML 5
             out.println("<html><head>");
             out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
             String title = service.test();
             out.println("<title>" + title + "</title></head>");
             out.println("<body>");
             out.println("<h1>" + title + "</h1>");  // Prints "Hello, world!"
             out.println("</body></html>");
          } finally {
             out.close();  // Always close the output writer
          }
       }
}

我只是得到一个 NPE ......没有别的。

这是我的 Tomcat 7 的启动轨迹

2016-06-07 22:49:27 DEBUG logging:37 - Logging Provider: org.jboss.logging.Log4jLoggerProvider
2016-06-07 22:49:27 INFO  servletWeldServlet:57 - WELD-ENV-001008: Initialize Weld using ServletContainerInitializer
2016-06-07 22:49:27 INFO  Version:153 - WELD-000900: 2.3.4 (Final)
2016-06-07 22:49:27 DEBUG Bootstrap:121 - WELD-ENV-000030: Cannot load class using the ResourceLoader: org.jboss.jandex.Index
2016-06-07 22:49:27 DEBUG Bootstrap:121 - WELD-ENV-000030: Cannot load class using the ResourceLoader: org.jboss.jandex.Index
2016-06-07 22:49:27 DEBUG Bootstrap:316 - WELD-ENV-000024: Archive isolation enabled - creating multiple isolated bean archives if needed
2016-06-07 22:49:27 INFO  Bootstrap:166 - WELD-ENV-000028: Weld initialization skipped - no bean archive found
juin 07, 2016 10:49:27 PM org.apache.coyote.AbstractProtocol start
INFOS: Starting ProtocolHandler ["http-bio-8080"]
juin 07, 2016 10:49:27 PM org.apache.coyote.AbstractProtocol start
INFOS: Starting ProtocolHandler ["ajp-bio-8009"]
juin 07, 2016 10:49:27 PM org.apache.catalina.startup.Catalina start
INFOS: Server startup in 1127 ms

beans.xml(在 META-INF 下)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee">
    <scan>

    </scan>
</beans>

【问题讨论】:

  • 请使用 NPE 发布您的堆栈跟踪。
  • 你有 beans.xml 吗?
  • NPE 没有提供任何信息,只是调用了注入的空对象。
  • 我已经添加了 META-INF 中的 beans.xml
  • Where 你读过应该将beans.xml 放在WAR 档案的/META-INF 中吗?

标签: tomcat servlets cdi weld


【解决方案1】:

2016-06-07 22:49:27 INFO Bootstrap:166 - WELD-ENV-000028:焊接初始化已跳过 - 未找到 bean 存档

这意味着 Weld 在您的 WAR 中没有找到任何 bean 存档。请注意,在 WAR 中,beans.xml 必须命名为 WEB-INF/beans.xmlWEB-INF/classes/META-INF/beans.xml(另请参阅规范 12.1. Bean archives)。我猜你有META-INF/beans.xml

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多