为什么initMethod没有执行 ?

yjhengineer 2014-11-09 03:21:22
尝试搭建struts2+spring的框架集成,配置如下:

web.xml:

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>


applicationContext.xml:
<bean name="loginService" class="com.ssh.service.LoginService" init-method="initMethod" scope="prototype">
</bean>


service代码:
public class LoginService implements InitializingBean {

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("service afterPropertiesSet method");
}

public void initMethod(){
System.out.println("service initMethod method");
}



}


tomcat启动后没有执行相关代码:

2014-11-9 15:20:26 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (org.apache.struts2.dispatcher.StaticContentLoader)
2014-11-9 15:20:26 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.UnknownHandlerManager)
2014-11-9 15:20:26 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (org.apache.struts2.views.util.UrlHelper)
2014-11-9 15:20:26 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.TextParser)
2014-11-9 15:20:26 org.apache.struts2.spring.StrutsSpringObjectFactory info
信息: Initializing Struts-Spring integration...
2014-11-9 15:20:26 com.opensymphony.xwork2.spring.SpringObjectFactory info
信息: Setting autowire strategy to name
2014-11-9 15:20:26 org.apache.struts2.spring.StrutsSpringObjectFactory info
信息: ... initialized Struts-Spring integration successfully
2014-11-9 15:20:26 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2014-11-9 15:20:26 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2014-11-9 15:20:26 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/16 config=null
2014-11-9 15:20:26 org.apache.catalina.startup.Catalina start
信息: Server startup in 2125 ms


是不是哪里配置有问题啊 ??
...全文
370 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
马可菠萝啊 2015-08-11
  • 打赏
  • 举报
回复
二楼说的好有道理,受教了,那如果配置scope="singleton"呢 ?这个什么时候会被加载?
kevin-go 2014-11-10
  • 打赏
  • 举报
回复
启动tomcat的时候当然不会去调用afterPropertiesSet回调方法和initMethod初始方法了 , 你这个bean是scope="prototype", 只有在每次使用的时候在去ioc容器拿这个bean的时候才会执行初始化方法, tomcat启动的时候, 容器也会启动, 但是scope="prototype"不会再这个时候初始化bean, 更谈不上执行初始化方法了
yjhengineer 2014-11-09
  • 打赏
  • 举报
回复
没人遇到过这种问题吗 ?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧