Spring集成web环境(手动实现)
1.创建UserDao接口及其实现类UserDaoImpl(接口代码省略)
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("save running....");
}
}
2.创建UserService及其实现类UserDaoImpl(接口代码省略)
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save1() {
userDao.save();
}
}
3.将UserDaoImpl及其UserServiceImpl注入到spring容器当中
<?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">
<!-- 配置Dao-->
<bean id="userDao" class="com.hao.dao.impl.UserDaoImpl"/>
<!-- 配置Service-->
<bean id="userService" class="com.hao.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
4.编写UserServlet
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean(UserService.class);
service.save1();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
5.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.hao.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6.启动tomcat服务器,在浏览器访问userServlet
我们思考这一段代码
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean(UserService.class);
service.save1();
}
当我们每次访问userServlet时,是不是都会创建一个spring容器,这样是不是造成了很大地问题,还有一个问题就是我们在用户访问userServlet时才创建spring容器,是不是会造成效率底下地问题,所以引入了监听器的概念,我们使用了范围最大的ServletContextListener监听器(用来监听ServletContext,ServletContext对象在服务器启动时就会创建,在初始化方法时就创建spring容器,提供访问效率;
创建普通类,让它实现ServletContextListener接口
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//将spring应用上下文对象存储到ServletContext域中
ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.setAttribute("app",context);
System.out.println("spring容器创建完毕!");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
在web.xml中配置监听器
<listener>
<listener-class>com.hao.listener.ContextLoaderListener</listener-class>
</listener>
修改UserServlet类里面的代码
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
ServletContext servletContext = this.getServletContext();
ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
UserService userService = app.getBean(UserService.class);
userService.save1();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
然后启动服务器,访问userServlet(spring容器的创建在服务器启动时就会创建
结果:
Spring集成web环境(手动实现)的更多相关文章
- Spring集成web环境(使用封装好的工具)
接上文spring集成web环境(手动实现) ##########代码接上文############# spring提供了一个监听器ContextLoaderListener对上述功能的封装,该监听器 ...
- Spring与Web环境集成
1. Spring与Web环境集成 1.1 ApplicationContext应用上下文获取方式 应用上下文对象是通过new ClasspathXmlApplicationContext(sprin ...
- Spring(五)Spring与Web环境集成
MVC 是 Model.View 和 Controller 的缩写,分别代表 Web 应用程序中的 3 种职责. 模型:用于存储数据以及处理用户请求的业务逻辑. 视图:向控制器提交数据,显示模型中的数 ...
- Shiro集成web环境[Springboot]-基础使用
Shiro集成web环境[Springboot] 1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包 <dependenc ...
- Shiro集成web环境[Springboot]-认证与授权
Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...
- spring集成环境下的axis webservice的发布,调试
在spring集成的环境下,无论你是ssh集成,还是ssi集成的情况下,发布webservice往往在调用的时候会出错. 特别是,如果你是这个方式: 将webservice打aar包,放到tomcat ...
- Web环境中Spring的启动过程
1.spring不但可以在JavaSE环境中应用,在Web环境中也可以广泛应用,Spring在web环境中应用时,需要在应用的web.xml文件中添加如下的配置: …… <context-par ...
- Spring-IOC 在非 web 环境下优雅关闭容器
当我们设计一个程序时,依赖了Spring容器,然而并不需要spring的web环境时(Spring web环境已经提供了优雅关闭),即程序启动只需要启动Spring ApplicationContex ...
- Shiro在Web环境下集成Spring的大致工作流程
1,Shiro提供了对Web环境的支持,其通过一个 ShiroFilter 入口来拦截需要安全控制的URL,然后进行相应的控制. ①配置的 ShiroFilter 实现类为:org.spri ...
随机推荐
- python 绘图介绍
1. python 绘图介绍 2. 函数 import numpy as np import matplotlib.pyplot as plt t = np.arange(0.0, 3.0, 0.01 ...
- 关于linux下的open()write()read()close()函数
http://blog.sina.com.cn/s/blog_71d1a98701010s0v.html 1.read和write函数调用时,都会记录下当前写的位置,下次调用时就会从这个位置开始读或写 ...
- 后端跨域问题究极解决 nginx+springboot 解决OPTIONS通过却报CORS的问题
location /joinus { # 允许跨域请求的"域",有些请求不允许* add_header 'Access-Control-Allow-Origin' $http_or ...
- python django对数据表的增删改查操作
新增操作:方式1:book = BookInfo(title='西游记',price=99)book.save() 方式2:BookInfo.objects.create(title='西游记',pr ...
- mac phpStrom 卸载
cd ~/Library/Logs/cd ~/Library/Application\ Supportcd ~/Library/Preferences/cd ~/Library/Caches/
- 面试问题之操作系统:linux线程API
https://blog.csdn.net/youwotianya/article/details/80933449
- jQuery--选择器案例实战
1.案例需求 jquery最基础的选择器部分已经基本结束,来一个简单案例总结回顾下学的东西. 案例需求: 用一个按钮控制元素的显示与隐藏,页面如下,从第五个开始,不要最后一个,控制他们的显示和隐藏. ...
- 什么是可重入锁ReentrantLock?
举例来说明锁的可重入性 public class UnReentrant{ Lock lock = new Lock(); public void outer(){ lock.lock(); inne ...
- 如何监控 Elasticsearch 集群状态?
Marvel 让你可以很简单的通过 Kibana 监控 Elasticsearch.你可以实时查看你 的集群健康状态和性能,也可以分析过去的集群.索引和节点指标.
- 数据库SQL之学习SUM总和套用条件CASE WHEN语句
1.SQL之学习SUM总和套用条件CASE WHEN语句 2.条件语句CASE WHEN 格式已经在图中写的很明白了 -- 查询t_wzw库中所有数据 总和(条件为t_wzw.birthday > ...