首先是bean文件:

package onlyfun.caterpillar;

public class HelloBean {
    private String helloWord = "";

public void setHelloWord(String helloWord) {
        this.helloWord = helloWord;
    }

public String getHelloWord() {
        return helloWord;
    }
}

再配置xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
        <property name="helloWord" value="Hello!Justin!"></property>
    </bean>

</beans>

最后写测试代码:

package onlyfun.caterpillar;

import java.io.*;
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 SpringTest {
    public static void main(String[] args) throws IOException {

//BeanFactory方式读取bean

//网上有用FileInputStream读bean.xml的例子,但似乎用的是老版本的spring
        Resource res = new ClassPathResource(
                "onlyfun/caterpillar/bean.xml");
        BeanFactory factory = new XmlBeanFactory(res);
        HelloBean hello = (HelloBean) factory.getBean("helloBean");
        System.out.println(hello.getHelloWord());

//         ApplicationContext方式读取bean

//         ApplicationContext application = new ClassPathXmlApplicationContext("onlyfun/caterpillar/bean.xml");
//         HelloBean hello = (HelloBean) application.getBean("helloBean");
//         System.out.println(hello.getHelloWord());
    }
}

要导入的包除了有spring的包外,还有commons-logging的包,在网上很容易下载。

本例中各包版本:

spring : spring-framework-2.5.6.SEC01

commons-logging : commons-logging-1.1.1

注意:本例中这3个文件在同一目录下


明:两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比,提供了更多的扩展功能,但其主要区别
在于后者是延迟加载,如果Bean的某一个属性没有注入,BeanFacotry加载后,直至第一次使用调用getBean方法才会抛出异常;而
ApplicationContext则在初始化自身是检验,这样有利于检查所依赖属性是否注入;所以通常情况下我们选择使用
ApplicationContext.

参考:http://dinghaoliang.blog.163.com/blog/static/126540714201021234644910/

spring实例入门的更多相关文章

  1. Spring oxm入门实例

    O/XMapper是什么? Spring3.0的一个新特性是O/XMapper.O/X映射器这个概念并不新鲜,O代表Object,X代表XML.它的目的是在Java对象(几乎总是一个plainoldJ ...

  2. Spring MVC入门的实例

      作为Spring MVC入门,以XML配置的方式为例.首先需要配置Web工程的web.xml文件. 代码清单14-1:web.xml配置Spring MVC <?xml version=&q ...

  3. spring boot 系列之一:spring boot 入门

    最近在学习spring boot,感觉确实很好用,开发环境搭建和部署确实省去了很多不必须要的重复劳动. 接下来就让我们一起来复习下. 一.什么是spring boot ? spring boot是干嘛 ...

  4. Spring Cloud 入门教程(一): 服务注册

    1.  什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...

  5. Spring Cloud入门教程(二):客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  6. Spring Cloud 入门 之 Config 篇(六)

    原文地址:Spring Cloud 入门 之 Config 篇(六) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,为了方便开发和维护项目,我们通常会将大项目拆分 ...

  7. Spring Cloud 入门 之 Zuul 篇(五)

    原文地址:Spring Cloud 入门 之 Zuul 篇(五) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口 ...

  8. Spring Cloud 入门 之 Hystrix 篇(四)

    原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...

  9. spring cloud 入门系列:总结

    从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...

随机推荐

  1. c/c++赋值函数(重载=号运算符)

    c/c++赋值函数(重载=号运算符) 首先c++里的各种运算符都是用函数实现的,比如=,就等号函数. 所以当用=给一个对象赋值的时候,实际调用的是=号所对应的=号函数. 分析下面的代码 #includ ...

  2. python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例

    第一次写一个算是比较完整的爬虫,自我感觉极差啊,代码low,效率差,也没有保存到本地文件或者数据库,强行使用了一波多线程导致数据顺序发生了变化... 贴在这里,引以为戒吧. # -*- coding: ...

  3. 后台登录(包含验证码)的php代码实现

    login.html文件 <html> <title>login in</title> <body> <form action="han ...

  4. 基于python的Selenium使用小结

    之前介绍过基于Unittest和TestNG自动化测试框架,然而基于Web端的测试的基础框架是需要Selenium做主要支撑的,这里边给大家介绍下Web测试核心之基于Python的Selenium 一 ...

  5. 《Java大学教程》--第3章 迭代

    迭代(iteration).重复(repetition):三种循环* for: 重复执行固定次数* while: 重复执行不固定次数* do...while: 比while至少多一次 1.答:P47迭 ...

  6. qianduan

    head.html <style type="text/css"> #header { height: 70px; line-height: 60px; backgro ...

  7. spring boot监控--actuator

    原文地址:http://blog.csdn.net/wh_ouyangshuang/article/details/48048111 spring-boot-actuator模块提供了一个监控和管理生 ...

  8. c#窗体获取系统时间、回车触发按钮事件、实现验证码功能

    1.窗体上显示时间: 1)首先新建一个Timer,命名为timer,并在属性中修改Interval为1000: 2)在需要显示时间的地方添加一个label(假设设置名称为:timerLabel): 3 ...

  9. camke使用例程

    1 同文件夹直接编译 1 同文件夹直接编译 # cmake needs this line cmake_minimum_required(VERSION 2.8) # Define project n ...

  10. 07 Python初学(元组)

    tuple:    元组被称为只读列表,即数据可以被查询,但不能被修改,列表的切片操作同样适用于元组. 元组写在小括号()里,元素之间用逗号隔开 虽然 tuple 的元素不可改变,但他可以包含可变的对 ...