eclipse 搭建 spring 环境 |
您所在的位置:网站首页 › eclipse配置spring4 › eclipse 搭建 spring 环境 |
一、下载 Spring 下载地址:http://repo.spring.io/libs-release-local/org/springframework/spring 下载zip压缩包: 并解压。
二、在 Eclipse 呀 myEclipse 中开发 Spring 应用 1. 新建 java project 项目命名为 myspring 2. 为该项目增加 Spring 支持。添加用户库 spring4.0.6 和 common-logging 添加方法如下图: commons-logging-1.1.3.jar 3. 定义一个 Spring 管理容器中的 Bean (POJO) src\hsl\service\PersonService.java 代码如下: [java] view plain copy package hsl.service; public class PersonService { private String name; // name属性的setter方法 public void setName(String name) { this.name = name; } // 测试Person类的info方法 public void info() { System.out.println("此人名为:" + name); } } 注:spring 可以管理任意的 POJO,并不要求 Java 类是一个标准的 JavaBean. 4. 编写主程序,该程序初始化 Spring 容器 src\hsl\SpringTest.java 代码如下: [java] view plain copy package hsl; import hsl.service.PersonService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { public static void main(String[] args) { // 创建Spring的ApplicationContext ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); System.out.println(ctx); // 输出Spring容器 // 通过 Spring 容器获取 Person 实例,并为 Person 实例设置属性值(这种方式称为控制反转,IoC) PersonService p = ctx.getBean("personService", PersonService.class); p.info(); } } 5. 将 PersionService 类部署在 Spring 配置文件中, src\bean.xml 代码如下: [html] view plain copy 6. 运行主程序,结果如下:
|
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |