Spring 开发第一步

   经过今天上午的学习发现spring上手开发一个"hello world"真的非常简单。

开发环境搭建:

1、去spring官网下载spring-framework-3.2.11.RELEASE-dist.zip 。这里带dist的是二进制的jar包,不带dist的是源代码

2、eclipse下新建一个java项目,把上面的包解压后的jar都引用到项目里去,还要再引入一个commons-logging.jar不然会报日志logging factory找不到的错误。

运行《spring in action 3rd Edition》chapter 2中的例子:

package com.springinaction.springidol;

public interface Performer {
    void perform() throws PerformanceException;
}
package com.springinaction.springidol;

public class Juggler implements Performer{
    private int bags = 3;
    public Juggler(){
        
    }
    public Juggler(int bags){
        this.bags = bags;
    }
    public void perform() throws PerformanceException{
        System.out.println("Juggling "+ this.bags + " bags.");
    }
}
<?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-3.0.xsd">
<!-- Beans declararions go here -->
<bean id="duke" class="com.springinaction.springidol.Juggler"></bean>
</beans>

ApplicationContext ctx = new ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
Performer performer = (Performer)ctx.getBean("duke");
performer.perform();

运行结果:Juggling 3 bags.

原文地址:https://www.cnblogs.com/lyhero11/p/4050142.html