zoukankan      html  css  js  c++  java
  • 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,就可以获取了

    这样,问题就解决了(相信大神应该还有跟多好的方法,方便的话可以在评论中教导小白)。小白只是把自己在学习中遇到的问题写出来,方便自己查看学习,也可以让大家防止遇到一样的错误,嘿嘿。

    公众号

    欢迎关注我的公众号“码上开发”,每天分享最新技术资讯、最优原创文章。关注获取最新资源

    版权声明:本文为不会代码的小白原创文章,未经允许不得转载。

  • 相关阅读:
    SQL一般注入(一)
    SQl注入的分类
    SQl注入常见参数
    wireshark
    分享.Net 设计模式大全
    .net Core实战简单文件服务器
    .net Core中间件实战
    c#5.0/6.0/7.0
    使用Bot Framework建立你的第一个聊天机器人
    如何用.net制作一个简易爬虫抓取华为应用市场数据
  • 原文地址:https://www.cnblogs.com/xswz/p/7062344.html
Copyright © 2011-2022 走看看