日期:2014-05-17  浏览次数:20735 次

Spring3注入始终为空
小弟刚接触Spring,写了一个简单的值注入(setter),不管怎么调注入都是NULL,请各位大大指点。感激不尽。
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="car" class="joe.test2.Car"></bean>
<bean id="testStr" class="joe.test2.abc">
<property name="testStr" value="123"></property>
</bean>
</beans>
web.xml 代码如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
   </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

abc.java 
package joe.test2;

public  class abc {
public String testStr;

public String getTestStr() {
return testStr;
}

public void setTestStr(String testStr) {
this.testStr = testStr;
}
}

inde.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="joe.test2.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
abc a = new abc();
%>

<!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>
    <%= a.getTestStr() %><br>
  </body>
</html>

a.getTestStr()怎么调都是NULL,实在不知道为什么了,请大侠指点。
------最佳解决方案--------------------
楼主,首先建议你在网上找一些spring的注入的demo看一看,了解一下spring是如何实现注入来实现对象的实例化的,从上面你写的例子程序来看,你对spring了解的实在是太少了,在这儿给你说