简体   繁体   English

如何确定应用程序上下文中的对象?

[英]How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. 我正在尝试为Liferay(使用Tomcat和Spring)编写一个Portlet,并且需要通过Persistence API / Hibernate使用数据库。 I am using some configuration XMLs (applicationContext.xml, etc.) and annotations to realize that. 我正在使用一些配置XML(applicationContext.xml等)和注释来实现这一点。 eg 例如

@Component @Scope("application")
public class SomeForm {
   @PersistenceContext EntityManager entityManager;

At the moment I am having some troubles with NullPointerExceptions which occur, in my opinion, because the entityManager is not bound. 目前,我对NullPointerExceptions感到有些麻烦,在我看来,这是因为entityManager没有绑定。 But I am not asking for an answer to that problem, but to a more wider topic: 但是,我不是要为这个问题提供答案,而是要为更广泛的话题提供答案:

Is there an easy way to check what Objects are available in the application context at runtime (eg PesistenceContext, SessionContext) and to determine how an Object is bound in the application context at runtime? 是否有一种简单的方法可以在运行时检查应用程序上下文中有哪些对象(例如PesistenceContext,SessionContext),并确定在运行时如何在应用程序上下文中绑定对象?

You can call methods 您可以调用方法

getBeanDefinitionCount()
getBeanDefinitionNames()
getBeanNamesForType()
getBeansOfType()
getBean()

on the application context itself to inspect the beans it contains at runtime. 在应用程序上下文本身上检查运行时包含的bean。

There are also equivalent static methods on the BeanFactoryUtils class that will take into account the nesting hierarchy if you are using nested contexts. 如果您使用的是嵌套上下文,则BeanFactoryUtils类上还存在等效的静态方法,这些方法将考虑嵌套层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM