How can I determine Objects in application context?

╄→гoц情女王★ 提交于 2020-01-06 15:25:13

问题


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;

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:

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?


回答1:


You can call methods

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

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

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



来源:https://stackoverflow.com/questions/1713943/how-can-i-determine-objects-in-application-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!