自动装配不能在类@Entity中工作 [英] Autowired not working in a Class @Entity
问题描述
我有一个名为 Menu 的类,其标注为 @Entity ,在该类中,我需要在名为 GestoreMessaggi
I have a class called Menu, annnotated as @Entity where i need to use a method inside a class called GestoreMessaggi
....
@Component
@Entity
@Table(name="menu")
public class Menu implements Serializable{
@Autowired
@Transient // because i dont' save this field on the DB
private GestoreMessaggi gestoreMessaggi;
.....
public void setCurrentLanguage(){
/* I got the error both if use gestoreMessaggi
this way and if I use the autowired istance of GestoreMessaggi*/
GestoreMessaggi gestoreMessaggi = new GestoreMessaggi();
gestoreMessaggi.gest();
}
.....
这是GestoreMessaggi类的相关代码
This is the relevant code of the class GestoreMessaggi
@Component
public class GestoreMessaggi {
@Autowired
private ReloadableResourceBundleMessageSource messageSource;
public void gest(){
messageSource.doSomething() <--- here messageSource is null
}
}
什么时候,我叫gestoreMessaggi.gest();从Menu类收到错误,因为 messageSource 为空. gestoreMessaggi距离不为null,仅 messageSource
When, I call gestoreMessaggi.gest(); from Menu class, I got an error because messageSource is null. The gestoreMessaggi istance is NOT null, is null just messageSource
重要:仅当我从标注为 @Entity 的类中调用GestoreMessaggi时,messageSource上为null.
IMPORTANT: I get null on messageSource only when I call GestoreMessaggi from classes annotated as @Entity.
在ds-servlet.xml中,我告诉Spring扫描包含Menu和GestoreMessaggi类的包装:
In ds-servlet.xml I tell Spring to scan the pakages that contain Menu and GestoreMessaggi classes:
//Menu package
<context:component-scan base-package="com.springgestioneerrori.model"/>
//Gestore messaggi package
<context:component-scan base-package="com.springgestioneerrori.internazionalizzazione"/>
谢谢
推荐答案
您可以采用两种方法:
- 尝试从
@Entity
类的方法中获取Spring管理的bean的实例 - 按照@Stijn Geukens的建议更改设计,并使您的实体成为POJO,而没有任何逻辑或依赖项注入机制
- Try to get an instance of a Spring-managed bean from within a method of your
@Entity
class - Change the design as suggested by @Stijn Geukens and make your entity a POJO without any logic or dependency injection machanisms
如果选择选项1,则必须显式访问Spring的上下文并检索所需的bean实例:
If you go by option 1, you have to explicitly access Spring's context and retrieve an instance of the bean you need:
@Component
public class Spring implements ApplicationContextAware {
private static final String ERR_MSG = "Spring utility class not initialized";
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T bean(Class<T> clazz) {
if (context == null) {
throw new IllegalStateException(ERR_MSG);
}
return context.getBean(clazz);
}
public static <T> T bean(String name) {
if (context == null) {
throw new IllegalStateException(ERR_MSG);
}
return (T) context.getBean(name);
}
}
您需要让Spring扫描此类,以使其正常工作.
You need to make Spring scan this class in order for this to work.
然后在您的@EntityClass
内部,执行以下操作:
Then, inside your @EntityClass
, do this:
public void setCurrentLanguage(){
GestoreMessaggi gestoreMessaggi = Spring.bean(GestoreMessaggi.class);
gestoreMessaggi.gest();
}
仅此而已.请注意,您不再需要将GestoreMessaggi
自动接线到@Entity
中.另请注意, Spring和大多数社区都不推荐这种方法,因为它会将您的域类(您的@Entity
类)耦合到Spring类.
And that would be all. Note that you wouldn't need to autowire GestoreMessaggi
into your @Entity
any more. Please also note that this approach is not recommended neither by Spring nor by most of the community, since it couples your domain classes (your @Entity
class) to Spring classes.
如果您选择选项2,那么您所需要做的就是让Spring像往常一样解决自动装配问题,但要在实体之外 (即在dao或服务中)以及您的实体进行自动装配需要您用一些消息或其他内容来填充它,只需在其上调用一个setter. (然后由您决定是否将@Entity
s属性设置为@Transient
,具体取决于您的要求.)
If you go by option 2, then all you need to do is let Spring resolve the autowiring as usual, but outside of your entity (i.e. in a dao or service), and if your entity needs you to fill it with some message or whatever, just invoke a setter on it. (Then it's up to you to make your @Entity
s attribute @Transient
or not, depending on your requirements).
这篇关于自动装配不能在类@Entity中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!