Eclipse IDE下的Spring框架使用简单实例

Eclipse IDE下的Spring框架使用简单实例


1 准备
Java jdk安装。

Eclipse软件安装。
根据系统安装32/64版本,选择Eclipse IDE for Java Developers 进行在线安装。

Spring框架下载。 

Commons_Logging包下载

2 配置工作
在项目栏右键,Build Path->Config Build Path 选择Librarys -> Add External Jars导入Spring 与Commons_Logging的jar包

3 示例程序
在src文件夹下创建com.manongjc包,在包中分别创建HelloWorld类与MainApp类,代码如下:

//HelloWorld类
package com.manongjc;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

//MainApp类
package com.manongjc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context =
             new ClassPathXmlApplicationContext("Beans.xml");

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

      obj.getMessage();
   }
}

在src文件夹下创建beans.xml文件,代码如下:

<?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">

<bean id="helloWorld" class="com.manongjc.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>

</beans>

选中MainApp,点击运行,可以在Console里看到输出的日志。

原文地址:https://www.cnblogs.com/mingzhang/p/7615593.html