您的位置:首页 > 编程语言 > Java开发

一个Spring简单例子

2010-03-08 08:14 405 查看
HelloBean.java代码如下

package onlyfun.caterpillar; public class HelloBean {     private String helloWord;         public void setHelloWord(String helloWord) {         this.helloWord = helloWord;     }     public String getHelloWord() {         return helloWord;     } }


beans-config.xml 配制如下

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"   "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>     <bean id="helloBean"           class="onlyfun.caterpillar.HelloBean">         <property name="helloWord">            <value>Hello!Justin!</value>        </property>     </bean> </beans>


SpringDemo.java代码如下

package onlyfun.caterpillar; import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.Resource;import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; public class SpringDemo {     public static void main(String[] args) {         Resource rs =             new FileSystemResource("beans-config.xml");         BeanFactory factory =             new XmlBeanFactory(rs);                 HelloBean hello =             (HelloBean) factory.getBean("helloBean");         System.out.println(hello.getHelloWord());     } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: