首先,可以通过springframework官方说明文档找到如下网址下载spring包
http://repo.spring.io/release/org/springframework/spring/
这里以4.2为例子
spring-framework-4.2.1.RELEASE-dist.zip
如果你只需要基本的beans ,ioc ,aop ,context 这四个spring的核心包
spring的其他包为企业应用扩展包
另外说明:spring需要依赖apache的common-logging包
Eclipse工程如何建立
导入这5个包,然后在右键项目buildpath,添加这些包到编译路径
配置spring配置文件helloMessage.xml
xml说明:
xmlns代表默认命名空间,xmlns:xsi代表改xml配置文件遵循xml规范
xsi:schemaLocation:代表具体用到的schema资源,可以提供自动提示的功能
xmlns : *** 代表如果 用***:$$$ 以使用***开头的命名空间$$$属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
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-4.2.xsd
">
<
bean
id
=
"helloChina"
class
=
"com.wzl.imp.HelloChina"
></
bean
>
<
bean
id
=
"helloWorld"
class
=
"com.wzl.imp.HelloWorld"
></
bean
>
<
bean
id
=
"person"
class
=
"com.wzl.model.Person"
>
<
property
name
=
"iHelloMessage"
ref
=
"helloChina"
></
property
>
</
bean
>
</
beans
>
|
在Main方法中读取配置文件生成Bean工厂类,便可以使用IOC服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
com.wzl.main;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.xml.XmlBeanFactory;
import
org.springframework.core.io.FileSystemResource;
import
org.springframework.core.io.Resource;
import
com.wzl.model.Person;
public
class
Main {
public
static
void
main(String[] args) {
Resource resource =
new
FileSystemResource(
"helloMessage.xml"
);
BeanFactory beanFactory =
new
XmlBeanFactory(resource);
Person person = (Person)beanFactory.getBean(
"person"
);
person.sayHello();
}
}
|