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 );}}