gpt4 book ai didi

java jvax 注入(inject)失败并出现空点 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:08:24 24 4
gpt4 key购买 nike

我尝试在我的项目中使用依赖注入(inject)。我犯了一些错误,但我看不到我的失败。

我的项目设置:

Maven main project
|-- maven module "model"
|-- org.inject.test.model
|-- Address.java
|-- User.java
|-- org.inject.test.model.manger
|-- UserManager.java
|-- maven module "core"
|-- org.inject.test.core
|-- MyMain.java
|-- org.inject.test.core.managerImpl
|-- UserManagerImpl.java

地址.类:

package org.inject.test.model;

public class Address {

private String street;

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}
}

用户.类:

package org.inject.test.model;

import javax.inject.Inject;
import javax.inject.Named;

import org.inject.test.model.manager.UserManager;

@Named
public class User {

private String name;
private Address address;

@Inject
private UserManager usermanager;

public User (String name) {
this.name = name;
address = null;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Address getAddress() {
if (address == null) {
address = usermanager.getAddress();
}
return address;
}

public void setAddress(Address address) {
this.address = address;
}
}

用户管理器界面:

package org.inject.test.model.manager;

import org.inject.test.model.Address;

public interface UserManager {

public Address getAddress ();
}

用户管理器实现:

package org.inject.test.core.managerImpl;

import javax.inject.Named;

import org.inject.test.model.Address;
import org.inject.test.model.manager.UserManager;

@Named
public class UserManagerImpl implements UserManager {

public Address getAddress() {
// Do the DB stuff here (DB.getAddress...)
Address address = new Address();
address.setStreet("My test street");

return address;
}
}

我的主类:

package org.inject.test.core;

import javax.inject.Inject;

import org.inject.test.model.Address;
import org.inject.test.model.User;
import org.inject.test.model.manager.UserManager;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

public class MyMain {

@Inject
UserManager usermanager;

public static void main(String[] args) {
// run everything in a weld container (we need someone who manages our injects)
Weld weld = new Weld();
WeldContainer weldContainer = weld.initialize();
MyMain mymain = weldContainer.instance().select(MyMain.class).get();

mymain.printUser();
}

private void printUser() {
// Working inject
System.out.println("Working inject: " + usermanager.getAddress().getStreet() + "\n");

// Not working inject in the user class
User myuser = new User("nobody");
System.out.println("Username: " + myuser.getName() + "\n");

try {
Address address = myuser.getAddress();
String streetname = address.getStreet();
System.out.println("User Street: " + streetname + "\n");
} catch (Exception e) {
System.out.println(e + "\n");
}

}

}

这是我的小型测试项目。我的问题是 User 类中的 @Inject 不起作用。在getAddress()函数中,我得到一个java.lang.NullPointerException。但是 MyMain 类中的 @Inject 可以工作。

我已经上传了整个maven项目: http://www.file-upload.net/download-9979514/inject_test.zip.html

使用mvn clean verify运行它

输出是这样的:

[...]
556 [org.inject.test.core.MyMain.main()] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
556 [org.inject.test.core.MyMain.main()] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
Working inject: My test street

Username: nobody

java.lang.NullPointerException

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] test .............................................. SUCCESS [0.739s]
[INFO] test-model ........................................ SUCCESS [2.539s]
[INFO] test-core ......................................... SUCCESS [1.851s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

最佳答案

您的问题在这里:

User myuser = new User("nobody");

您正在创建一个注入(inject)容器不知道的新实例。

关于java jvax 注入(inject)失败并出现空点 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27407671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com