我尝试在项目中使用依赖注入。我做错了,但我看不出我的失败。
我的项目设置:
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
Address.class:
package org.inject.test.model;
public class Address {
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
User.class:
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 ();
}
用户管理器Impl:
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;
}
}
MyMain班级:
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
。
但是@Inject
类中的MyMain
可以使用。
我上传了整个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] ------------------------------------------------------------------------
答案 0 :(得分:1)
你的问题在这里:
User myuser = new User("nobody");
您正在创建注射容器不知道的新实例。