帝国cms 孕婴网站模板,微信网站开发简单,三维培训学费一般多少,沈阳网站的建设一、引子
前面我们在Spring集成Junit中为读者引出了Spring善于集成其它框架的优势#xff0c;而Spring项目不可能仅限于小范围的某个方法的测试#xff0c;终究会落脚于Web项目上。于是#xff0c;我们就从这里正式进入Spring集成Web的话题。由于笔者会从原生的Java Web开发…一、引子
前面我们在Spring集成Junit中为读者引出了Spring善于集成其它框架的优势而Spring项目不可能仅限于小范围的某个方法的测试终究会落脚于Web项目上。于是我们就从这里正式进入Spring集成Web的话题。由于笔者会从原生的Java Web开发过渡到Spring集成Web从而体现出Spring集成的魅力因此在阅读这篇文章之前希望读者已经有关于Java Web的一些基础可先浏览Java Web专栏包括ServletTomcatJSP等。
这里笔者想多说几句虽然现在主流的开发似乎都不再与ServletTomcatJSP之流直接接触了但是理解一个技术是要理解这个技术为什么产生是解决了当时的什么问题这样能帮助我们将不断迭代的技术连贯起来因此我们就需要了解这门技术产生之前使用的是什么技术这样会让我们更深入地理解现在使用的技术的优势。Servlet是Web应用的基石虽不直接使用却是必要掌握的Tomcat这里泛指Web服务器似乎在SpringBoot的兴起后而不再需要像此前一样关注但是Web项目离不开TomcatJSP更是一门难用、逐渐被摒弃的语言虽然JSP是过时的技术但依旧不妨去学习到能看懂JSP代码的程度这样你将更能理解什么是前后端分离。重要的是当你慢慢熟悉技术的迭代发展你会对一些似乎耳熟能详的词语感觉到豁然开朗而不是看似理解了这些概念。
二、仅用Spring框架如何实现
1利用我们上一篇介绍的Spring注解开发声明一个配置类
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;Configuration
ComponentScan(com.bylearning)
public class SpringConfiguration {
}2定义Dao层与Service层类注解在类上添加注解表明将此类交给Spring容器
import org.springframework.stereotype.Repository;Repository
public class UserDao {public void save() {System.out.println(save successfully...);}
}Service层注入Dao层对象属性
import com.bylearning.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;Service
public class UserService {Autowiredprivate UserDao userDao;public void saveUser() {userDao.save();}
}Web层先延用在Java Web中使用的Servlet
import com.bylearning.config.SpringConfiguration;
import com.bylearning.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;WebServlet(/demo)
public class DemoServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) {ApplicationContext ioc new AnnotationConfigApplicationContext(SpringConfiguration.class);UserService us ioc.getBean(UserService.class);us.saveUser();}
}配置Tomcat启动项目在浏览器地址栏中访问该Servlet便可在控制台中看到Dao层中打印的输出语句。至此便完成了原生的Spring 对于Web的开发。
三、在此基础上怎么改进
我们注意到在DemoServlet类中我们为了获取到容器中的Service层对象利用配置类创建了一个IoC容器。我们不禁想到难道每个Servlet中都要重复这一段代码吗这不仅是代码的重复可以想象更是性能的负担我们不可能这么做。有经验的小伙伴可能会想到一个办法提供一个静态方法来获取单例的IoC容器呗。例如这样
import com.bylearning.config.SpringConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class IoCUtil {public static ApplicationContext ioc;static {ioc new AnnotationConfigApplicationContext(SpringConfiguration.class);}public static ApplicationContext getIoC() {return ioc;}
}但是我们根据Java类加载顺序知道静态代码块会在类第一次被加载的时候自动执行这意味着创建容器的耗时操作将落在第一次被访问的Servlet时这似乎也不太优雅。
解决办法还记得我们在介绍Java Web阶段简单提及的三大组件之一——Listener回顾。监听器中其中有一个ServletContextListener就是监听ServletContext对象的创建与销毁。因此我们在启动Web项目时会自动执行该监听器的contextInitialized()方法。我们把创建容器的逻辑放在这个方法里很优雅了。
监听类中创建IoC容器
import com.bylearning.config.SpringConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;WebListener
public class ContextLoaderListener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {System.out.println(listener init...);ApplicationContext ioc new AnnotationConfigApplicationContext(SpringConfiguration.class);ServletContext servletContext servletContextEvent.getServletContext();servletContext.setAttribute(ioc, ioc);}Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {}}提供一个工具类
import org.springframework.context.ApplicationContext;import javax.servlet.ServletContext;public class WebApplicationContextUtils {public static ApplicationContext getWebApplicationContext(ServletContext servletContext) {ApplicationContext ioc (ApplicationContext) servletContext.getAttribute(ioc);return ioc;}
}改造Servlet获取Service层对象
import com.bylearning.WebApplicationContextUtils;
import com.bylearning.service.UserService;
import org.springframework.context.ApplicationContext;import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;WebServlet(/demo)
public class DemoServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) {ApplicationContext ioc WebApplicationContextUtils.getWebApplicationContext(getServletContext());UserService us ioc.getBean(UserService.class);us.saveUser();}
}四、Spring为我们做好了——Spring集成Web
如此典型的优化动作Spring作为一个优秀的框架当然为我们做好了。于是我们要开始介绍Spring集成Web。
1、引入依赖
!-- https://mvnrepository.com/artifact/org.springframework/spring-web --dependencygroupIdorg.springframework/groupIdartifactIdspring-web/artifactIdversion5.3.1/version/dependency
2、使用spring-web依赖中提供的工具类。我们此时删除import重新引入时发现了两个工具类供我们选择一个是我们在上一步中自己实现的下一个是spring-web提供的。我们选择下面那一个。 3、在web.xml里配置监听类。不用我们自己在定义监听类了只需要在web.xml里配置好并将Spring配置文件作为全局参数配置好。前面我们特意设计将我们自定义的监听类与spring-web提供的监听类名称一致。 context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:applicationContext.xml/param-value/context-paramlistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener
4、Spring配置文件。进行包扫描即可
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd!--扫描控制层组件--context:component-scan base-packagecom.bylearning//beans
五、总结
至此我们便完成了Spring集成Web的介绍我们可以看到我们只需要完成监听器的配置与初始参数的配置接着就可以利用工具类获取到IoC容器了。接下来我们将为读者继续介绍SpringMVC的内容。
读者在阅读第三、四节时可能会有一个疑问这里为什么是用一个Servlet来充当Web层呢这和当下流行的Controller是什么关系呢因为我们是从原生的Java Web开发过渡而来因此采用在Java Web开发中常用的继承HttpServlet接口来承接客户端请求下面我们将为读者介绍SpringMVC的内容这将与大家熟知的Controller联系起来请读者继续阅读SpringMVC-基本概念。