逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

给点信心,来个简单的 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>

 

 

 

posted on 2010-11-04 18:59 逛奔的蜗牛 阅读(206) 评论(0)  编辑 收藏 引用 所属分类: Java

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理