myeclipse10怎么搭建spring框架

如题所述

   在MyEclipse中快速配置spring框架的步骤如下:

    1、建工程;
       File -> New -> Project ->Web Project,"Project
Name":MySpringTest,然后"Finish";  

    2、导入spring包;
       选中MySpringTest,右击,MyEclipse
-> Add Spring Capabilities……,都默认即可;

    3、建立项目所需类;
        (1)、接口Action:(MySpringTest ->
src -> New -> interface ,取名为Action)

  public interface Action {
                public String execute(String str);
            }

   (2)、实现接口Action的类UpperAction:(将其 message 属性与输入字符串相连接,并返回其大写形式。)

         public class UpperAction implements Action{
        private String message;
           
        public String getMessage() {
             return message;
        }
           
        public void setMessage(String message) {
            this.message = message;
        }
        public String execute(String str){
            return (getMessage()+str).toUpperCase();
        }
    }

  (3)、 实现接口Action的类LowerAction:

           public class LowerAction implements Action {
        private String message;
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        public String execute(String str) {
            return (getMessage() + str).toLowerCase();
        }
    }

      (4)、做测试用的SimpleTest类:

 public class SimpleTest {
        public static void main(String args[]) {
            SimpleTest test = new SimpleTest();
            test.testQuickStart();
        }
        public void testQuickStart() {
            ApplicationContext ctx = new FileSystemXmlApplicationContext(
                    "src/bean.xml");
            Action action = (Action) ctx.getBean("action1");
            System.out.println(action.execute("Rod Johnson"));
            action = (Action) ctx.getBean("action2");
            System.out.println(action.execute("jeckj"));
        }
    }

    4、配置applicationContext.xml文件;

 <beans>
          <description>Spring Quick Start</description>
            
           <!--该处bean中的name值必须是 其对应的class中的私有成员名
           -->
           <bean id="action1" class="UpperAction">
           <property name="message"> 
          <value>HeLLo</value> 
          </property> 
          </bean>
             
          <bean id="action2" class="LowerAction">
          <property name="message"> 
          <value>HeLLo</value> 
          </property> 
          </bean> 
        </beans>

5、至此就完成了spring框架的搭建了。

温馨提示:答案为网友推荐,仅供参考
相似回答