Java EE 7 CDI-注入不起作用,发送NullPointerException [英] Java EE 7 CDI - Injection doesn't work, sending NullPointerException

查看:80
本文介绍了Java EE 7 CDI-注入不起作用,发送NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注射有问题,这是我第一次尝试。我正在使用Wildfly和Java EE7。在尝试访问 Authenticator 实例时,出现了 NullPointerException 的情况。 LoginController



我使用Maven,我的bean.xml位于 src / main下/ webapp / META-INF ,但我尝试将其放在 src / main / webapp / WEB-INF / classes 中,但没有成功。 / p>

这是我的代码:



要注入的类:

  @Stateless 
公共类Authenticator {

@Inject
HashGenerator hashGenerator;

@Inject
UserPersistance userPersistance;

public boolean authenticate(最终字符串用户名,最终字符串密码){
User user = userPersistance.getUser(username);
字符串盐= user.getSalt();
字符串hash = hashGenerator.hash(密码,盐);
返回user.getPassword()。equals(hash);
}
}

抛出NullPointerException的控制器:

  @Stateless 
公共类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(),用户名);
ui.getNavigator()。navigateTo(MainView.getName());
}
}
}

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

 < bean 
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归档文件,而我之前曾尝试将bean.xml放在 META-INF WEB-INF WEB-INF / classes 。这些文件夹似乎都不起作用。我检查了JBoss AS 7的一个快速入门项目(尝试并运行了),他们将bean.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>提供的< / scope>
< / dependency>
< dependency>
< groupId> javax.enterprise< / groupId>
< artifactId> cdi-api< / artifactId>
< version> 1.1< / version>
< scope>提供的< / scope>
< / dependency>
< dependency>
< groupId> javax.inject< / groupId>
< artifactId> javax.inject< / artifactId>
< scope>提供的< / scope>
< / dependency>
< dependency>
< groupId> javax.annotation< / groupId>
< artifactId> javax.annotation-api< / artifactId>
< version> 1.2< / version>
< scope>提供的< / scope>
< / dependency>
< dependency>
< groupId> javax.ejb< / groupId>
< artifactId> javax.ejb-api< / artifactId>
< version> 3.2< / version>
< scope>提供的< / scope>
< / dependency>
< dependency>
< groupId> javax.transaction< / groupId>
< artifactId> javax.transaction-api< / artifactId>
< version> 1.2< / version>
< scope>提供的< / scope>
< / dependency>
< dependency>
< groupId> javax.persistence< / groupId>
< artifactId> persistence-api< / artifactId>
< version> 1.0.2< / version>
< scope>提供的< / 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>


解决方案

因此,我可以在这里解决问题我所做的就是:



Maven依赖项



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



beans.xml



这只是战争档案中的一个大型项目。我已将bean.xml放在 src / main / webapp / WEB-INF 下(是否有必要将它与Wildfly一起使用?不知道)



Vaadin CDIUI



这就是解决我的问题的方法。我尝试使用Vaadin CDIUI失败。 Vaadin实际上是使用 new 操作初始化UI类的。由于它不是由容器管理的,因此每个注入点都无法正常工作(我首先通过对视图和控制器执行 new 操作来解决该问题,我已经描述过)



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

  @Theme(变色龙绿色)
@Title(数据管理器)
@CDIUI()
公共类DataManagerUI扩展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

公共静态类Servlet扩展了VaadinServlet {
}

[...]
}

通常用<$ c实例化Vaadin f的$ c> new 操作ramework,UI类应该由 UIProvider 在使用Injection时给出。



感谢帮助! p>

I have a problem with my injection, it's the first time I try it. I'm working with Wildfly and Java EE 7. I've got a NullPointerException when trying to access Authenticator instance in LoginController.

I use maven, my beans.xml is found under src/main/webapp/META-INF but I'v tried to put it in src/main/webapp/WEB-INF/classes without success.

Here is my code :

The class to inject :

@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);
    }
}

The controller which throw 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());
        }
    }
}

And my beans.xml (Under 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>

What did I do wrong?


UPDATE

Sorry, I previously miswritten the folders name of beans.xml.

So, just to be clear, maven generate a WAR archive, and I've previously tried to put beans.xml under META-INF , WEB-INF and WEB-INF/classes. None of those folder seems to make it work. I've checked (tried and it works) a quickstart project from JBoss AS 7, and they put beans.xml under WEB-INF. I've done the same.

I think that maybe it comes from my Maven configuration. Here it is :

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

解决方案

So, I was able to fix my problem, here is what I've done :

Maven dependencies

I was assuming that javax:javaee-api was enough to use the functionnalities of Java EE 7. I've added dependencies to javax.enterprise:cdi-api, javax.inject:javax.inject and javax.annotation:javax.annotation-api

beans.xml

It's only one big project build into a war archive. I've put my beans.xml under src/main/webapp/WEB-INF (Is it necessary to have it at all with Wildfly ? Don't know)

Vaadin CDIUI

That's what solved my issue. I've tried to use Vaadin CDIUI without success. Vaadin was actually initializing my UI class with a new operation. Since it was not managed by the container, every Injection points didn't work (I've first fix it by doing new operations on my views and controllers, falling on the problem I've described)

I've refactored everything so injection is used in the views and controllers, and I've added @CDIUI to my Vaadin UI. Last, I've also added a new parameter to my servlet configuration (UIProvider) for this result :

@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 {
    }

    [...]
}

When normally instantiated with new operation from Vaadin framework, the UI class should be given by the UIProvider when using Injection.

Thanks for the help !

这篇关于Java EE 7 CDI-注入不起作用,发送NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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