从现在开始,我将从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. 程序员找工作必备 PHP 基础面试题

    1.优化 MYSQL 数据库的方法 (1) 选取最适用的字段属性,尽可能减少定义字段长度,尽量把字段设置 NOT NULL, 例如’省份,性别’, 最好设置为 ENUM (2) 使用连接(JOIN)来 ...

  2. 从一个小例子引发的Java内存可见性的简单思考和猜想以及DCL单例模式中的volatile的核心作用

    环境 OS Win10 CPU 4核8线程 IDE IntelliJ IDEA 2019.3 JDK 1.8 -server模式 场景 最初的代码 一个线程A根据flag的值执行死循环,另一个线程B只 ...

  3. iOS开发如何面对疫情过后的面试高峰期 !

    2020年本应该是一个 "爱你.爱你"的年份!却因为 黑天鹅 给我们带来非常大的影响! 一.2020年iOS招聘数据分析 这里是 2020年3月份BOSS直聘 北京iOS招聘前几页 ...

  4. JDK dump

    1. 查看整个JVM内存状态 jmap -heap 1237(pid) 2.生成dump文件 jmap -dump:file=文件名.dump 1237(pid)

  5. JSP(一)----入门学习

    ##  JSP 1.概念: *  Java  Server  Pages:java服务端页面 *  可以理解为:一个特殊的页面,其中既可以直接定义html标签,又可以定义java代码 2.原理 *  ...

  6. [并查集+逆向思维]Codeforces Round 722C Destroying Array

    Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. Qt 的信号与槽(纯干货)

    接触Qt断断续续有些时间了,总看了一堆的文章说信号槽的概念,心里就想骂人,做为一个初学者,最重要的就是怎么写代码,写代码写多了,再去看理论,有时水到渠成的就明白那些理论了.但所有讲信号槽的都把一堆信号 ...

  8. 超越Mask-RCNN:谷歌大脑的AI,自己写了个目标检测AI

    这是一只AI生出的小AI. 谷歌大脑的Quoc Le团队,用神经网络架构搜索 (NAS) ,发现了一个目标检测模型.长这样: △ 看不清请把手机横过来 它的准确率和速度都超过了大前辈Mask-RCNN ...

  9. 一、配置Ubuntu网络设置大纲

    root@ubuntu:为我的Ubuntu系统,即 用户名@主机名: 1.改主机名 ifconfig查询本机IP地址vim  /etc/hostname进入i编辑更改,改完按esc键 然后:wq!保存 ...

  10. 理解MapReduce计算构架

    用Python编写WordCount程序任务 程序 WordCount 输入 一个包含大量单词的文本文件 输出 文件中每个单词及其出现次数(频数),并按照单词字母顺序排序,每个单词和其频数占一行,单词 ...