如何确定应用程序上下文中的对象? [英] How can I determine Objects in application context?

查看:101
本文介绍了如何确定应用程序上下文中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Liferay(使用Tomcat和Spring)编写一个Portlet,并且需要通过Persistence API/Hibernate使用数据库.我正在使用一些配置XML(applicationContext.xml等)和注释来实现这一点.例如

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and annotations to realize that. e.g.

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

目前,我对NullPointerExceptions感到有些麻烦,我认为这是因为没有绑定entityManager.但是,我不是要回答这个问题,而是要回答更广泛的话题:

At the moment I am having some troubles with NullPointerExceptions which occur, in my opinion, because the entityManager is not bound. But I am not asking for an answer to that problem, but to a more wider topic:

是否有一种简便的方法可以在运行时检查应用程序上下文中可用的对象(例如PesistenceContext,SessionContext),并确定在运行时如何将对象绑定到应用程序上下文中?

Is there an easy way to check what Objects are available in the application context at runtime (e.g. PesistenceContext, SessionContext) and to determine how an Object is bound in the application context at runtime?

推荐答案

您可以调用方法

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

在应用程序上下文本身上

在运行时检查它包含的bean.

on the application context itself to inspect the beans it contains at runtime.

BeanFactoryUtils 类上还有等效的静态方法,如果您使用的是嵌套上下文,这些方法将考虑嵌套层次结构.

There are also equivalent static methods on the BeanFactoryUtils class that will take into account the nesting hierarchy if you are using nested contexts.

这篇关于如何确定应用程序上下文中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆