方法一:html
實現BeanPostProcessor接口:java
- public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {
-
- public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- return bean;
- }
-
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- return bean;
- }
- }
在配置文件中添加:web
- <bean class="processor.InstantiationTracingBeanPostProcessor"/>
方法二:app
實現InitializingBean接口:
ide
- public class SysInitBean implements InitializingBean, ServletContextAware {
- public void afterPropertiesSet() throws Exception {
- }
-
- @Override
- public void setServletContext(ServletContext servletContext) {
- }
- }
在配置文件中添加:post
- <bean class="processor.SysInitBean"/>
方法三:spa
實現ServletContextListener:.net
- public class RedisInitListener implements ServletContextListener {
-
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
-
- }
-
- @Override
- public void contextInitialized(ServletContextEvent sce) {
-
- ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
- }
- }
在web.xml中添加listener:xml
- <listener>
- <listener-class>listener.RedisInitListener</listener-class>
- </listener>