新手如何配置spring

首先,可以通过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包

下载common-logging.jar


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();
 
     }
 
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值