2009-11-11 28 views
0

我正在嘗試爲Liferay編寫一個portlet(使用Tomcat和Spring),並且需要通過Persistence API/Hibernate使用數據庫。我正在使用一些配置XML(applicationContext.xml等)和註釋來實現這一點。例如如何確定應用程序上下文中的對象?

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

目前我遇到了NullPointerExceptions發生的一些麻煩,在我看來,因爲entityManager沒有綁定。但我並不是要求回答這個問題,而是要求更廣泛的主題:

是否有一種簡單的方法來檢查運行時(例如PesistenceContext,SessionContext)應用程序上下文中的對象是否可用以及如何確定一個對象在運行時綁定在應用程序上下文中?

+0

這裏有一些信息:http://stackoverflow.com/questions/129207/getting-spring-application-context –

回答

2

你可以調用方法

getBeanDefinitionCount() 
getBeanDefinitionNames() 
getBeanNamesForType() 
getBeansOfType() 
getBean() 
應用程序上下文本身來檢查它包含在運行時豆

BeanFactoryUtils類中也有等效靜態方法,如果您使用嵌套上下文,將考慮嵌套層次結構。

相關問題