1.开发的步骤(总概述)
准备jar包
开发HelloWorld程序
在applicationContext.xml中完成配置(xsd-config.html)
启动容器,从容器中得到bean,调用bean中的方法
2.步骤
(1).要导入的jar包
spring-core-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
(2).普通的POJO类
package cn.wwh.www.spring.aaa.helloworld;
/**
*类的作用:
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-9-21 下午03:22:26
*/
public class HelloWorld {
public void show() {
System.out.println("Hello World! This is my first Spring!");
}
}
(3)配置helloworld.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.xsd">
<!--
id:bean的唯一标识
class:类的全限定名:不能是抽象类,不能是接口.
Object obj=Class.forName("cn.wwh.www.spring.aaa.helloworld.HelloWorld").newInstance();
-->
<bean id="helloworld" class="cn.wwh.www.spring.aaa.helloworld.HelloWorld">
</bean>
</beans>
在applicationContex.xml文件中引用
helloworld.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.xsd">
<!--
引入其他配置文件:
默认从当前路径去找文件.
可以带上路径前缀:
classpath:表示从classpath路径去找文件(推荐)
file:表示从磁盘中去找文件
<import resource="file:D:\Users\Administrator\Workspaces\MyEclipse 8.5
\SpringExercise1\src\cn\wwh\www\spring\aaa\helloworld\helloworld.xml"/>
注意:只有Resource接口才能识别和解析这种前缀:/SpringExercise1/src/cn/wwh/www/spring/aaa/helloworld/helloworld.xml
-->
<import resource="classpath:cn/wwh/www/spring/aaa/helloworld/helloworld.xml"/>
</beans>
(4).测试案例
package cn.wwh.www.spring.aaa.helloworld;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
*类的作用:
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-9-21 下午03:25:12
*/
public class TestHelloWorld {
// 1.采用传统的方式进行方法调用
@Test
public void testOld() {
// 创建一个对象
HelloWorld hello = new HelloWorld();
hello.show();
}
//采用Spring框架的控制反转的方式调用
@Test
public void testIoC(){
//1.加载applicationContext.xml文件:从classpath路径路径找
Resource resource = new ClassPathResource("applicationContext.xml");
//2.创建并获取Spring容器
BeanFactory bean = new XmlBeanFactory(resource);
//3.获取指定的bean,(特别要注意这个,getBean中的参数是,hellowrld.xml文件中bean标签中的id)
HelloWorld hello = (HelloWorld) bean.getBean("helloworld");
//4.操作bean
hello.show();
}
}
效果测试图:
3.注意:1.本人resources文件夹放在工程目录下,是和src同一个的目录,applicationcontext.xml文件就放在该文件夹中
2.引入其他配置文件:
默认从当前路径去找文件.
可以带上路径前缀:
classpath:表示从classpath路径去找文件(推荐)
file:表示从磁盘中去找文件
<import resource="file:E:\Users\Administrator\Workspaces\MyEclipse 8.5
\SpringExercise1\src\cn\wwh\www\spring\aaa\helloworld\helloworld.xml"/>
特别要注意:只有Resource接口才能识别和解析这种前缀:/SpringExercise1/src/cn/wwh/www/spring/aaa/helloworld/helloworld.xml
3.在helloworld.xml文件中<bean>标签下:
bean元素的id和name:
最好用id:特殊情况才使用name.
Spring开发中 Bean的 id属性 ,遵守xml语法ID约束:
id 的命名要满足XML对ID属性命名规范 必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号
使用name属性,就可以使用很多特殊字符,早期在struts1和spring整合 (现在的SpringMVC)
<bean name="/login" class="....LoginAction" /> name中含有/ ,使用id会报错
4.HelloWorld程序从Spring的角度看: 为什么配置一下,Spring就会去创建/管理Bean对象.
(1).通过Resource对象加载配置文件;
(2).解析配置文件,得到bean;
(3).解析bean,id作为bean的名字,class用于反射得到bean的实例(Class.forName(className));
注意,这种配置下,所有的bean保证有一个无参数构造器
(4).调用getBean的时候,从容器中返回对象实例
结论:就是把代码从JAVA文件中转移到了XML中.
5.getBean方法的三种方法:
(1).按照类型获取bean:
world=factory.getBean(HelloWorld.class);
要求在spring中只配置一个这种类型的实例;
若该类型的类配置了多个<bean/>,就要报错:--一个类型可以找到多个对象
(2).按照bean的名字获取bean:
world=(HelloWorld)factory.getBean("hello2");
按照名字拿bean不太安全;
异常:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloWorld' is defined:
在容器中找不到一个<bean/>元素的id或name叫helloWorld.
(3).按照名字和类型:(推荐)
world=factory.getBean("hello", HelloWorld.class);
异常:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [cn.itcast.cd.day1._1hello.HelloWorld] is defined:
不唯一的Bean类型错误: