第一个spring程序
在这里我们使用xmlschema来进行spring的配置文件的描述规则:
开始我们的第一个java程序:
package com.itcast.first;
//第一个使用spring管理的bean,展示依赖注入
public class Hello {
private String helloworld;
public String getHelloworld() {
return helloworld;
}
public void setHelloworld(String helloworld) {
this.helloworld = helloworld;
}
}
编写我们的第一个配置文件,来实现反转控制ioc,依赖注入di:
<?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:aop="http://www.springframework.org/schema/aop"
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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
<!-- 我的第一个依赖注入的bean -->
<bean name="hello" class="com.itcast.first.Hello">
<property name="helloworld">
<value>hello,man</value>
</property>
</bean>
</beans>
编写我们的测试类:
package com.itcast.first;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
//测试程序,测试简单依赖注入
public class TestHello {
private static final Log logger = LogFactory.getLog(TestHello.class);
public static void main(String[] args) {
Resource rs = (Resource) new ClassPathResource("bean-config.xml");
BeanFactory factory = new XmlBeanFactory(rs);
Hello h = (Hello) factory.getBean("hello");
logger.info(h.getHelloworld());
}
}
上边就是我们的第一个spring程序。
关于spring2版本以后,配置文件用xmlschema而不用dtd的原因如下:
注意点:我们的spring的xml schema文件的编写,以及如果使用dtd,那么我们需要做的处理。
我们为什么要使用schema,而放弃dtd来表述我们的配置文件?
因DTD有着不少缺陷:
1) DTD是基于正则表达式的,描述能力有限;
2) DTD没有数据类型的支持,在大多数应用环境下能力不足;
3) DTD的约束定义能力不足,无法对XML实例文档作出更细致的语义限制;
4) DTD的结构不够结构化,重用的代价相对较高;
5) DTD并非使用XML作为描述手段,而DTD的构建和访问并没有标准的编程接口,无法使用标准的编程方式进行DTD维护。
而XML Schema正是针对这些DTD的缺点而设计的,XML Schema的优点:
1) XML Schema基于XML,没有专门的语法
2) XML可以象其他XML文件一样解析和处理
3) XML Schema支持一系列的数据类型(int、float、Boolean、date等)
4) XML Schema提供可扩充的数据模型。
5) XML Schema支持综合命名空间
6) XML Schema支持属性组。
分享到:
相关推荐
Spring Framework。 官网 Spring Framework API。 Spring Framework 开发文档。
标题中的“maven仓库中org下的springframework”指的是在Maven的本地或远程仓库中,位于`org`组织下的`springframework`项目。Spring Framework是Java开发中的一个核心框架,由Pivotal Software公司维护,它为构建...
"Spring Framework 4 参考文档中文版" 以下是 Spring Framework 4 参考文档中文版的知识点总结: 一、Spring Framework 概览 * Spring Framework 是一个轻量级的解决方案,是一站式构建企业级应用的一种选择。 * ...
1. **基本类型转换与通用工具类**:`org.springframework.core.convert`包提供了类型转换服务,允许在不同数据类型之间进行安全转换。`org.springframework.util`则包含了一系列通用的工具类,如对象处理、集合操作...
spring framework 中文参考手册
Spring Framework 是一个广泛使用的开源Java应用框架,特别适用于企业级应用程序开发。它的最新版本v6.1.4为我们带来了许多新特性和改进,使得开发者能够更高效、更灵活地构建可维护的、高性能的应用程序。这个...
Spring Framework API文档。Spring是什么呢?首先它是一个开源的项目,而且非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据...
org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包,org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3....
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....
1. **专注于管理业务对象**:Spring Framework 主要关注于为开发者提供一种高效的方式去管理和组织业务逻辑层的对象。这意味着它可以更好地处理业务相关的组件和服务,从而让开发者能够更加专注于应用程序的核心逻辑...
1. **模块化设计**:Spring Framework 分为多个模块,如Core Container(核心容器)、Data Access/Integration(数据访问/集成)、Web、AOP(面向切面编程)、Test等,这使得开发者可以根据项目需求选择需要的模块,...
Spring Framework 是Java开发中的核心框架,它以其强大的功能和易用性成为了许多开发者首选的工具。Spring Framework 6.0.8-SNAPSHOT 的中文文档是针对这一最新版本的详尽指南,旨在解决中文互联网上关于Spring框架...
Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...
Spring Framework 5.1.0源码 The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key ...
1. `org.springframework.core.convert`: 这个包包含了Spring的类型转换系统,如`Converter`和`ConverterFactory`接口,用于实现对象间的自动转换。`ConversionService`是转换服务的入口,它可以将任何对象转换为...
Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...
org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE.jar org.springframework....
Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...
在Java的Web开发领域,Spring框架是不可或缺的重要工具,其中`org.springframework.web.jar`文件是Spring框架的核心组成部分,主要用于处理Web应用程序的相关功能。这个JAR(Java Archive)文件包含了Spring Web模块...
org\springframework\aop org\springframework\beans org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\spring...