我想在tomcat启动后做一些数据库搜索。
因此,我只是实现了InitializingBean,实现了方法afterPropertiesSet,并将数据库放在afterPropertiesSet中操作。
而且,我的项目是使用proxool。
然后我启动tomcat来测试afterPropertiesSet方法。我犯了这个错误
org.logicalcobwebs.proxool.ProxoolException: Attempt to refer to a unregistered pool by its alias我认为web.xml中组件的启动顺序有问题。
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
<init-param>
<param-name>propertyFile</param-name>
<param-value>WEB-INF/classes/jdbc.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>ProxoolAdmin</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProxoolAdmin</servlet-name>
<url-pattern>/proxool/admin</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/application-context-*.xml</param-value>
</context-param>然后我在afterPropertiesSet方法的第一行设置了一个断点,在org.logicalcobwebs.proxool.configuration.ServletConfigurator的init方法中设置了第二个断点。
为了避免以前的错误,我只需在afterPropertiesSet中输入一个打印操作。
然后启动tomcat来检查afterPropertiesSet和ServletConfigurator的顺序。
afterPropertiesSet方法在ServletConfigurator init之前调用。
我一直在想为什么我会有Attempt to refer to a unregistered pool by its alias错误。
但是,如何让afterPropertiesSet在ServletConfigurator init之后调用?
编辑
还有GoodsRecommendService类
@Service
public class GoodsRecommendServiceImpl implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("There is invoked before servlet init!!");
}
}发布于 2014-05-06 13:11:58
造成这个问题的原因是,在创建上下文的所有ServletContextListeners之前都会调用Servlet。作为ServletContextListener 州的API
接收web应用程序初始化进程正在启动的通知。 在初始化web应用程序中的任何过滤器或servlet之前,都会将上下文初始化通知所有ServletContextListeners。
因此,在您的配置中:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/application-context-*.xml</param-value>
</context-param>意味着您的Spring ApplicationContext是在任何Servlets之前创建的。这就是你犯错误的原因。
要修复它,您需要确保无论在bean中做什么,只要Proxool被正确初始化,就可以执行它。
我认为最简单的方法是将Spring ApplicationContext切换为由DispatcherServlet加载,并确保DispatcherServlet的启动负载值大于Proxool的值。
有关DispatcherServlet的更多信息,请参见这里。
发布于 2016-10-19 09:09:34
另一种方法可能是确保Spring应用程序上下文将配置Proxool,如以下示例所示:
<bean id="proxoolProperties" class=" org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:proxool.properties"/>
</bean>
<bean id="proxoolConfiguration" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.logicalcobwebs.proxool.configuration.PropertyConfigurator" />
<property name="targetMethod" value="configure" />
<property name="arguments" ref="proxoolProperties" />
</bean>https://stackoverflow.com/questions/23487672
复制相似问题