Spring温习(1)--最基础的示例

Spring温习(1)--最基础的示例

博客分类:

SpringXMLBeanWebDAO

从现在开始,我将从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

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

Java代码  

  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

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

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

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>

测试类

Java代码  

  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有三种方式:

使用类构造器直接实例化

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

使用实例工厂方法实例化

具体对应配置如

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. <!--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

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>

测试类

Java代码  

  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中引入这么多文件可以是这样写

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

Java代码  

  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中应该是

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>
时间: 2024-12-26 18:55:54

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

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

随着前后端分离架构和微服务架构的流行,我们使用Spring Boot来构建RESTful API项目的场景越来越多.通常我们的一个RESTful API就有可能要服务于多个不同的开发人员或开发团队:IOS开发.Android开发.Web开发甚至其他的后端服务等.为了减少与其他团队平时开发期间的频繁沟通成本,传统做法就是创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型.HTTP头部信息.HTTP请求内容

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

上一节,我们介绍了Spring Boot在JDBC模块中自动化配置使用的默认数据源HikariCP.接下来这一节,我们将介绍另外一个被广泛应用的开源数据源:Druid. Druid是由阿里巴巴数据库事业部出品的开源项目.它除了是一个高性能数据库连接池之外,更是一个自带监控的数据库连接池.虽然HikariCP已经很优秀,但是对于国内用户来说,可能对于Druid更为熟悉.所以,对于如何在Spring Boot中使用Druid是后端开发人员必须要掌握的基本技能. 配置Druid数据源 这一节的实践我们

Spring源码阅读:Spring事务管理的基础

上一节了解了全局事务与局部事务以及Spring提供的两种事务模式:编程式事务与声明式事务. 不论是编程式的事务处理,还是声明式的事务处理.他们都要对局部事务和全局事务以支持,也就是说要对JDBC进行支持.ORM框架,同时也要对JTA进行支持.他们的公共部分是commit,rollback.通过这一节的了解,我相信以后配置Spring事务时,就不需要在去网上查资料了或者去查Spring的参考文档了. 因此,Spring设计了如下的事务管理框架: 从上面的类图中和容易可以看出分为三部分:Platfo

spring batch(一):基础部分

spring batch(一):基础部分 博客分类: Spring java spring batch 官网: http://www.springsource.org/spring-batch 下载页面: http://static.springsource.org/spring-batch/downloads.html 文档: http://static.springsource.org/spring-batch/reference/index.html 数据库表格创建连接:DDL http:

docker-compose 构建mongodb并导入基础数据示例

使用docker-compose构建mongodb服务并导入基础数据示例. 1.文件目录结构 --mongo/ |--docker-compose.yml |--mongo-Dockerfile |--setup.sh |--data/ |--xxx1.json |--xxx2.json 2.docker-compose.yml 1 services: 2 mongo_db: 3 build: 4 context: . 5 dockerfile: mongo-Dockerfile 6 resta

Spring基础 快速入门spring(1) 基础概念

作为流行了10年以上的老将,spring依然精神矍铄,影响不减.本文将对spring很基础的概念进行介绍以及为学习spring最核心和基础的知识作环境搭建的准备. Spring官网 http://docs.spring.io/ 简介 Spring为JAVA企业级应用提供了一个较为复杂框架流行框架.spring到底能做什么,或者说spring现在能做什么? 除了spring framework之外还有spring boot/spring data/spring cloud/- 快接近无所不包了.已

Spring目录结构和基础JAR包介绍

目前 Spring 框架的最新版本是 5.1.8,本教程是基于 Spring 的稳定版本 3.2.13 进行讲解的.读者可以通过网址 http://repo.spring.io/simple/libs-release-local/org/springframework/spring/ 下载名称为 springframework-3.2.13.RELEASE-dist.zip 的压缩包.单击此链接下载,下载完成后,解压文件的目录结构如图1 所示. 图 1  解压后目录 下面对图 1所示的目录进行简

Spring MVC + Hibernate + Maven: Crud操作示例

Alexey是一个在使用Java,TestNG 和Selenium的自动化WEB应用程序中有丰富经验的测试开发者.他如此的喜欢QA以至于在下班后他为初级QA工程师提供培训课程. 在这篇文章中我想介绍一个Spring MVC + Hibernate + Maven例子.这组技术主要涉及一些基础知识,我想在每一个必要的地方详细解释它.本篇话题范围以外的更多资源,我会提供链接方便你阅读.在文章的最后,我将发布一个GitHub的链接. 目标 示例web应用程序是基于Spring MVC, Hiberna

Spring框架的一些基础知识

Spring 框架 Spring 框架是一个分层架构,由 7 个定义良好的模块组成.Spring 模块构建在核心容器之上,核心容器定义了创建.配置和管理 bean 的方式,如图 1 所示. 图 1. Spring 框架的 7 个模块 组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现.每个模块的功能如下: 核心容器:核心容器提供 Spring 框架的基本功能.核心容器的主要组件是 BeanFactory,它是工厂模式的实现.BeanFactory 使用控