Spring框架学习——HelloWorld
来源:互联网 发布:centos vim插件 编辑:程序博客网 时间:2024/06/07 02:51
首先下载所需的jar包
新建一个工程,创建一个HelloService接口
public interface HelloService {public void hello();}
新建一个类HelloServiceImpl实现接口
public class HelloServiceImpl implements HelloService{ public void hello(){ System.out.println("Hello World!"); }}
新建一个helloworld.xml文件(在src目录下)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- id 表示组件的名字,class表示组件类 --> <bean id="helloService" class="HelloServicImpl" /> </beans>
现在要实例化一个IOC容器,然后从容器中获取所需的对象,调用接口完成我们的功能。写一个测试类
public class HelloTest {@Test public void testHelloWorld() { // 1、读取配置文件实例化一个IOC容器 ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml"); // 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现” HelloService helloService = context.getBean("helloService", HelloService.class); // 3、执行业务逻辑 helloService.hello(); }getBean里的参数helloService是id,HelloService.class是接口
右键运行JUnit Test,Console成功输出helloworld。
阅读全文
0 0
- Spring框架学习——HelloWorld
- Spring框架学习(一)——HelloWorld
- Spring框架学习(1)——HelloWorld
- 1. 笔记JAVA框架学习——Spring入门环境搭建及helloworld
- Spring框架学习笔记--HelloWorld(二)
- Spring —— HelloWorld
- spring框架创建helloworld
- Spring框架HelloWorld程序
- SpringMVC框架学习(1)——HelloWorld
- Spring框架学习(1):环境配置以及HelloWorld
- Spring学习一:基本框架图介绍与HelloWorld程序
- Spring Data 学习(1)—— HelloWorld
- Spring学习之路——简单入门HelloWorld
- 学习框架——Spring
- Spring+Maven——HelloWorld
- Spring入门——Helloworld
- Spring学习-helloworld
- spring学习笔记-helloworld
- redisson-2.10.4源代码分析
- Andrew Ng的machine learning课程week2编程题python实现
- 工作中linux常用命令总结
- struts、hibernate、spring、jre version
- 小波变换教程(二十四 完)
- Spring框架学习——HelloWorld
- Apache
- 请使用一种算法,对字符串进行升序排列
- scheduleAtFixedRate与scheduleWithFixedDelay的区别
- python学习_4
- OpenFst的创建和读取
- 51NOD 1217 Minimum Modular(数论)
- According to Bartjens UVA
- 网页直接调用android客户端的某个功能