commons-logging 和log4j包下載 Spring根據XML設定檔產生對象,
需要用到Spring壓縮包中的四個核心JAR包
beans 、context、core 和expression
:
https://pan.baidu.com/s/1qXLHzAW
以及日誌jar包
commons-logging 和log4j
:
https://pan.baidu.com/s/1mimTW5i
建立一個Dynamic Web Project 動態Web項目,在src中建立一個測試的類User如下:
package com.swift;public class User { public void fun() { System.out.println("fun is ready."); }}
原始的方法是在main()中 User user=new User(); user.fun();
現在交給Spring幫我們建立對象,它的底層會使用反射機制等,我們只需要配置xml檔案就可以了。
在src下建立applicationContext.xml
添加schema約束,檔案代碼如下:
<?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"></beans>
設定物件建立
<?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 控制反轉 Spring產生對象小執行個體 --><bean id="user" class="com.swift.User"></bean></beans>
建立Servlet類觀察結果
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("/test")public class TestIOC extends HttpServlet { private static final long serialVersionUID = 1L; public TestIOC() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); @SuppressWarnings("resource") //就是下邊這幾句了 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); User user=(User) context.getBean("user"); String userInfo=user.fun(); response.getWriter().println(); response.getWriter().append(userInfo); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
//就是下邊這幾句了
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//解析xmlUser user=(User) context.getBean("user");//得到對象String userInfo=user.fun();//使用對象response.getWriter().append(userInfo);//伺服器輸出
注意,如果User類中寫了有參構造,而找不到無參構造,則<bean id="user" class="com.swift.User"></bean>這種約束會失敗,無法成功建立對象,所以要加上無參構造,代碼如下
package com.swift;public class User { private String userName; public User(String s) { this.userName=s; } public User() {} public String fun() { return "User's fun is ready."; }}
換一種方法,使用靜態工廠的方法
package com.swift;public class BeanFactoryUser { public static User getUser() { return new User(); }}
類名.加static的方法
這時xml設定檔增加
<bean id="beanFactory" class="com.swift.BeanFactoryUser" factory-method="getUser"></bean>
把靜態方法也填上
Servlet類的代碼如下:
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("/test2")public class TestIOCServlet2 extends HttpServlet { private static final long serialVersionUID = 1L; public TestIOCServlet2() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); User user=(User) context.getBean("beanFactory"); String s=user.fun(); response.getWriter().println(s); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}