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容器的创建在服务器启动时就会创建
结果:

最新文章

  1. 奇妙的CSS之布局与定位
  2. tp中session用来做权限方法 (缓解mysql压力)
  3. Linux修改环境变量的方法
  4. X86调用约定
  5. Codevs 1009 产生数
  6. CentOS SSH安装与配置
  7. 609B Load Balancing
  8. BZOJ 1640: [Usaco2007 Nov]Best Cow Line 队列变换
  9. 隐藏和显示 ng-show ng-hide
  10. vm拷贝cloudera-scm-agent造成问题
  11. Byte[]和BASE64之间的转换
  12. CSS之样式属性(背景固定、圆形头像、模态框)
  13. css 文本设置
  14. oracle存储大文本clob、blob
  15. Find Amir CodeForces - 805C (贪心+思维)
  16. Atitit 错误处理机制:(1)静默模式(2)警告模式 (3)异常模式
  17. 创建属性Attribute
  18. Java中的包学习笔记
  19. 重启ngix失败
  20. ASCII 、UTF-8、Unicode都是个啥啊,为啥会乱码啊?

热门文章

  1. MYSQL数年库安装
  2. LGP5430题解
  3. SourceTree代码变更和FoxMail邮件管理(效率小计俩)
  4. ArcMap从建库到出图
  5. Excel文件导入SQL Server数据库
  6. 闲聊系列之 5-why root cause分析法
  7. P5018 [NOIP2018 普及组] 对称二叉树
  8. 程序语言与编程实践7-&gt; Java实操4 | 第三周作业及思路讲解 | 异常处理考察
  9. springMVC的执行流程?
  10. MySQL 里记录货币用什么字段类型好?