如何在 WebLogic 应用程序初始化期间获取 DataSource

How to get a DataSource during initialization of a WebLogic application

我将 WebLogic 与 Spring 一起使用,与 Oracle 数据库对话。我设置了一个实现 ServletContextListener 的 Servlet,并配置 web.xml 以将 class 添加为侦听器。在对 contextInitialized(ServletContextEvent sce) 的调用中,我试图获取我在 application.xml 中的 bean 中配置的 DataSource 对象,以便在应用程序启动时我可以对 DB 做一些事情.

  public void contextInitialized(ServletContextEvent sce) {
    try {
      InitialContext enc = new InitialContext();
      Context compContext = (Context) enc.lookup("java:comp/env");
      dataSource = (DataSource) compContext.lookup("dataSource");
      ...

这会在查找时引发 NameNotFoundException。我应该怎么得到这个? 我的豆子看起来像这样:

<bean id="dataSource" class="oracle.ucp.jdbc.PoolDataSourceFactory"
      factory-method="getPoolDataSource">
    <property name="URL" value="jdbc:oracle:thin:@localhost:1521:XE"/>
    <property name="connectionFactoryClassName" value="oracle.jdbc.pool.OracleDataSource"/>
    <property name="connectionPoolName" value="COS_POOL"/>
    ...
</bean>

您可以在其中一个 Spring 托管 bean 中使用 @PostConstruct 注释,以在应用程序启动期间执行您想要的操作。或者,您可以实现 InitializingBean 并实现 afterPropertiesSet 方法。