创建项目
项目名称:spring0926012.引入相关jar包--日志记录
commons-logging.jar--单元测试
junit-4.4.jar--日志
log4j.jar --SpringIoC(依赖注入)的基础实现 spring-beans-3.2.0.RELEASE.jar --Spring提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持 spring-context-3.2.0.RELEASE.jar --Spring3.0的核心工具包 spring-core-3.2.0.RELEASE.jar --Spring表达式语言 spring-expression-3.2.0.RELEASE.jar3.添加配置文件 在项目中创建conf源文夹,并在conf目录下创建applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> </beans>4.创建业务bean 在src目录下创建HelloSpring业务Bean 包名:cn.jbit.spring092601.domain package cn.jbit.spring092601.domain; import java.io.Serializable; /** * spring入门 * @author Administrator * */ public class HelloSpring implements Serializable { private String name;//名称 public void setName(String name) { this.name = name; } public String getName() { return name; } }5.在配置文件中配置HelloSpring业务bean,在applicationContext.xml文件中的beans节点下配置, 配置完成后的applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- spring入门 控制反转IOC --> <bean id="helloSpring" class="cn.jbit.spring092601.domain.HelloSpring"> <!-- 给属性注入一个值:hello --> <property name="name"> <value>hello</value> </property> </bean> </beans> 6.测试spring是否工作 在项目中创建test源文件夹,在test目录下创建测试类 包名:cn.jbit.spring092601.domain package cn.jbit.spring092601.domain; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloSpringTest { /** * 依赖注入方式实现 */ @Test public void testHellpSpring3(){ /* * 对象由spring创建 */ ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring"); System.out.println(helloSpring.getName()); } /** * 控制反转方式实现 * IOC方式 */ @Test public void testHellpSpring2(){ /* * 对象由spring创建 */ ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring"); helloSpring.setName("张三"); System.out.println(helloSpring.getName()); } /** * 传统方式实现 */ @Test public void testHellpSpring1(){ HelloSpring helloSpring = new HelloSpring(); helloSpring.setName("zhangsan"); System.out.println(helloSpring.getName()); } }