目标:使用Spring的控制反转功能IoC(Inversion of Control).
备注:spring的ioc功能就是将对象的创建交给spring容器负责,我们不创建。
我们只是通过xml描述创建对象的方式,然后spring帮我们创建。
准备工作:
1.下载spring
下载后的压缩包为spring-3.2.0.M2-dist.zip,为spring3.2版本.
2.下载commons-logging.jar文件,spring依赖这个jar,否则运行异常。
地址:http://commons.apache.org/logging/download_logging.cgi
我下载到的是commons-logging-1.1.1-bin.zip
工程搭建:
一、新建一个 Java Project
二、导入jar包
经过测试spring运行ioc最少导入以下4个包:
spring-core-3.2.0.M2.jar
spring-context-3.2.0.M2.jar
spring-beans-3.2.0.M2.jar
spring-expression-3.2.0.M2.jar
然后导入
commons-logging-1.1.1.jar
好了,环境搭好了,然后就是新建类并在xml中描述该类的实例化方式。
这里使用beans.xml描述.
相关描述说明看文档spring-3.2.0.M2/docs/reference/html/beans.html
工程目录
代码:
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-3.0.xsd"> <!-- more bean definitions go here --> <!-- 没有指定实例化方式,则通过默认构造器实例化。此时Student类必须有默认构造器,否则异常 --> <bean id="student" class="com.spring.beans.Student"/> <!-- 默认构造器实例化后属性注入,通过setter方法注入值 --> <bean id="student2" class="com.spring.beans.Student"> <property name="name" value="zhangsan"/> <property name="age" value="18"/> <property name="num" ref="numCard"/> </bean> <!-- 指定构造器实例化并注入值 --> <bean id="numCard" class="com.spring.beans.NumCard"> <constructor-arg index="0" name="num" type="int" value="123457"/> <!-- 相当于 <property name="num" value="123457"/> --> </bean> </beans>
HelloSpring.java
package com.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.beans.Student; public class HelloSpring { public static void main(String[] args) throws Exception{ //通过xml配置文件实例化容器 ApplicationContext appCtx = new ClassPathXmlApplicationContext("beans.xml"); //获取bean Student student = (Student)appCtx.getBean("student"); System.out.println("name="+student.getName()+";age="+student.getAge()+";numCard="+(student.getNum()==null?"null":student.getNum().getNum())); Student student2 = (Student)appCtx.getBean("student2"); System.out.println("name="+student2.getName()+";age="+student2.getAge()+";numCard="+(student2.getNum()==null?"null":student2.getNum().getNum())); } }
Student.java
package com.spring.beans; public class Student { private String name; private int age; private NumCard num; private Student(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public NumCard getNum() { return num; } public void setNum(NumCard num) { this.num = num; } }
NumCard.java
package com.spring.beans; public class NumCard { private int num; public NumCard(int num){ this.num=num; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
运行结果:
name=null;age=0;numCard=null
name=zhangsan;age=18;numCard=123457
备注:
beans.xml文件编写不提示的时候,需要添加scheme.
window--preferences--xml-- 最终找到 XML Catalog ,点击add按钮,
location:选择解压后包里的spring-3.2.0.M2\schema\beans\spring-beans-3.0.xsd
key type: 选择URI
key: http://www.springframework.org/schema/beans/spring-beans-3.0.xsd (即xml中的xsi:schemaLocation的值)
保存,重新打开beans.xml后就有提示了