Spring学习心得(1)-- 简单开发环境搭建

去官网下载开发包。

一,我的项目结构

二,搭建准备

导入jar包:spring.jar,commons-logging.jar,log4j-1.2.15.jar,junit-4.4.jar

日志文件放入src目录下:log4j.properties

spring配置文件放入src目录下:applicationContext.xml

三,创建HelloWorld类

将要纳入spring管理的类对象

  1. package com.lanhuigu.spring.action;  
  2.   
  3. public class HelloWorld {  
  4.     private String msg;  
  5.     //通过set方法注入属性值  
  6.     public void setMsg(String msg) {  
  7.         this.msg = msg;  
  8.     }  
  9.       
  10.     public String getMsg() {  
  11.         return msg;  
  12.     }  
  13.   
  14. }  
四,创建spring配置文件applicationContext.xml

将HelloWorld对象纳入spring管理,创建一个惟一id为HelloWorld的bean对象

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!--  
  4.   - Application context definition for JPetStore's business layer.  
  5.   - Contains bean references to the transaction manager and to the DAOs in  
  6.   - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").  
  7.   -->  
  8. <beans xmlns="http://www.springframework.org/schema/beans"  
  9.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  10.         xmlns:aop="http://www.springframework.org/schema/aop"  
  11.         xmlns:tx="http://www.springframework.org/schema/tx"  
  12.         xsi:schemaLocation="  
  13.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  14.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  15.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  16.     <!-- 定义一个id为HelloWorld的bean -->  
  17.     <bean id="HelloWorld" class="com.lanhuigu.spring.action.HelloWorld">  
  18.         <!-- 将变量msg值依赖注入 -->  
  19.         <property name="msg">  
  20.             <value>HelloWorld</value>  
  21.         </property>  
  22.     </bean>  
  23. </beans>  
五,测试程序

读取spring配置文件,根据bean对象id为HelloWorld获取HelloWorld对象,调用对象的属性方法

  1. package com.lanhuigu.spring.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import com.lanhuigu.spring.impl.ISayHello;  
  8.   
  9. public class TestHelloWorld {  
  10.     @Test  
  11.     public void testMyHelloWorld(){  
  12.         //1.读取spring初始化的配置文件  
  13.         ApplicationContext acxt =   
  14.                 new ClassPathXmlApplicationContext("/applicationContext.xml");  
  15.         //2.根据bean获取ISayHello实现类对象  
  16.         ISayHello sayHello = (ISayHello) acxt.getBean("sayHello");  
  17.         //3.调用接口方法  
  18.         System.out.println(sayHello.doSay());  
  19.     }  
  20. }  

六,输出结果

HelloWorld

七,总结

spring使用步骤:

(1)创建项目导入必须jar包

(2)加入日志文件log4j.properties,spring配置文件applicationContext.xml

(3)创建业务接口或类,纳入spring管理

(4)读取spring配置文件,根据bean对象id拿到我们的接口或类对象,使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值