Spring 注入问题

J2EE 码拜 7年前 (2017-05-01) 1686次浏览
自学ssh框架 spring 和Struts 整合的时候  bean action 设为单例模式 但是每次请求还是会new  下面贴上代码 讨教各位高手
action 代码

package com.user.action;
import java.io.IOException;
import com.opensymphony.xwork2.ActionSupport;
import com.user.bean.User;
import com.user.service.UserService;
public class UserLoginAction extends ActionSupport{ 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    
	private UserService userService;
	private User user;
	public String userLogin() throws IOException{
		System.out.println("userLogin this="+this);
		System.out.println("userService="+userService);
//		tuserService.userLogin(user);
	    return "success";
	}
	public UserLoginAction() {
		System.out.println("new Action--");
	}
	public UserLoginAction(UserService userService, User user) {
		this.userService = userService;
		this.user = user;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		System.out.println("setUserService");
		this.userService = userService;
	}
}

applicationContext.xml 代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
           
       <bean name="user" class="com.user.bean.User"></bean>
       
       <bean name="userService" class="com.user.service.UserService"></bean>
           
       <bean name="userloginAction" class="com.user.action.UserLoginAction" scope="singleton">
           <property name="userService" ref="userService"></property>
       </bean>
     
</beans>

struts.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.i18n.encoding" value="utf-8"></constant>
    
     <package name="default" namespace="/" extends="struts-default">
     
     <action name="userLoginAction" class="com.user.action.UserLoginAction" method="userLogin">
     <result name="success">index.jsp</result>
     <result name="error">error.jsp</result>
     </action>

web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>score_struts2_spring</display-name>
<!-- 	struts2配置  -->
  <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>
  
<!-- Spring配置  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

用jsp登录页面请求action 运行结果如下  action new了三次。
运行结果

四月 15, 2017 8:28:05 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3c3423: defining beans [user,userService,userloginAction]; root of factory hierarchy
new Action--<span style="color: #FF0000;">(第一次new action spring加载)</span>
setUserService
四月 15, 2017 8:28:06 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization completed in 294 ms
四月 15, 2017 8:28:06 下午 org.apache.catalina.util.SessionIdGenerator createSecureRandom
信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [126] milliseconds.
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-default.xml]
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to locate configuration files of the name struts-plugin.xml, skipping
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-plugin.xml]
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts.xml]
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.ObjectFactory)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.FileManager)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.XWorkConverter)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.TextProvider)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.ActionProxyFactory)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.ObjectTypeDeterminer)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.dispatcher.mapper.ActionMapper)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (jakarta) for (org.apache.struts2.dispatcher.multipart.MultiPartRequest)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.views.freemarker.FreemarkerManager)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.components.UrlRenderer)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.validator.ActionValidatorManager)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.ValueStackFactory)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.reflection.ReflectionProvider)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.reflection.ReflectionContextFactory)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.PatternMatcher)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.dispatcher.StaticContentLoader)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.UnknownHandlerManager)
四月 15, 2017 8:28:06 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.views.util.UrlHelper)
四月 15, 2017 8:28:06 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.57\webapps\ROOT
四月 15, 2017 8:28:06 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.57\webapps\ROOT has finished in 54 ms
四月 15, 2017 8:28:06 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8088"]
四月 15, 2017 8:28:06 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
四月 15, 2017 8:28:06 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 2677 ms
new Action--<span style="color: #FF0000;"><span style="font-size: 12px;">(请求action的时候又会 new action 而且还是两次)</span></span>
new Action--
userLogin this=com.user.action.UserLoginAction@160ac0b
userLogin this=com.user.action.UserLoginAction@87bc99
userService=null
userService=null
解决方案

40

在struts的配置文件中:<action name=”userLoginAction” class=”com.user.action.UserLoginAction” method=”userLogin”>  这个class的属性值,使用spring管理的bean名称, 没看错的话应该是这个“userloginAction”。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Spring 注入问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)