1. 程式人生 > >spring中set註入的一些小細節錯誤

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

學習 細節 borde 集合 south let odi 桂林 代碼

這是小白偶爾一直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類中。所以就只能在這兩個類中加個static,就可以獲取了

技術分享

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

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

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