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基础教程:事务管理入门
什么是事务? 我们在开发企业应用时,通常业务人员的一个操作实际上是对数据库读写的多步操作的结合.由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻 ...
随机推荐
- 3D游戏中各种空间变换到底是怎么回事
每一个游戏可以呈现炫丽效果的背后,需要进行一系列的复杂计算,同时也伴随着各种各样的顶点空间变换.渲染游戏的过程可以理解成是把一个个顶点经过层层处理最终转化到屏幕上的过程,本文就旨在说明,顶点是经过了哪 ...
- ajax上传文件,通过FromData把数据传给后端
前端代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- 包和访问权限修饰符,.单例设计模式,.Object类常用方法,.内部类
1.包和访问权限修饰符 1.1 包 为什么要导包? 将字节码文件(.class)文件进行分类存放 --->包 其实就是文件夹 import java.util.Arrays; 包的划分规则 方案 ...
- for循环与串行化、并行化Stream流性能对比
第四章 并行化Stream流 关注公众号(CoderBuff)回复"stream"获取<Java8 Stream编码实战>PDF完整版. <Java8 Strea ...
- hdu1171kmp果题
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1711/ #include<bits/stdc++.h> using namespace std; t ...
- KMP 算法简单解释
讲KMP算法,离不开BF,实际上,KMP就是BF升级版,主要流程和BF一样 不同是在匹配失败时能利用子串的特征减少回溯,利用根据子串特征生成的Next数组来减少 <( ̄︶ ̄)↗[GO!] ...
- tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图
tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图 因为很多 demo 都比较复杂,专门抽出这两个函数,写的 demo. 更多教程:http://www.tensorflown ...
- Attention-based Extraction of Structured Information from Street View Imagery:基于注意力的街景图像提取结构化信息
基于注意力的街景图像提取结构化信息 一种用于真实图像文本提取问题的TensorFlow模型. 该文件夹包含在FSNS数据集数据集上训练新的注意OCR模型所需的代码,以在法国转录街道名称. 您还可以使用 ...
- js事件的获取
获取元素样式属性 Method DES clientWidth 获取元素宽度 clientHeight 获取元素高度(内容+内边距) document.body.clientWidth 获取body宽 ...
- html vue简单
1.Vue 简单的替换更新 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...