一.创建项目
项目名称:spring092902
二.添加jar包
1.在项目中创建lib目录
/lib
2.在lib目录下添加jar包
commons-logging.jar
junit-4.4.jar
log4j.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
三.添加配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下添加配置文件
<?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:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 解析注解 -->
<context:annotation-config />
<!-- 扫描注解 -->
<context:component-scan base-package="cn.jbit.spring092902.service"></context:component-scan>
</beans>
四.创建bean
1.在src目录下创建包
cn.jbit.spring092902.service
2.在包下创建bean
bean名称:HelloService.java
bean内容:
@Component("helloService")
public class HelloService {
public void sayHello(){
System.out.println("hello");
}
}
五.测试
1.在项目中创建test目录
/test
2.在test目录下创建包
cn.jbit.spring092902.service
3.在包下创建测试类
测试类名:HellpServiceTest.java
测试类内容:
public class HellpServiceTest {
@Test
public void testSayHello(){
ApplicationContext con = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
HelloService helloService = (HelloService) con.getBean("helloService");
helloService.sayHello();
}
}
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1559504
原文地址:http://suyanzhu.blog.51cto.com/8050189/1559504