搭建环境
1、创建普通的Java工程
2、添加相应的jar包,下载链接:https://files.cnblogs.com/files/AmyZheng/lib.rar,此外,为了打印信息,我们还需要一个Apache Commons Logging API,在这里下载commons-logging-1.2。
3、jar包
- Spring核心必须依赖的库:commons-logging-1.1.1.jar
- Spring IoC部分核心库:
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-context-support-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
spring-web-4.3.9.RELEASE.jar ------> 支持在Web环境中使用Spring IoC容器
- Spring AOP部分核心库:
spring-aop-4.3.9.RELEASE.jar
spring-aspects-4.3.9.RELEASE.jar
- Spring AOP需要依赖于aspectj库:
aspectjrt.jar
aspectjweaver.jar
第一个实例
1、新建一个配置文件,用于配置和管理所有的bean。
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" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="birthdate" class="java.util.Date" /> <util:map map-class="java.util.HashMap" id="map"> <entry key="罗玉凤" value="30" /> <entry key="罗玉龙" value="40" /> </util:map> </beans>
每一个bean的Id唯一
2、新建Java测试类
package ecut.ioc.ex; import java.util.Date; import java.util.Map; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //classpath和classpath下的所有jar包 System.out.println( System.getProperty( "java.class.path" ) ); //configLocations为不定参数,classpath当前工程下的bin目录下D:java_workspacejavaSpringin; String configLocations = "classpath:ecut/**/ex/beans.xml" ; //String configLocations = "classpath:ecut/ioc/ex/beans.xml" ; //String configLocations = "classpath:beans.xml" ;//若配置文件在src目录底下 // AbstractApplicationContext 实现了 org.springframework.context.ApplicationContext 接口 // ClassPathXmlApplicationContext 继承了 org.springframework.context.support.AbstractApplicationContext AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations); //name 与xml中的bean标签的ID相对应 Date date = context.getBean( "birthdate", Date.class ); System.out.println( date ); Map<?,?> map = context.getBean( "map" , Map.class ); System.out.println( map ); //ApplicationContext没有close方法,AbstractApplicationContext context.close(); } }
package ecut.ioc.ex; import java.util.Date; import java.util.Map; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //classpath和classpath下的所有jar包 System.out.println( System.getProperty( "java.class.path" ) ); //configLocations为不定参数,classpath当前工程下的bin目录下D:java_workspacejavaSpringin; String configLocations = "classpath:ecut/**/ex/beans.xml" ; //String configLocations = "classpath:ecut/ioc/ex/beans.xml" ; //String configLocations = "classpath:beans.xml" ;//若配置文件在src目录底下 // AbstractApplicationContext 实现了 org.springframework.context.ApplicationContext 接口 // ClassPathXmlApplicationContext 继承了 org.springframework.context.support.AbstractApplicationContext AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations); //name 与xml中的bean标签的ID相对应 Date date = context.getBean( "birthdate", Date.class ); System.out.println( date ); Map<?,?> map = context.getBean( "map" , Map.class ); System.out.println( map ); //ApplicationContext没有close方法,建议使用AbstractApplicationContext context.close(); } }
因为ApplicationContext没有close方法,建议使用AbstractApplicationContext来创建一个spring 容器, 这个容器读取classpath(当前工程下的bin目录)下的配置文件,并由容器去创建相应的对象,最后提供getBean方法获取name( 与xml中的bean标签的Id相对应)所指定的对象。
转载请于明显处标明出处