【问题标题】:Java EE 7 CDI - Injection doesn't work, sending NullPointerExceptionJava EE 7 CDI - 注入不起作用,发送 NullPointerException
【发布时间】:2014-02-17 07:00:56
【问题描述】:

我的注射有问题,这是我第一次尝试。我正在使用 Wildfly 和 Java EE 7。在尝试访问 LoginController 中的 Authenticator 实例时,我有一个 NullPointerException

我使用 maven,我的 beans.xml 在 src/main/webapp/META-INF 下找到,但我尝试将其放入 src/main/webapp/WEB-INF/classes 没有成功。

这是我的代码:

要注入的类:

@Stateless
public class Authenticator {

    @Inject
    HashGenerator hashGenerator;

    @Inject
    UserPersistance userPersistance;

    public boolean authenticate(final String username, final String password) {
        User user = userPersistance.getUser(username);
        String salt = user.getSalt();
        String hash = hashGenerator.hash(password, salt);
        return user.getPassword().equals(hash);
    }
}

抛出 NullPointerException 的控制器:

@Stateless
public class LoginController {

    @Inject
    Authenticator authenticator;

    public void login(String username, String password) {
        if (authenticator.authenticate(username, password)) {
            UI ui = UI.getCurrent();
            ui.getSession().setAttribute(SessionAttribute.USER.getAttributeName(), username);
            ui.getNavigator().navigateTo(MainView.getName());
        }
    }
}

还有我的 beans.xml(src/main/webapp/META-INF 下)

<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"
    bean-discovery-mode="all">
</beans>

我做错了什么?


更新

对不起,我之前写错了 beans.xml 的文件夹名称。

所以,为了清楚起见,maven 会生成一个 WAR 存档,我之前曾尝试将 beans.xml 放在 META-INFWEB-INFWEB-INF/classes 下。这些文件夹似乎都无法正常工作。我已经检查(尝试过并且有效)来自 JBoss AS 7 的快速入门项目,他们将 beans.xml 放在WEB-INF 下。我也做过。

我认为这可能来自我的 Maven 配置。这里是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>be.normegil</groupId>
    <artifactId>datamanager</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>datamanager</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.7</maven.compiler.target>
        <maven.compiler.source>1.7</maven.compiler.source>        
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss</maven.build.timestamp.format>
        <vaadin.version>7.1.9</vaadin.version>
        <vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
    </properties>
    <pluginRepositories>
        <pluginRepository>
            <id>vaadin-snapshots</id>
            <url>http://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>javax.ejb-api</artifactId>
            <version>3.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-server</artifactId>
            <version>${vaadin.version}</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client-compiled</artifactId>
            <version>${vaadin.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-themes</artifactId>
            <version>${vaadin.version}</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-cdi</artifactId>
            <version>1.0.0.alpha1</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.7.1.1</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>1.0.0.Beta1</version>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • 对于 Web 应用程序,beans.xml 部署描述符必须位于 WEB-INF 目录中。对于 EJB 模块或 JAR 文件,beans.xml 部署描述符必须位于 META-INF 目录中
  • Wildfly 的 CDI 会自动发现,所以根本不需要 beans.xml?

标签: vaadin cdi java-ee-7 wildfly


【解决方案1】:

所以,我能够解决我的问题,这就是我所做的:

Maven 依赖项

我假设 javax:javaee-api 足以使用 Java EE 7 的功能。我已将依赖项添加到 javax.enterprise:cdi-apijavax.inject:javax.injectjavax.annotation:javax.annotation-api

beans.xml

这只是构建到战争档案中的一个大项目。我已经将 beans.xml 放在 src/main/webapp/WEB-INF 下(Wildfly 是否有必要拥有它?不知道)

Vaadin CDIUI

这就是解决我的问题的方法。我尝试使用 Vaadin CDIUI 没有成功。 Vaadin 实际上是使用 new 操作初始化我的 UI 类。由于它不是由容器管理的,因此每个注入点都不起作用(我首先通过在我的视图和控制器上执行new 操作来修复它,遇到了我描述的问题)

我已经重构了所有内容,因此在视图和控制器中使用了注入,并且我在我的 Vaadin UI 中添加了@CDIUI。最后,我还在我的 servlet 配置 (UIProvider) 中添加了一个新参数来获得这个结果:

@Theme("chameleon-green")
@Title("Data Manager")
@CDIUI("")
public class DataManagerUI extends UI {
    @WebServlet(value = "/*",
            asyncSupported = true,
            initParams = {@WebInitParam(
                    name = "session-timeout",
                    value = "60"
            ),@WebInitParam(
                    name = "UIProvider",
                    value = "com.vaadin.cdi.CDIUIProvider"
            )}
    )
    @VaadinServletConfiguration(productionMode = false,
            ui = DataManagerUI.class,
            closeIdleSessions = true
    )
    public static class Servlet extends VaadinServlet {
    }

    [...]
}

当通常从 Vaadin 框架使用 new 操作实例化时,使用注入时 UI 类应由 UIProvider 给出。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多