
可看到src下有applicationContext.xml文件生成,Eclipse可自己新建。
打开applicationContext.xml,找到<Beans></Beans>节点,在节点中添加一个<Bean></Bean>节点,具体如下:
- <?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.0.xsd">
- <bean id="hello" class="com.testspring.dao.impl.TestSpringImpl">
- <property name="hello" value="Hello Word"></property>
- </bean>
- </beans>
接下来在dao包中新建一个接口:ITestSpring.java
- package com.testspring.dao;
- public interface ITestSpring {
- public String say();
- public String getHello();
- public void setHello(String name);
- }
再在dao包中新建接口实现包impl ,包中新建类:TestSpringImpl.java
- package com.testspring.dao.impl;
- import com.testspring.dao.ITestSpring;
- public class TestSpringImpl implements ITestSpring {
- private String hello;
- public TestSpringImpl(){
- }
- public String say(){
- return "Spring return :" + this.getHello();
- }
- public String getHello() {
- return hello;
- }
- public void setHello(String hello) {
- this.hello = hello;
- }
- }
回过头来,在main包中新建类:HelloWord.java
- package com.testspring.main;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
- import com.testspring.dao.ITestSpring;
- public class HelloWord {
- public static void main(String[] args){
- XmlBeanFactory bean = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
- ITestSpring spring =(ITestSpring)bean.getBean("hello");
- System.out.println(spring.say());
- bean.destroySingletons();
- }
- }
自此,Spring的HelloWord小例子完成,把HelloWord作为javaApplication程序运行。
applicationContext.xml分析:
applicationContext.xml ,这个文件对于spring开发十分重要,他自Spring的BeanFactory继承而来,却扩展了更多功能,如与Struts、JSF等流行框架的整合;整体或声明式事务的管理;JMS的管理等等。
如作为web工程,一般在Web工程启动时,会在Web.xml中读取这个文件,将Beans节点中声明的bean通过class读取到内存中,而每次代码用 XXX.getBean("xxx") 通过反射得到一个Bean 时,都会通过BeanFactory获得这个Bean来进行处理。
web.xml :
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
这种方式称为Spring独有的组件装配方式: 依赖注入
依赖注入有三种方式 1:xml注入,大部分人都用 2:构造函数注入 3:接口注入(不推荐)
具体的依赖注入资料参见:http://blog.youkuaiyun.com/xiaoranchenxi/archive/2008/06/01/2466639.aspx
xml解释:
//xml定义了用hello作为类com.testspring.dao.impl.TestSpringImpl的引用id,业务类可用beanFactory.getBean('hello")来获取类的实例。
<bean id="hello" class="com.testspring.dao.impl.TestSpringImpl">
//表示在TestSpringImpl类中,有属性hello,xml定义了他的初始值为Hello Word,当然可以不预设,则为null。也可设为boolean、int、甚至List、Map、url 等各种资源,但如spring不能将value自动转换,会抛出转换异常。
<property name="hello" value="Hello Word"></property>
TestSpringImpl.java 分析:
public String say(){
return "Spring return :" + this.getHello();
}
say方法返回字符串,而this.getHello()取得本类的hello属性,此属性在applicationContext.xml中已定义为HelloWord,而xml又在HelloWord.java 的main方法上下文中初始化了,因此最后返回的结果为:Spring return :Hello Word 。
HelloWord.java 分析:
// 这是通过Spring提供的XmlBeanFactory类,查找写好的XML配置文件注入用beans中的类实例。
XmlBeanFactory bean = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//applicationContext.xml 中每个bean都有自己的id,XxmlBeanFactory通过id反射获取bean中声明好的class实例
ITestSpring spring =(ITestSpring)bean.getBean("hello");
//显示bean实例中的say方法结果,
System.out.println(spring.say());
//目的达到,注销掉spring的beanfactory(java垃圾机制处理)
bean.destroySingletons();
在 ITestSpring spring =(ITestSpring)bean.getBean("hello");以后,可以通过spring.setHello("xxx")更改ITestSpring实现类的hello属性值,从而改变applicationContext.xml 预设好的结果。
最终 , Helloword写好 打印出:
Spring return :Hello Word