Some minor details of set injection in spring are incorrect. springset injection details

Source: Internet
Author: User

Some minor details of set injection in spring are incorrect. springset injection details

This is an occasional null pointer error. After debugging for a long time, I was not familiar with spring injection.

I believe that many beginners who are similar to me will encounter this problem, so I wrote it specially to prevent others from being the same as me. Haha, write it in case you make such a mistake again next time.

First, we will give a counterexample.

All classes

 

There is a city

 

There is a North China class and a collection attribute of a City class.

 

Same as above, South China

Index. jsp page

<% @ Page language = "java" import = "java. util. *" pageEncoding = "GB18030" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Finally, there is a servlet class.

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 ("injected, city count:" + northChina. getCitys (). size (); this. northChina = northChina;} public SouthChina getSouthChina () {return southChina;} public void setS OuthChina (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 =" Shenzhen "> </property> </bean> <bean id =" hongkong "class =" com. javaBean. city "> <property name =" city "value =" Hong Kong "> </property> </bean> <bean id =" guilin "class =" com. javaBean. city "> <property name =" city "value =" Guilin "> </property> </bean> <bean id =" guangzhou "class =" com. javaBean. city "> <property name =" city "value =" Guangzhou "> </property> </bean> <bean id =" beijin "class =" com. javaBean. city "> <property name =" city "value =" Beijing "> </property> </bean> <bean id =" tianjin "class =" com. javaBean. city "> <property name =" city "value =" Tianjin "> </property> </bean> <bean id =" shanghai "class =" com. javaBean. city "> <property name =" city "value =" Shanghai "> </property> </bean> <bean id =" haerbin "class =" com. javaBean. city "> <property name =" city "value =" Harbin "> </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>

The web. xml configuration is correct and deployment to tomcat starts.

 

 

Open page access

Click query. However, an error occurs.

During execution of the test method

The reason is that when I mistakenly thought that the server was started, NorthChina was already injected, so the servlet calls the method in the class and returns null,

As a result, I searched Baidu for "set injection" (in fact, there are a lot of demos, but I didn't explain them carefully). Then I found that the CountryServlet injected into the NorthChina container is in the spring container,

It is not in the CountryServlet class that has not been built. Therefore, you can only add a static value to the two classes to obtain

In this way, the problem is solved (I believe the great God should have a good way to do it. If it is convenient, you can teach Tom in the comments ). Xiao Bai just writes out the problems he encountered during his learning so that he can view the problems and prevent the same mistakes.

Copyright Disclaimer: This article is an original article without code and cannot be reproduced without permission.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.