当前位置: 首页 > news >正文

帝国cms 孕婴网站模板微信网站开发简单

帝国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-基本概念。
http://www.pierceye.com/news/719130/

相关文章:

  • 网站子目录怎么做国内做的比较好的二手网站
  • 短链生成网站html模板免费十个网页
  • 图跃企业网站建设seo提供服务
  • 厦门市建设管理协会网站发帖效果好的网站
  • 手机商城网站制作网页设计与制作的岗位职责
  • 教学网站系统流程图wordpress激活主题
  • 北京房地产网站建设做app还是做微网站好
  • 网站建设的整个流程管理咨询公司网站
  • 长沙网站建设有限公司怎么做网站赚大钱
  • 找做网站页的在哪找沭阳建设局网站
  • 私人做网站有什么用不断加强门户网站建设
  • WordPress简单百度站长插件使用cms建设网站安全吗
  • 响水做网站价格余江网站建设
  • 好的免费个人网站网站建设所需要的材料
  • 南宁本地网站有哪些建筑工程网络组网
  • 如何构建一个网站wordpress更换ssl
  • 做电影网站需要注意什么安徽易企建站
  • 莆田网站格在哪里做网站数据维护
  • 建设的网站别人登录密码做个企业网站多少钱
  • 邢台网站推广怎么做好网站推理
  • 网站项目需求盐城网站开发基本流程
  • 桐乡市城乡规划建设局网站网站企业备案和个人备案的区别
  • 公职人员可以做公益网站吗dw做的网站怎么放到服务器上
  • wordpress 导航网站模板wordpress建立企业网站
  • 厦门网站建设哪里好青岛做网站电话
  • 中国做网站正邦自己建站模板
  • 网站设计公司如何盈利安阳做网站多少钱
  • 简述网站开发的主要阶段邢台短视频推广
  • 黑彩网站充值就给你做单子青岛高品质网站建设
  • 网站建设是固定资产还是列费用矿泉水瓶50种手工制作