开发者社区> 问答> 正文

Struts2整合Spring,Action配置报错,请问什么情况?403.10 禁止访问:配置无效

项目使用了SSH,用注解的方式装备bean,如果只在Dao,service层注解。没有问题可以正常启动tomcat,bean也装配成功,可以正常访问。如果action也加了注解就会启动失败,并且报错
启动tomcat时的异常:

[ERROR] 2016-02-25 17:21:24,427 [ pool-2-thread-1:1123 ] method:initWebApplicationContextContext initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'policeLoginAction' defined in file [E:\Workspaces\eclispe.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\zyhCity160129\WEB-INF\classes\com\safe\action\police\PoliceLoginAction.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.safe.action.police.PoliceLoginAction]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4765) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5260) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1525) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1515) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.safe.action.police.PoliceLoginAction]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069) ... 22 more Caused by: java.lang.NullPointerException at org.apache.struts2.ServletActionContext.getRequest(ServletActionContext.java:112) at com.safe.action.BaseAction.<init>(BaseAction.java:23) at com.safe.action.police.PoliceLoginAction.<init>(PoliceLoginAction.java:15) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148) ... 24 more

action:

package com.safe.action.police;

import java.text.SimpleDateFormat; import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;

import com.safe.action.BaseAction; import com.safe.model.Police; import com.safe.service.PoliceService; import com.safe.util.ConstantUtil;

@Controller public class PoliceLoginAction extends BaseAction { private static final long serialVersionUID = 1L; @Autowired private PoliceService policeService;

private String policeNo;
private String pwd;// 叫做授权码 只有在开发测试时使用 2016年1月20日15:34:02
private String token;

public String getPoliceNo() {
	return policeNo;
}

public void setPoliceNo(String policeNo) {
	this.policeNo = policeNo;
}

public String getPwd() {
	return pwd;
}

public void setPwd(String pwd) {
	this.pwd = pwd;
}

public String getToken() {
	return token;
}

public void setToken(String token) {
	this.token = token;
}

@Override
public String execute() throws Exception {
	String returnStr = SUCCESS;
	System.out.println(
			new SimpleDateFormat("yyyy-MM-dd HH🇲🇲ss").format(new Date()) + " login:" + policeNo + "---" + pwd);
	Police police = policeService.login(policeNo, pwd, token);

	if (police == null) {
		message = "登录失败!";
		code = 0;
		returnStr = "fail";
		// return "fail"; 返回json的时候,如果此处return,就不会执行后面的发送json
	} else {
		jsonMap.put("token", police.getToken());
		jsonMap.put("police", police);
		session.setMaxInactiveInterval(ConstantUtil.HTTPSESSION_life);// 设置session闲置间隔(有效期),单位:秒
		// 登陆状态标识符
		session.setAttribute("policeId", police.getId());// 登录警员的Id
		session.setAttribute("loginName", policeNo);// 验证在线状态时使用,如果为null则表示没有登录
		session.setAttribute("identity", "police");// 登陆者身份类别
	}
	returnJson();
	return returnStr;
}

}

最后一个空指针异常的位置

package com.safe.action;

import java.util.HashMap; import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport; import com.safe.util.ConstantUtil; import com.safe.util.JsonUtil;

public class BaseAction extends ActionSupport { private static final long serialVersionUID = 1L;

protected int code = 1;// 响应登陆状态码,1成功 0失败
protected String message = ConstantUtil.RETURN_OK;

protected Map<String, Object> jsonMap = new HashMap<String, Object>();	

protected HttpSession session = ServletActionContext.getRequest()//这一行报空指针
		.getSession();</pre> 

如果,没有在action加@Controller注解,则没有异常,并且BaseAction中的函数可也以正常调用。 另外一个问题,spring配置action时该使用单例还是其他的作用域?

展开
收起
kun坤 2020-05-27 14:15:13 710 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多