一.建立項目
項目名稱: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目錄下建立包
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();
}
本文轉自 素顔豬 51CTO部落格,原文連結:http://blog.51cto.com/suyanzhu/1559504