Java 实体中的SpringRoo服务自动连接始终为空(使用带有@RooJPAEEntity的JPA存储库)
我一直在寻找一种在保存实体时从服务调用某个方法的方法 我的应用程序是使用Roo1.2.4.0版本创建的 我有一个叫做SaldoCliente的平衡实体和一个叫做AuxCliente的ClientAction实体 每次新的ClientAction实体被持久化时,我都需要更新客户端余额 这是我的代码:Java 实体中的SpringRoo服务自动连接始终为空(使用带有@RooJPAEEntity的JPA存储库),java,spring,jpa,spring-roo,autowired,Java,Spring,Jpa,Spring Roo,Autowired,我一直在寻找一种在保存实体时从服务调用某个方法的方法 我的应用程序是使用Roo1.2.4.0版本创建的 我有一个叫做SaldoCliente的平衡实体和一个叫做AuxCliente的ClientAction实体 每次新的ClientAction实体被持久化时,我都需要更新客户端余额 这是我的代码: @RooJavaBean @RooToString @RooJpaEntity(entityName = "AUX_CLIENTES") public class AuxCliente {
@RooJavaBean
@RooToString
@RooJpaEntity(entityName = "AUX_CLIENTES")
public class AuxCliente {
@Transient
@Autowired
static private SaldoClienteService saldoClienteService;
...
@PostConstruct
public void init()
{
System.out.println("Initializing with dependency ["+ saldoClienteService + "]");
}
@PostPersist
private void afectaSaldoCliente(/*Long idTrans, Cliente, Integer cargo, BigDecimal importe, Integer creditos*/) {
if (saldoClienteService == null) {
System.out.println("saldoClienteService FUE NULL");
}
...
我不知道为什么saldoClienteService总是空的
注意,我不希望在我的数据库中保存saldoClienteService字段,因此使用@Transient注释
我一直在寻找解决办法,但没有成功。许多解释都说where这样说:您需要或启用@PostConstruct处理
我的applicationContext.xml中有Roo创建的
文件说:
默认情况下,Spring提供了@Component、@Repository、@Service和@Controller原型
将被检测到。注意:此标记表示“annotation config”标记的效果,激活
@必需、@Autowired、@PostConstruct、@PreDestroy、@Resource、@PersistenceContext和
@组件类中的PersistenceUnit注释,这通常是自动检测所需要的
没有外部配置的组件
至少@Autowired注释在除此之外的任何地方都有效
有人有什么建议吗
---------编辑---------
首先:我要感谢@Sotirios和@Ralph花时间帮助我
如果我从字段中删除static,它是相同的。在我的实体中,注入的字段始终为空。看到我在这个问题上的评论,我补充说,因为一个可能的解决方案
我在另一门课上也遇到了麻烦,我需要注射。我将其添加到与AuxClient之前相同的类中:
@Transient
@Autowired
private ConfigUser configUser;
并且configUser也始终为null
这是另一门课的开始,以防万一
@RooJavaBean(settersByDefault=false)
public class ConfigUser {
...
当然,在applicationContext.xml中:
<bean class="com.i4b.adminctes.util.ConfigUser" id="appConfigUser" />
编辑服务:
package org.example.service;
public class MyEntityServiceImpl implements MyEntityService {
@Override
public String testAutowire() {
return "Some data";
}
}
编辑实体:
package org.example.domain;
import javax.persistence.PrePersist;
import javax.persistence.Transient;
import org.example.service.MyEntityService;
import org.example.service.MyEntityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.entity.RooJpaEntity;
import org.springframework.roo.addon.tostring.RooToString;
@RooJavaBean
@RooToString
@RooJpaEntity
public class MyEntity {
@Transient
@Autowired
MyEntityService myEntityService;
/**
*/
private String text;
@PrePersist
public void prePersist() {
if (myEntityService == null) {
System.out.println("myEntityService IS NULL");
} else {
String data=myEntityService.testAutowire();
System.out.println("it works: " + data);
this.text = data;
}
}
}
并编辑create.jspx以隐藏服务字段。否则它不会让你节省
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<jsp:output omit-xml-declaration="yes"/>
<form:create id="fc_org_example_domain_MyEntity" modelAttribute="myEntity" path="/myentitys" render="${empty dependencies}" z="T0LoTr6PZAwfIQHkjOZMmPW7cO8=">
<field:input field="myEntityService" id="c_org_example_domain_MyEntity_myEntityService" render="false" z="12sHnsW2dWYyuD+vDtbTve/jWuI="/>
<field:input field="text" id="c_org_example_domain_MyEntity_text" z="jUCTnP7E3pYPcZcfGn1tyJ2VeFI="/>
</form:create>
<form:dependency dependencies="${dependencies}" id="d_org_example_domain_MyEntity" render="${not empty dependencies}" z="Un0bJ/PmWmczxoVTom9NowwIRWk="/>
</div>
并且实体有一个空文本字段
为了确保这一点,我在MyEntity类中添加了@Component:
...
@Component
@RooJavaBean
@RooToString
@RooJpaEntity
public class MyEntity {
...
没有什么变化。服务仍然为空
我真的希望它能帮助比我更有知识的人帮我找到解决办法
谢谢大家
同时,我将重新阅读Ralph指出的文档部分。
我显然做错了什么。我不认为我是唯一需要这个的人
再次:谢谢大家Spring不会注入静态字段
关于更新2
看起来ConfigUser是一个Hibernate/JPA/任何实体,但不是Springbean。但是您只能注入SpringBean,比如服务、Dao。。。每个东西都有一个@Component、@Controller、@Service、@Repository注释*
您只能注入Springbean或带有@Configurable requires AspectJ注释的类
使用@Configurable是Spring Roo的做法,在Eclipse中,您的实体旁边必须有一个文件AuxCliente\u Roo\u Configurable.aj。它需要禁用隐藏生成Spring Roo ITDs过滤器,以便在Package Explorer中显示此文件
*有更多的方法可以使对象成为SpringBean,但这并不重要。最终使它工作起来 正如我在上一次对@Ralph的评论中提到的那样:添加@Configurable注释成功了 如果该类如下所示,则自动布线将起作用:
@Configurable <--- added this
@RooPlural("Tarjetas")
@RooJavaBean
@RooToString
@RooJpaEntity
@Table(name = "TARJETAS")
public class Tarjeta {
@Autowired
@Transient
private ConfigUser configUser;
...
当我需要自动连线bean时,我会使用如下内容:
ConfigUser configUser = new Helper().getConfigUser();
再次:非常感谢@RalphSpring不能像这样自动连接静态场。哦。。。我添加static是因为:一个在无状态bean上注入依赖项的黑客。但这没用我变得绝望了我更新了我的问题。无论字段是否为静态字段,我都会得到相同的错误。还有其他想法让我试试吗?哇。。。反应很好。非常感谢你。不过我有个问题。我不确定为什么configUser@Autowired字段在服务或其他地方工作正常。你知道为什么吗?好的,谢谢。关于您对更新2的评论,我尝试将\@组件添加到ConfigUser,而不是在我的实体中,在这两个实体中,并且仅在我的实体中添加\@组件,但没有成功。无论如何,\@PrePersist中的configUser始终为空。我甚至创建了一个新的roo项目,也是一样的。我将更新我的问题在这段时间之后,我重新阅读了文档,发现我不理解@Configurable注释。我不知道为什么我认为我不能在我的案件中使用它。为了使故事简短,只需将其添加到我的实体类中,即可实现所有自动连接。最后,我决定使用一个helper类来避免srpingroo为我的配置bean创建的所有代码。我将添加一个新的答案来解释这一点。
@RooJavaBean
@RooToString
@RooJpaEntity(entityName = "AUX_CLIENTES")
public class AuxCliente {
@Transient
@Autowired
private SaldoClienteService saldoClienteService;
@PostPersist
void afectaSaldoCliente() {
this.saldoClienteService.doWhatYouWant();
}
}
@Configurable <--- added this
@RooPlural("Tarjetas")
@RooJavaBean
@RooToString
@RooJpaEntity
@Table(name = "TARJETAS")
public class Tarjeta {
@Autowired
@Transient
private ConfigUser configUser;
...
@Configurable
@RooJavaBean
public class Helper {
@Autowired
private ConfigUser configUser;
@Autowired
private SaldoClienteService saldoClienteService;
}
ConfigUser configUser = new Helper().getConfigUser();