struts获得spring

来源:互联网 发布:手机淘宝怎么加淘友 编辑:程序博客网 时间:2024/06/01 18:57

在web.xml文件中配置监听器,作用是在webapplication已启动就让它自动读取配置文件,并且初始化bean 

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  <!-- default: /WEB-INF/applicationContext.xml -->
 </listener>

 <context-param><!--作用是选择启动web application时找到配置文件的路径>
  <param-name>contextConfigLocation</param-name>
  <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  --><!--默认会寻找WEB-INF目录下的applicationContext开头的xml文件>
  <param-value>classpath:beans.xml</param-value><!--配置文件-->
 </context-param>

 <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>

在action类上写

@Component

@Scope("prototype")就完成了struts的注入

 

struts执行过程:一开始起动先读struts-default.xml然后struts-plugin.xml然后struts.xml然后struts.properties最后web.xml.

                             webapplication启动后找到配置文件,然后文件里的bean就会被初始化,当用到action的时候,就会从struts.xml里找到名字和action name相同的bean

 

 action是由struts的struts-spring-plugin产生的,xml文件里配置的bean是service(业务逻辑层)和dao层的容器,在action类的上面不用写@Component("user")
@Scope("prototype"),默认是按名字注入的,可以改成按类型来注入。

struts容器里产生action的时候会自动的从spring容器中注入相应的东西

 

 

原创粉丝点击