spring中set注入的一些小細節錯誤


這是小白偶爾一直null指針的錯誤,調試了好久,原來是自己對spring注入的不夠了解

我相信有很多跟我差不多的初學者會遇上,所以特地寫出來,防止有人跟我一樣。哈哈,也寫上去,以防自己下次還犯這樣的錯誤。

一樣,首先,舉個反例

所有類

 

有個城市類

 

有個華北地區類,有個城市類的集合屬性

 

同上,華南地區類

 index.jsp頁面

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
   <center>
   <h1>歡迎光臨</h1><p><p><p><p>
    <form action="query" method="post">
      <table border="0">
        <tr>
            <td>
                <input type="radio" name="country" value="SC" checked> 華南旅游城市<br>
                <input type="radio" name="country" value="NC"> 華北旅游城市 <br>            
            </td>
        </tr>
        <tr>
          <td colspan="2" align="center">
          <input type = "submit" name = "submit" value = "查 詢" />
          </td>
        </tr>       
      </table>
    </form>
   </center>
  </body>
</html>

最后有個servlet類

package com.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import com.javaBean.City; import com.javaBean.NorthChina; import com.javaBean.SouthChina; public class CountryServlet extends HttpServlet { private NorthChina northChina; private SouthChina southChina; public NorthChina getNorthChina() { return northChina; } public void setNorthChina(NorthChina northChina) { System.out.println("已經注入了,城市個數:"+northChina.getCitys().size()); this.northChina = northChina; } public SouthChina getSouthChina() { return southChina; } public void setSouthChina(SouthChina southChina) { this.southChina = southChina; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); String diqu=request.getParameter("country"); PrintWriter writer = response.getWriter(); List<City> citys=null; if(diqu!=null){ if("SC".equals(diqu)){ citys=southChina.getCitys(); }else if("NC".equals(diqu)){ citys=northChina.getCitys(); } } String str=""; for (int i = 0; i < citys.size(); i++) { str+=citys.get(i).getCity()+","; } writer.write(str); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<?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="shenzhen" class="com.javaBean.City">
    <property name="city" value="深圳"></property>
</bean>
<bean id="hongkong" class="com.javaBean.City">
    <property name="city" value="香港"></property>
</bean>
<bean id="guilin" class="com.javaBean.City">
    <property name="city" value="桂林"></property>
</bean>
<bean id="guangzhou" class="com.javaBean.City">
    <property name="city" value="廣州"></property>
</bean>
<bean id="beijin" class="com.javaBean.City">
    <property name="city" value="北京"></property>
</bean>
<bean id="tianjin" class="com.javaBean.City">
    <property name="city" value="天津"></property>
</bean>
<bean id="shanghai" class="com.javaBean.City">
    <property name="city" value="上海"></property>
</bean>
<bean id="haerbin" class="com.javaBean.City">
    <property name="city" value="哈爾濱"></property>
</bean>

<bean id="southChina" class="com.javaBean.SouthChina">
    <property name="citys">
         <list>
             <ref bean="guilin"/>
             <ref bean="shenzhen"/>
             <ref bean="hongkong"/>
             <ref bean="guangzhou"/> 
         </list>
    </property>
</bean>
<bean id="northChina" class="com.javaBean.NorthChina">
    <property name="citys">
        <list>
            <ref bean="shanghai"/>
            <ref bean="haerbin"/>
            <ref bean="beijin"/>
            <ref bean="tianjin"/>
        </list>
    </property>
</bean>
<bean id="countryServlet" class="com.servlet.CountryServlet">
    <property name="northChina" ref="northChina"></property>
    <property name="southChina" ref="southChina"></property>
</bean>

</beans>

web.xml配置是正確的,開始部署到tomcat

 

 

 打開頁面訪問

點擊查詢,然而錯誤就來了

測試方法中執行時會發現

原因竟然是我誤認為啟動服務器時,已經把NorthChina注入進去了,所以就在servlet調用類中的方法,報出null,

結果我百度搜索“set注入”(其實都是很多demo,並沒有仔細詳解),然后我就發現注入NorthChina的是spring容器中的CountryServlet,

而現在調用的CountryServlet類並非是Spring構建的,可以理解為自己new了一個。所以就只能在這兩個類中加個static,就可以獲取了

這樣,問題就解決了(相信大神應該還有跟多好的方法,方便的話可以在評論中教導小白)。小白只是把自己在學習中遇到的問題寫出來,方便自己查看學習,也可以讓大家防止遇到一樣的錯誤,嘿嘿。

 

公眾號

 

歡迎關注我的公眾號“碼上開發”,每天分享最新技術資訊、最優原創文章。關注獲取最新資源

 

 

版權聲明:本文為不會代碼的小白原創文章,未經允許不得轉載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM