Spring injects attributes according to the XML configuration file, and Spring XML configuration file

Source: Internet
Author: User

Spring injects attributes according to the XML configuration file, and Spring XML configuration file

Method 1 use the setter Method

package com.swift;public class Book {    private String bookName;    public void setBook(String bookName) {        this.bookName = bookName;    }    @Override    public String toString() {        return "Book [book=" + bookName + "]";    }}

In the Spring framework, assume that the Servlet class cannot directly generate the Book Class Object and inject the String bookName attribute value.

However, you need to use the xml method of the configuration file.

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- IoC control inversion SpringSpring injects Attributes Based on XML configuration files --> <bean id = "book" class = "com. swift. book "> <property name =" bookName "value =" -- Dark Forest "> </property> </bean> </beans>

Servlet code:

Package com. swift; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. annotation. webServlet; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; @ WebServlet ("/book") public class BookServlet extends HttpServlet {private static final long serialVersionUID = 1L; public BookServlet () {super ();} protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); response. getWriter (). append ("Served :"). append (request. getContextPath (); @ SuppressWarnings ("resource") // The following sentence: ApplicationContext context = new ClassPathXmlApplicationContext (". xml "); Book book = (Book) context. getBean ("book"); String bookInfo = book. fun (); response. getWriter (). println (); response. getWriter (). append (bookInfo);} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}

Note:

Core jar packages of beans, context, core, and expression

And the commons-logging and log4j jar packages must not be missing.

 

Method 2 construct with Parameters

package com.swift;public class Book {    private String bookName;    public Book() {}    public Book(String bookName) {        this.bookName=bookName;    }    public String fun() {        return "Book [book="+ bookName+"]";    }}

The xml configuration file code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- IoC control inversion spring generates an object based on the XML configuration file --> <bean id = "book" class = "com. swift. book "> <constructor-arg name =" bookName "value =" count of Christchurch "> </constructor-arg> </bean> </beans>

Finally, the Servlet class code is as follows:

Package com. swift; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. annotation. webServlet; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; @ WebServlet ("/book2") public class BookServlet2 extends HttpServlet {private static final long serialVersionUID = 1L; public BookServlet2 () {super ();} protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); response. getWriter (). append ("Served :"). append (request. getContextPath (); @ SuppressWarnings ("resource") // The following sentence: ApplicationContext context = new ClassPathXmlApplicationContext ("B. xml "); Book book = (Book) context. getBean ("book"); String bookInfo = book. fun (); response. getWriter (). println (); response. getWriter (). append (bookInfo);} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}

 

Related Article

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.