Spring之Hello
2016-04-21 23:11
561 查看
下载spring包
Download一, 添加所需jar包
commons-logging-1.3.jarspring-beans-4.2.5.RELEASE.jar
spring-context-4.2.5.RELEASE.jar
spring-context-support-4.2.5.RELEASE.jar
spring-core-4.2.5.RELEASE.jar
spring-expression-4.2.5.RELEASE.jar
spring-test-4.2.5.RELEASE.jar
二, 编写接口
package com.lee.service; public interface Hello { public void say(String name); //方法说明 }
三, 编写接口实现类
package com.lee.service.impl; import org.springframework.stereotype.Service; import com.lee.service.Hello; @Service //注册bean public class HelloImpl implements Hello { @Override public void say(String name) { System.out.println("Hello " + name); } }
四, 编写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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 使用了注解, 需要告诉spring上下文去哪里找, 并注册到spring上下文中 --> <context:component-scan base-package="com.lee" /> <!-- 若不使用注解, 则需要添加以下代码注册bean到spring上下文中 <bean id="hello" class="com.lee.service.impl.HelloImpl" /> --> </beans>
五, 使用JUnit4测试框架测试spring
package com.lee.test.service; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.lee.service.Hello; @RunWith(SpringJUnit4ClassRunner.class) //整合spring和JUnit4 @ContextConfiguration(locations="classpath:applicationContext.xml")//声明xml所在 public class HelloTest { @Autowired //自动装载 private Hello hello; @Test public void hello() { hello.say("Lee"); } }
文件格式
控制台输出
相关文章推荐
- JavaSript模块规范 - AMD规范与CMD规范介绍
- java堆排序非递归代码,无原理版,比较好理解
- java 中的观察者模式实现之接口回调
- java安全-线程上下文类加载器
- springmvc中拦截器的使用
- Java IO:阻塞/非阻塞式IO、同步/异步IO
- spring框架学习笔记(四)
- Java日志框架
- Java的字符表示格式
- Java之ClassLoader
- Java反射机制
- spring面试题总结
- Java内部类实例
- 从头认识java-特辑-你不知道的main函数
- 欢迎使用CSDN-markdown编辑器
- java从0开始学习第一课-让你的Java程序跑起来!!!
- javax.mail.AuthenticationFailedException异常
- spring 手动事务回滚
- ubuntu下配置JDK7环境变量
- Java中的线程基本信息