Spring---搭建开发环境和第一个简单实例

来源:互联网 发布:淘宝产品推广 编辑:程序博客网 时间:2024/05/22 22:41

Spring是一个类的容器实例化托管框架,可以实现对实现类的实例化进行动态的托管。更可以实现控制反转。控制

转就是应用本身不负责倚赖对象的创建和维护,倚赖对象的创建和维护是通过其他的外部容器负责的,这样的控制

权就由应用转移到了容器。控制权的转移就是所谓的反转。


下面我们来说一下一个简单的Spring框架的搭建和实例.首先下载下来spring的压缩包,在解压后的dist文件夹下

有spring.jar和commons-logging.jar这两个JAR包,这就是实现最简单的spring框架所必须的包,然后就是

docs/reference下面的参考手册,里面又spring的配置文件写法。导入包完成后,最好在src目录下面建立beans.xml配

转就是应用本身不负责倚赖对象的创建和维护,倚赖对象的创建和维护是通过其他的外部容器负责的,这样的控制权

由应用转移到了容器。控制权的转移就是所谓的反转。

下面我们来说一下一个简单的Spring框架的搭建和实例.首先下载下来spring的压缩包,在解压后的dist文件夹下

有spring.jar和commons-logging.jar这两个JAR包,这就是实现最简单的spring框架所必须的包,然后就是

docs/reference下面的参考手册,里面又spring的配置文件写法。导入包完成后,最好在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-2.5.xsd"><bean id="personService" class="com.bird.service.impl.PersonServerImpl"></bean></beans>

然后写一个类和抽取出接口

package com.bird.service;public interface PersonServer {void save();}package com.bird.service.impl;import com.bird.service.PersonServer;public class PersonServerImpl implements PersonServer {@Overridepublic void save(){System.out.println("save()方法调用");}}
然后在beans.xml中配置这个类就可以使用spring实例化这个类了,下面的操作全部都是面向接口的编程了。

package junit.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bird.service.PersonServer;public class SpringTest {@Testpublic void test(){ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");PersonServer s = (PersonServer)ctx.getBean("personService");s.save();}                                                                                                                                                                                                                                                              }

运行结果如下

2012-2-20 16:47:15 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@723d7c: display name [org.springframework.context.support.ClassPathXmlApplicationContext@723d7c]; startup date [Mon Feb 20 16:47:15 CST 2012]; root of context hierarchy2012-2-20 16:47:16 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [beans.xml]2012-2-20 16:47:17 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@723d7c]: org.springframework.beans.factory.support.DefaultListableBeanFactory@a4e7432012-2-20 16:47:17 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a4e743: defining beans [personService]; root of factory hierarchysave()方法调用



原创粉丝点击