如何在web应用中使用Spring配置文件

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/88597322

什么意思?

一般情况都是在某个.class类里面的main方法里面,写下如下代码就可以访问applicationContext.xml文件里面的内容

ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
        Regedit re=(Regedit) ioc.getBean("regedit");
        re.login();

以上仅仅是我们在学习Spring框架的时候的做法,通常情况下我们是从前台页面一个连接或者一个请求,去加载这个applicationContext.xml文件的!


如何在web应用中使用Spring配置文件?

我们想前后台通过什么交互呢?

java类(不能,除非开启线程、或者死循环,不然的话 类执行结束程序就结束运行了)

所以只能是servlet,servlet是多线程,语法上是java ,可以接收jsp里面的参数可以传递参数到jsp页面!


第一种:原始方法

jar包部分:

jar包 Spring所需的jar包+ 

如果你是ssh框架,那么完整的jar包在这里

https://download.csdn.net/download/qq_37591637/10977763

 web.xml部分

作用:在web程序加载的时候,就开启监听,一旦涉及到关于spring的,就找到applicationContext.xml文件的位置

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
   <listener> 
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
   </listener>
	

servlet部分代码

这样写的目的,因为servlet也是java类,所以java里面的写法也适用于servlet

package cn.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.service.Regedit;

public class HelloServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
		Regedit re=(Regedit) ioc.getBean("regedit");
		re.login();
	}

}

前台页面

<body>
   <a href="HelloServlet">你好</a>
  </body>

applicationContext.xml文件配置不变


第二种:重命名法

1、jar包设置和第一种一样

2、web.xml

注意:注意:注意

<listener-class>

这个是ServletContextListener接口实现类的路径,这个实现类需要我们去写代码实现,后面会讲这个类

</listener-class>

  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
  <listener>
		<listener-class>cn.com.listen.SpringServletContextListener</listener-class>
	</listener> 

3、ServletContextListener接口实现类SpringServletContextListener

内容如下

注意

1、contextConfigLocation是在web.xml里面<param-name>contextConfigLocation</param-name>设置的,要一致

2、"app"这个名称随意,但是在servlet里面会用到,也要一致

package cn.com.listen;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringServletContextListener implements ServletContextListener{
//	起别名的监听器
    public void contextInitialized(ServletContextEvent servletContextEvent) {
    //从上下文中
        ServletContext servletContext = servletContextEvent.getServletContext();
        String config=servletContext.getInitParameter("contextConfigLocation");
        ApplicationContext ioc=new ClassPathXmlApplicationContext(config);
        servletContext.setAttribute("app", ioc);
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
    public SpringServletContextListener(){}
}

 

servlet部分

package cn.com.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 cn.com.service.Regedit;
public class RegeditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext ser = getServletContext();
		ApplicationContext ioc = (ApplicationContext) ser.getAttribute("app");
		Regedit re=ioc.getBean(Regedit.class);
		re.login();
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
	}

}

前台页面都一样

   <a href="HelloServlet">你好</a>

applicationContext.xml文件配置不变


喜欢我,就关注我吧,或者点赞!!哈哈哈哈

猜你喜欢

转载自blog.csdn.net/qq_37591637/article/details/88597322