从现在开始,我将从Spring为起点,逐步复习几大框架各方面的知识,以便今后查看使用

第一各Spring示例

必须包:spring-framework-2.5.6\dist\spring.jar

spring-framework-2.5.6\lib\jakarta-commons\common-logging.jar

为了方便测试还需要:spring-framework-2.5.6\lib\junit\junit4.4.jar

第一步,先在spring资源包找到:spring-framework-2.5.6\samples\jpetstore\attributes\WEB-INF\applictionContext.xml

找到后将多余的删除,留下最基本的

  1. <span style="font-size: medium;"><span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  9. </beans></span></span>

UserDAO.java

  1. <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
  2. public interface UserDAO {
  3. void say();
  4. }</span></span><span style="font-size: large;">
  5. </span>

UserDAOImpl.java

  1. <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
  2. public class UserDAOImpl implements UserDAO {
  3. @Override
  4. public void say() {
  5. System.out.println("i can speak");
  6. }
  7. }</span></span><span style="font-size: large;">
  8. </span>

applictionContext.xml

  1. <span style="font-size: medium;"><span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
  3. <beans>
  4. <bean  id="userDAO" class="com.test.domain.UserDAOImpl"/>
  5. </beans></span></span><span style="font-size: large;">
  6. </span>

测试类

  1. <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import org.springframework.web.context.WebApplicationContext;
  6. import org.springframework.web.context.support.WebApplicationContextUtils;
  7. public class MyTest {
  8. @Test
  9. public void testUser(){
  10. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  11. UserDAO dao=(UserDAO)context.getBean("userDAO");
  12. dao.say();
  13. }
  14. }</span></span><span style="font-size: large;">
  15. </span>

测试结果:i can speak

Spring加载XML配置文件的方式

spring 中加载xml配置文件的方式,好像有3种, xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean,包括:     XmlBeanFactory ,     ClassPathXmlApplicationContext ,     FileSystemXmlApplicationContext ,     XmlWebApplicationContext

一、XmlBeanFactory 引用资源     Resource resource = new ClassPathResource("appcontext.xml");     BeanFactory factory = new XmlBeanFactory(resource); 二、ClassPathXmlApplicationContext  编译路径     1)ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");     2)ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");   // src目录下的     3)ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml");   // src/conf 目录下的     4)ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");

5)String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};       ApplicationContext ctx = new ClassPathXmlApplication(locations);

三 、 用文件系统的路径    1) ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml");     //使用了  classpath:  前缀,作为标志,  这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径     2)ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");     3)ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");     4)ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");

5)String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};         ApplicationContext ctx = new FileSystemXmlApplicationContext(locations );

四、XmlWebApplicationContext   是专为Web工程定制的。     ServletContext servletContext = request.getSession().getServletContext();     ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );

注:其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的

Spring的实例化Bean有三种方式:

使用类构造器直接实例化

使用静态工厂的方法实例化

使用实例工厂方法实例化

具体对应配置如

  1. <span style="font-size: medium;"><span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
  3. <beans>
  4. <!--Spring的实例化Bean有三种方式:-->
  5. <!-- 使用类构造器直接实例化 -->
  6. <bean  id="userDAO" class="com.test.domain.UserDAOImpl"/>
  7. <!-- 使用静态工厂的方法实例化 -->
  8. <bean id="userDAO1" class="com.test.domain.BeanFactory" factory-method="UserDAOService" />
  9. <!-- 使用实例工厂方法实例化 -->
  10. <bean id="factory" class="com.test.domain.BeanFactory" />
  11. <bean id="userDAO2" factory-bean="factory" factory-method="getUserDAOService" />
  12. </beans>
  13. </span></span>

BeanFactory.java

  1. <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
  2. public class BeanFactory {
  3. //使用静态工厂的方法实例化使用
  4. public static UserDAO UserDAOService()
  5. {
  6. return new UserDAOImpl();
  7. }
  8. public UserDAO getUserDAOService()
  9. {
  10. return new UserDAOImpl();
  11. }
  12. }</span></span><span style="font-size: medium;"><span style="font-size: large;">
  13. </span></span>

测试类

  1. <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import org.springframework.web.context.WebApplicationContext;
  6. import org.springframework.web.context.support.WebApplicationContextUtils;
  7. public class MyTest {
  8. @Test
  9. public void testUser(){
  10. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  11. UserDAO dao=(UserDAO)context.getBean("userDAO");
  12. dao.say();
  13. UserDAO dao2=(UserDAO)context.getBean("userDAO2");
  14. dao2.say();
  15. UserDAO dao3=(UserDAO)context.getBean("userDAO3");
  16. dao3.say();
  17. }
  18. }
  19. </span></span>

测试结果

i can speak

i can speak

i can speak

PS:Spring的配置文件引入方式

1)传统配置多个文件,applicationContext-xx.xml,applicationContext-yy.xml,applicatonContext-zz.xml

那么在web.xml中引入这么多文件可以是这样写

  1. <span style="font-size: large;"> <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:/META-INF/spring/applicationContext-*.xml</param-value>
  4. </context-param></span>

2)第二种方式,也是上面那么三个配置文件,那么我们可以将-yy.xml和-zz.xml都配置在-xx.xml中去,然后再在web.xml中单独配置-xx.xml就可以

applicationContext-xx.xml

  1. <span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  5. <import resource="classpath:/META-INF/spring/applicationContext-yy.xml" />
  6. <import resource="classpath:/META-INF/spring/applicationContext-zz.xml" />
  7. </beans></span>

那么在web.xml中应该是

  1. <span style="font-size: large;"><context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath*:/META-INF/spring/applicationContext-xx.xml</param-value>
  4. </context-param></span>

Spring温习(1)--最基础的示例的更多相关文章

  1. Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档

    随着前后端分离架构和微服务架构的流行,我们使用Spring Boot来构建RESTful API项目的场景越来越多.通常我们的一个RESTful API就有可能要服务于多个不同的开发人员或开发团队:I ...

  2. Spring Boot 2.x基础教程:JSR-303实现请求参数校验

    请求参数的校验是很多新手开发非常容易犯错,或存在较多改进点的常见场景.比较常见的问题主要表现在以下几个方面: 仅依靠前端框架解决参数校验,缺失服务端的校验.这种情况常见于需要同时开发前后端的时候,虽然 ...

  3. Spring Boot 2.x基础教程:Swagger接口分类与各元素排序问题详解

    之前通过Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档一文,我们学习了如何使用Swagger为Spring Boot项目自动生成API文档,有不少用户留言问了关于文档 ...

  4. Spring Boot 2.x基础教程:Swagger静态文档的生成

    前言 通过之前的两篇关于Swagger入门以及具体使用细节的介绍之后,我们已经能够轻松地为Spring MVC的Web项目自动构建出API文档了.如果您还不熟悉这块,可以先阅读: Spring Boo ...

  5. Spring Boot 2.x基础教程:使用国产数据库连接池Druid

    上一节,我们介绍了Spring Boot在JDBC模块中自动化配置使用的默认数据源HikariCP.接下来这一节,我们将介绍另外一个被广泛应用的开源数据源:Druid. Druid是由阿里巴巴数据库事 ...

  6. Spring Boot 2.x基础教程:找回启动日志中的请求路径列表

    如果您看过之前的Spring Boot 1.x教程,或者自己原本就对Spring Boot有一些经验,或者对Spring MVC很熟悉.那么对于Spring构建的Web应用在启动的时候,都会输出当前应 ...

  7. Spring Boot 2.x基础教程:使用MyBatis的XML配置方式

    上一篇我们介绍了如何在Spring Boot中整合我们国人最常用的MyBatis来实现对关系型数据库的访问.但是上一篇中使用了注解方式来实现,而对于很多MyBatis老用户还是习惯于XML的开发方式, ...

  8. Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置

    上一篇我们介绍了在使用JdbcTemplate来做数据访问时候的多数据源配置实现.接下来我们继续学习如何在使用Spring Data JPA的时候,完成多数据源的配置和使用. 添加多数据源的配置 先在 ...

  9. Spring Boot 2.x基础教程:事务管理入门

    什么是事务? 我们在开发企业应用时,通常业务人员的一个操作实际上是对数据库读写的多步操作的结合.由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻 ...

随机推荐

  1. HTML每日学习笔记(3)

    7.17.2019 XHTML 1.XHTML与HTML的区别: XHTML 元素必须被正确地嵌套. XHTML 元素必须被关闭. 标签名必须用小写字母. XHTML 文档必须拥有根元素.(所有的 X ...

  2. 混合开发 h5+ 沉浸式的适配

    1.需要在mainfest.json plus对象里添加 "statusbar": { "immersed": "true", " ...

  3. LeetCode | 707. 设计链表

    设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...

  4. 手把手教你用GoEasy实现Websocket IM聊天

    经常有朋友问起GoEasy如何实现IM,今天就手把手的带大家从头到尾用GoEasy实现一个完整IM聊天,全套代码已经放在了github. 今日的前端技术发展可谓百花争鸣,为了确保本文能帮助到使用任何技 ...

  5. eclipse操作快捷键

    Eclipse最全快捷键,熟悉快捷键可以帮助开发事半功倍,节省更多的时间来用于做有意义的事情. Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ ...

  6. hdu2203kmp匹配

    拼接字符串即可解决移位的问题: 代码如下: #include<bits/stdc++.h> using namespace std; typedef unsigned int ui; ty ...

  7. JDBC开源框架:DBUtils自定义业务类型相关转换器

    dbutils提供的handler转换不能满足实际业务开发的需求.比如枚举转int,时间类型LocalDateTime,实体对象的属性名与字段未能相对应. mysql表member结构字段: id.m ...

  8. CompletableFuture异步编排

    什么是CompletableFuture CompletableFuture是JDK8提供的Future增强类.CompletableFuture异步任务执行线程池,默认是把异步任务都放在ForkJo ...

  9. OpenCV -Python 性能衡量和提升技术 | 十二

    目标 在图像处理中,由于每秒要处理大量操作,因此必须使代码不仅提供正确的解决方案,而且还必须以最快的方式提供.因此,在本章中,你将学习 衡量代码的性能. 一些提高代码性能的技巧. 你将看到以下功能:c ...

  10. ICCV 2019|70 篇论文抢先读,含目标检测/自动驾驶/GCN/等(提供PDF下载)

    虽然ICCV2019已经公布了接收ID名单,但是具体的论文都还没放出来,为了让大家更快得看论文,我们汇总了目前已经公布的大部分ICCV2019 论文,并组织了ICCV2019论文汇总开源项目(http ...