一、环境准备
- 1、
myeclipse
开发工具安装springsource tool suite
可以在Help
–> Install from Catalog
搜索 springsource tool suite
, 只需要选中IDE
相关部门安装 - 2、下载
Spring
各种jar包
spring包下载地址
其中beans
、context
、core
、expression
四个包必须引入 - 3、下载
commons-logging
commons-logging包下载地址
二、项目结构目录如下:

三、代码实现步骤
- 1、新建bean相关的
HelloWorld.java
package com.fc.spring.bean;
public class HelloWorld {
private String name;
public HelloWorld() {
System.out.println("HelloWorld's Constructor...");
}
public void setNameVal(String name) {
System.out.println("evoke set function!");
this.name = name;
}
public void hello() {
System.out.println("hello, " + name);
}
}
- 2、新建
applicationContext.xml
配置文件
在src
同级目录下新建 applicationContext.xml
文件时选择spring
–> Spring Bean Configuration File
类型文件
<?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.xsd">
<bean id="hello" class="com.fc.spring.bean.HelloWorld">
<property name="nameVal" value="SpringDemo"></property>
</bean>
</beans>
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello = (HelloWorld) ctx.getBean("hello");
hello.hello();