给点信心,来个简单的 HelloWorld
使用到的类: Action, UpperAction, BeanFactoryTest
使用到的bean配置文件: bean.xml
使用到的Spring包: org.springframework.beans-3.0.5.jar, org.springframework.core-3.0.5.jar
另外的包: commons-logging-1.1.1.jar
----------------------------------------Action.java----------------------------------------
面向接口
public interface Action {
public String execute(String str);
}
-------------------------------------UpperAction.java-------------------------------------
public class UpperAction implements Action {
private String message;
@Override
public String execute(String str) {
return (getMessage() + str).toUpperCase();
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
----------------------------------BeanFactoryTtest.java----------------------------------
BeanFactory 就是 Spring 容器
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class BeanFactoryTest {
public static void main(String[] args) {
Resource resource = new FileSystemResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Action action = (Action)factory.getBean("theAction");
System.out.println(action.execute("Biao"));
}
}
----------------------------------------bean.xml----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<description>Spring Quick Start</description>
<bean id="theAction" class="UpperAction">
<property name="message">
<value>HeLLo World:</value>
</property>
</bean>
</beans>