当前位置: 代码迷 >> Java Web开发 >> spring注解装配出现的空指针有关问题,求
  详细解决方案

spring注解装配出现的空指针有关问题,求

热度:147   发布时间:2016-04-16 22:18:57.0
spring注解装配出现的空指针问题,急求啊
本帖最后由 u010571601 于 2014-06-11 12:31:49 编辑
菜鸟一个,最近刚刚知道spring可以通过注解装配,就在项目里添加了,可是怎么弄都会报空指针错误。上网百度了很多方法都未能解决,希望有大神能够指点一二。已经为这个问题耽误很长时间了。70分不多。已经是我剩下的所有分数了。。。

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_3_0.xsd" version="3.0">
  <display-name>springtest</display-name>
  <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>
  </context-param>
</web-app>


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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-2.5.xsd
 ">
 <bean id="student" class="bean.student" ></bean>
 <context:annotation-config />
<context:component-scan base-package="bean"></context:component-scan>
</beans>


student.java
package bean;

public class student {
public String name;
public String major;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return name+"同学的专业是"+major;
}
}


studentservice.java

package bean;

import javax.annotation.Resource;



public class studentService {
@Resource
student student;

public studentService()
{
}
public String getInfo()
{
student.setName("小强");
student.setMajor("计算机科学与技术专业");
return student.toString();
}

}


测试页面

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="bean.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <%
    bean.studentService service=new bean.studentService();
    %>
     <h1><%=service.getInfo() %></h1>
     <h3>加载完毕</h3>
  </body>
</html>


不用注解,直接用这个正常运行。另外,如果去掉applicationcontext.xml中的
 <bean id="student" class="bean.student" ></bean>
然后在student.java里加上@Component这个注解,也是可以正常出结果的,但就是一用@autowire或者@resource 
就会报空指针异常
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import bean.student;

public class test {

public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
student student=context.getBean("student",student.class);
student.setName("小明");
student.setMajor("计算机科学与技术专业");
System.out.println(student);
  相关解决方案