[b]spring2.5入门(Hello World)[/b]
----------------------------------------
其中用到的包和类
[img]http://dl.iteye.com/upload/attachment/532277/7d015039-ffd2-3b21-90f6-cc514449034e.jpg[/img]
-------------------------------------------
ShowMessage
----------------------------------------------
applicationContext.xml
----------------------------------------------
Mytest
----------------------------------------
其中用到的包和类
[img]http://dl.iteye.com/upload/attachment/532277/7d015039-ffd2-3b21-90f6-cc514449034e.jpg[/img]
-------------------------------------------
ShowMessage
package test;
public class ShowMessage {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void show() {
System.out.println(this.getMessage());
}
}
----------------------------------------------
applicationContext.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.5.xsd">
<!-- 定义了ShowMessage类的bean -->
<bean id = "myBean" class="test.ShowMessage">
<property name="message">
<value>Hello World!</value>
</property>
</bean>
</beans>
----------------------------------------------
Mytest
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String argv[]){
//获取Spring的上下文环境
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//从上下文环境中获取myBean
ShowMessage sm = (ShowMessage)ctx.getBean("myBean");
//调用ShowMessage的show方法输出消息
sm.show();
}
}
Spring 2.5 入门:HelloWorld 示例解析
本文详细介绍了使用Spring 2.5实现HelloWorld的基本步骤,包括配置文件applicationContext.xml的使用,以及ShowMessage类的定义与实例化。通过示例代码演示了如何创建并注入bean,最终输出“HelloWorld!”。
252

被折叠的 条评论
为什么被折叠?



