惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
爱范儿
爱范儿
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
Y
Y Combinator Blog
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
M
MIT News - Artificial intelligence
博客园 - Franky
V
Visual Studio Blog
I
InfoQ
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
博客园 - 司徒正美
L
LangChain Blog

博客园 - patrickwai

java enum用法 Java中哈希表(Hashtable)是如何实现的 注意for循环中变量的作用域 用SAX Parser(Simple API for XML)分析RSS android权限 【转】分治法求最大值最小值 JPA实体关系映射 JPA基本实体映射学习 Servlet实现基本文件上传 Servlet学习(一) 用javascript与正则表达式验证表单 - patrickwai - 博客园 java题目分析 java中的synchronized Spring学习——依赖注入 xml,javascript,XMLHttpRequest 学习笔记 向依赖关系宣战——依赖倒置、控制反转和依赖注入辨析[转载] MySql存储过程学习 自己用C语言写的扫雷算法 移位运算实际应用——判断整数N是否为2的阶次方
Spring学习——Hello World
patrickwai · 2010-06-05 · via 博客园 - patrickwai

开发环境jdk1.6,spring 3.0.2,spring tool suit。

1.新建一个java工程,在lib中加入如下jar包

2.写一个HelloWorld类,写一个sayHello()方法。

package com.mydomain

public class HelloWorld {
public

void sayHello(){
System.out.println(
"Hello World!");
}
}

3.类路径中创建bean配置文件bean.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-3.0.xsd"
><bean name="helloWorld" class="HelloWorld"></bean></beans>

4.写一个Main类测试HelloWorld.在这里用到用到接口ApplicationContext和ClassPathXmlApplicationContext。

代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
HelloWorld hello
= (HelloWorld)context.getBean("helloWorld");
hello.sayHello();
}

}

ApplicationContext是一个bean factory,可以存放各种bean和bean dependencies的注册。

ClassPathXmlApplicationContext是ApplicationContext的一个实现。

getBean(Stringname)取得配置的实例.然后调用方法sayHello().

5.运行程序。输出结果:

从控制台可以看到一些信息如下: 

代码

2010-6-5 21:51:55 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@758fc9:
 startup date [Sat Jun
05 21:51:55 CST 2010]; root of context hierarchy
2010-6-5 21:51:56 org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
信息: Loading XML bean definitions from
class path resource [bean.xml]
2010-6-5 21:51:57 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre
-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ef5502:
defining beans [helloWorld]; root of factory hierarchy
Hello World
!