spring在WEB中的应用。
生活随笔
收集整理的這篇文章主要介紹了
spring在WEB中的应用。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1:創建IOC容器。在WEB應用程序啟動的時候就創建。利用到監聽器。
ServletContextListener類的contextInitialized方法中
1 package com.struts2.listeners; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletContextEvent; 5 import javax.servlet.ServletContextListener; 6 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 10 public class SpringServletContextListener implements ServletContextListener{ 11 12 public void contextDestroyed(ServletContextEvent arg0) { 13 // TODO Auto-generated method stub 14 15 } 16 17 public void contextInitialized(ServletContextEvent arg0) { 18 //1:applicationContext.xml在web.xml中進行創建。然后利用ServletContext獲取到。 19 ServletContext sc=arg0.getServletContext(); 20 String config=sc.getInitParameter("configLocation"); 21 //創建IOC容器 22 ApplicationContext act=new ClassPathXmlApplicationContext(config); 23 //把創建的IOC容器放到ServletContext(即application域)中 24 sc.setAttribute("ApplicationContext", act); 25 } 26 27 }在web.xml中創建監聽器和applicationContext.xml
<context-param><param-name>configLocation</param-name><param-value>applicationContext.xml</param-value></context-param><listener><listener-class>com.struts2.listeners.SpringServletContextListener</listener-class></listener>然后創建一個實體:Person
package com.struts2.entyties;public class Person {private String username;public void setUsername(String username) {this.username = username;}public void hello(){System.out.println("My name is " + username);}}然后創建bean
<bean id="person" class="com.struts2.entyties.Person"><property name="username" value="陸偉"></property></bean>然后寫個servlet去使用:
package com.struts2.servlet;import java.io.IOException;import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.context.ApplicationContext;import com.struts2.entyties.Person;public class TestServlet extends HttpServlet{private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//1. 從 application 域對象中得到 IOC 容器的引用ServletContext sc=getServletContext();ApplicationContext act=(ApplicationContext) sc.getAttribute("ApplicationContext");//2. 從 IOC 容器中得到需要的 beanPerson person = act.getBean(Person.class);person.hello();}}在web.xml中加載servlet
<servlet><description></description><display-name>TestServlet</display-name><servlet-name>TestServlet</servlet-name><servlet-class>com.struts2.servlet.TestServlet</servlet-class></servlet><servlet-mapping><servlet-name>TestServlet</servlet-name><url-pattern>/TestServlet</url-pattern></servlet-mapping>然后寫個頁面進行訪問:
<a href="TestServlet">TestServlet</a>?
轉載于:https://www.cnblogs.com/bulrush/p/8000934.html
總結
以上是生活随笔為你收集整理的spring在WEB中的应用。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python之路_django路由配置及
- 下一篇: PostgreSQL-JDBC疑似bug