Java 无法从其他项目注入无状态bean
我的问题是,当我尝试将Maven项目A中打包在jar中的Bean注入到我的主项目中时 控制器的主要项目:Java 无法从其他项目注入无状态bean,java,maven,dependency-injection,cdi,java-ee-7,Java,Maven,Dependency Injection,Cdi,Java Ee 7,我的问题是,当我尝试将Maven项目A中打包在jar中的Bean注入到我的主项目中时 控制器的主要项目: package com.swapIt.register.controller; import java.io.Serializable; import javax.inject.Inject; import javax.inject.Named; import com.svSwapIt.JPA.com.baustein.reg.ControllerHalloWorld; import
package com.swapIt.register.controller;
import java.io.Serializable;
import javax.inject.Inject;
import javax.inject.Named;
import com.svSwapIt.JPA.com.baustein.reg.ControllerHalloWorld;
import com.swapIt.register.Model.Person;
import javax.enterprise.context.SessionScoped;
@Named
@SessionScoped
public class ControllerRegistration implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Person person;
@Inject
private ControllerHalloWorld rm;;
public Person getPerson(){
if(this.person == null){
this.person = new Person();
}
return this.person;
}
public void setPerson(Person person){
this.person = person;
}
public String nav2Danke(){
return "/contentFrame/registerDanke.xhtml";
}
public String getGreeting(){
return rm.getGreeiting();
}
}
项目A(在jar中):
我收到错误消息:
无法访问目标,标识符“controllerRegistration”解析为空
我知道这是因为@Inject注释。谁能帮帮我吗。我几乎什么都试过了。除了要在主项目中添加依赖项之外,还有什么特别的事情需要我从其他项目中注入bean吗?
My Beans XML:
<?xml version="1.0" encoding="UTF-8"?>
<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>
谢谢在你的class controllerhaloworld中,你可以添加anotation@LocalBean 在您的@inject中,您可以使用@EJB anotion。这只是一个建议,我不确定它是否正常工作,但请免费试用。
希望它能起作用。您是否在项目中添加了
bean.xml
?你能把它寄出去吗?哪个应用服务器?请回答。我使用wildlfy 8.2.1。这两个项目的META-INF中都包含Beans.xml。谢谢,我对“目标不可访问,标识符'controllerRegistration'解析为空”有点困惑。错误。这是名为的@bean,而不是hello world EJB的名称。这个错误发生在哪里?在视图上?或者在部署时?它发生在视图上,当我打开注册表时……但是当我在ControllerRegistration中删除@inject注释时,它工作正常,并且没有发生错误。我在主项目中尝试使用ManagedBean和EJB注释进行注入,在jar项目中尝试使用LocalBean。现在,我在部署过程中遇到了错误:JBAS018757:从服务模块加载器获取com.swapIt.register.controller.ControllerRegistration类的反射信息时出错,该类使用ClassLoader ModuleClassLoader为模块“deployment.swapIt app.war:main”注册
<?xml version="1.0" encoding="UTF-8"?>
<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>