Spring温习(1)--最基础的示例
从现在开始,我将从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
找到后将多余的删除,留下最基本的
- <span style="font-size: medium;"><span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- </beans></span></span>
UserDAO.java
- <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
- public interface UserDAO {
- void say();
- }</span></span><span style="font-size: large;">
- </span>
UserDAOImpl.java
- <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
- public class UserDAOImpl implements UserDAO {
- @Override
- public void say() {
- System.out.println("i can speak");
- }
- }</span></span><span style="font-size: large;">
- </span>
applictionContext.xml
- <span style="font-size: medium;"><span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
- <beans>
- <bean id="userDAO" class="com.test.domain.UserDAOImpl"/>
- </beans></span></span><span style="font-size: large;">
- </span>
测试类
- <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- public class MyTest {
- @Test
- public void testUser(){
- ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
- UserDAO dao=(UserDAO)context.getBean("userDAO");
- dao.say();
- }
- }</span></span><span style="font-size: large;">
- </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有三种方式:
使用类构造器直接实例化
使用静态工厂的方法实例化
使用实例工厂方法实例化
具体对应配置如
- <span style="font-size: medium;"><span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
- <beans>
- <!--Spring的实例化Bean有三种方式:-->
- <!-- 使用类构造器直接实例化 -->
- <bean id="userDAO" class="com.test.domain.UserDAOImpl"/>
- <!-- 使用静态工厂的方法实例化 -->
- <bean id="userDAO1" class="com.test.domain.BeanFactory" factory-method="UserDAOService" />
- <!-- 使用实例工厂方法实例化 -->
- <bean id="factory" class="com.test.domain.BeanFactory" />
- <bean id="userDAO2" factory-bean="factory" factory-method="getUserDAOService" />
- </beans>
- </span></span>
BeanFactory.java
- <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
- public class BeanFactory {
- //使用静态工厂的方法实例化使用
- public static UserDAO UserDAOService()
- {
- return new UserDAOImpl();
- }
- public UserDAO getUserDAOService()
- {
- return new UserDAOImpl();
- }
- }</span></span><span style="font-size: medium;"><span style="font-size: large;">
- </span></span>
测试类
- <span style="font-size: medium;"><span style="font-size: large;">package com.test.domain;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- public class MyTest {
- @Test
- public void testUser(){
- ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
- UserDAO dao=(UserDAO)context.getBean("userDAO");
- dao.say();
- UserDAO dao2=(UserDAO)context.getBean("userDAO2");
- dao2.say();
- UserDAO dao3=(UserDAO)context.getBean("userDAO3");
- dao3.say();
- }
- }
- </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中引入这么多文件可以是这样写
- <span style="font-size: large;"> <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:/META-INF/spring/applicationContext-*.xml</param-value>
- </context-param></span>
2)第二种方式,也是上面那么三个配置文件,那么我们可以将-yy.xml和-zz.xml都配置在-xx.xml中去,然后再在web.xml中单独配置-xx.xml就可以
applicationContext-xx.xml
- <span style="font-size: large;"><?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <import resource="classpath:/META-INF/spring/applicationContext-yy.xml" />
- <import resource="classpath:/META-INF/spring/applicationContext-zz.xml" />
- </beans></span>
那么在web.xml中应该是
- <span style="font-size: large;"><context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/META-INF/spring/applicationContext-xx.xml</param-value>
- </context-param></span>
Spring温习(1)--最基础的示例的更多相关文章
- Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档
随着前后端分离架构和微服务架构的流行,我们使用Spring Boot来构建RESTful API项目的场景越来越多.通常我们的一个RESTful API就有可能要服务于多个不同的开发人员或开发团队:I ...
- Spring Boot 2.x基础教程:JSR-303实现请求参数校验
请求参数的校验是很多新手开发非常容易犯错,或存在较多改进点的常见场景.比较常见的问题主要表现在以下几个方面: 仅依靠前端框架解决参数校验,缺失服务端的校验.这种情况常见于需要同时开发前后端的时候,虽然 ...
- Spring Boot 2.x基础教程:Swagger接口分类与各元素排序问题详解
之前通过Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档一文,我们学习了如何使用Swagger为Spring Boot项目自动生成API文档,有不少用户留言问了关于文档 ...
- Spring Boot 2.x基础教程:Swagger静态文档的生成
前言 通过之前的两篇关于Swagger入门以及具体使用细节的介绍之后,我们已经能够轻松地为Spring MVC的Web项目自动构建出API文档了.如果您还不熟悉这块,可以先阅读: Spring Boo ...
- Spring Boot 2.x基础教程:使用国产数据库连接池Druid
上一节,我们介绍了Spring Boot在JDBC模块中自动化配置使用的默认数据源HikariCP.接下来这一节,我们将介绍另外一个被广泛应用的开源数据源:Druid. Druid是由阿里巴巴数据库事 ...
- Spring Boot 2.x基础教程:找回启动日志中的请求路径列表
如果您看过之前的Spring Boot 1.x教程,或者自己原本就对Spring Boot有一些经验,或者对Spring MVC很熟悉.那么对于Spring构建的Web应用在启动的时候,都会输出当前应 ...
- Spring Boot 2.x基础教程:使用MyBatis的XML配置方式
上一篇我们介绍了如何在Spring Boot中整合我们国人最常用的MyBatis来实现对关系型数据库的访问.但是上一篇中使用了注解方式来实现,而对于很多MyBatis老用户还是习惯于XML的开发方式, ...
- Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置
上一篇我们介绍了在使用JdbcTemplate来做数据访问时候的多数据源配置实现.接下来我们继续学习如何在使用Spring Data JPA的时候,完成多数据源的配置和使用. 添加多数据源的配置 先在 ...
- Spring Boot 2.x基础教程:事务管理入门
什么是事务? 我们在开发企业应用时,通常业务人员的一个操作实际上是对数据库读写的多步操作的结合.由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻 ...
随机推荐
- HTML每日学习笔记(3)
7.17.2019 XHTML 1.XHTML与HTML的区别: XHTML 元素必须被正确地嵌套. XHTML 元素必须被关闭. 标签名必须用小写字母. XHTML 文档必须拥有根元素.(所有的 X ...
- 混合开发 h5+ 沉浸式的适配
1.需要在mainfest.json plus对象里添加 "statusbar": { "immersed": "true", " ...
- LeetCode | 707. 设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- 手把手教你用GoEasy实现Websocket IM聊天
经常有朋友问起GoEasy如何实现IM,今天就手把手的带大家从头到尾用GoEasy实现一个完整IM聊天,全套代码已经放在了github. 今日的前端技术发展可谓百花争鸣,为了确保本文能帮助到使用任何技 ...
- eclipse操作快捷键
Eclipse最全快捷键,熟悉快捷键可以帮助开发事半功倍,节省更多的时间来用于做有意义的事情. Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ ...
- hdu2203kmp匹配
拼接字符串即可解决移位的问题: 代码如下: #include<bits/stdc++.h> using namespace std; typedef unsigned int ui; ty ...
- JDBC开源框架:DBUtils自定义业务类型相关转换器
dbutils提供的handler转换不能满足实际业务开发的需求.比如枚举转int,时间类型LocalDateTime,实体对象的属性名与字段未能相对应. mysql表member结构字段: id.m ...
- CompletableFuture异步编排
什么是CompletableFuture CompletableFuture是JDK8提供的Future增强类.CompletableFuture异步任务执行线程池,默认是把异步任务都放在ForkJo ...
- OpenCV -Python 性能衡量和提升技术 | 十二
目标 在图像处理中,由于每秒要处理大量操作,因此必须使代码不仅提供正确的解决方案,而且还必须以最快的方式提供.因此,在本章中,你将学习 衡量代码的性能. 一些提高代码性能的技巧. 你将看到以下功能:c ...
- ICCV 2019|70 篇论文抢先读,含目标检测/自动驾驶/GCN/等(提供PDF下载)
虽然ICCV2019已经公布了接收ID名单,但是具体的论文都还没放出来,为了让大家更快得看论文,我们汇总了目前已经公布的大部分ICCV2019 论文,并组织了ICCV2019论文汇总开源项目(http ...