[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();
}
}