Spirng IoC/DI容器 的helloworld工程
来源:互联网 发布:js左右拖动滑块插件 编辑:程序博客网 时间:2024/06/05 11:57
第一步:新建一个java project
第二步:添加需要的jar包:
2.1在工程下新建文件夹lib,引入需要的jar包:
(1)Spring提供给我们用的jar包(2)Spring需要使用的jar包
2.2 把引入的jar包添加到eclipse中:右键工程-properties-Java Build Path -Libaries面板 Add JARs -选择对应工程下的lib文件夹
第三步:编程
3.1写应用程序,写法和java一样
//定义接口public interface Api { public String t(int a);}//定义接口的实现类public class ImplOne implements Api { @Override public String t(int a) { // TODO Auto-generated method stub System.out.println("now in Impl One t,a==" + a); return "hello==" + a; }}
3.2 为Spring写配置文件,name和class
在src文件夹下新建文件applicationContext.xml
<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" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"> <!--相当于Api api = new ImplOne();--> <bean name="test" class="cn.javass.spring3.hello.ImplOne"></bean></beans>
3.3写客户端程序
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Client { public static void main(String[] args) { // 第一步:读配置文件,来创建ApplicationContext,相当于bean的工厂 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); // 第二步:获取需要操作的bean Api api = (Api) context.getBean("test"); ////////// 第三步:以下和spring就没有关系了,面向接口编程//////////// String s = api.t(21); System.out.println("s==" + s); // 打印要有前缀 }}
一些想法:
1.什么情况下叫用了Spirng框架,什么情况下是没用Spring框架:
答:引用了Spring提供的jar包和Sprign依赖的要使用的jar包
2.怎么用Spring框架?
答:通过配置文件,上面这个例子是通过写Spring的配置文件 来使用Spring IoC容器
3.使用Spring框架的好处?
答:松散类之间的耦合,在上面的例子中,客户端Client类和Api接口没有关系
4. bean对象和IoC容器?
答:凡是受 Spring IoC容器管理的对象都叫bean对象。
Ioc容器管理bean对象的实例化,装配和生命周期。
所以bean对象是Spring框架特有的。
5.SpringK框架的非侵入性怎么体现?
答:除了测试代码,其他地方没有出现Spring的组件。所谓非侵入性,就是实现了功能,但是没有使用Spring的api
0 0
- Spirng IoC/DI容器 的helloworld工程
- Spirng IoC/DI容器 的helloworld工程2-多个配置文件
- IoC-DI容器理解
- 不可不知的DIP、IoC、DI以及IoC容器
- 关于DIP,IoC,DI,以及IoC容器的一个说明
- Guice:轻量级IOC/DI 容器
- Spirng Boot 的 helloWorld 项目
- 基于微软Unity的IoC容器(DI)实现
- Spring容器的控制反转(IOC)和依赖注入(DI)
- DIP、Ioc、DI、Ioc容器概念
- 浅谈DIP,IoC,DI,IOC容器
- Spring 的IoC DI
- IOC/DI的总结
- Java的几个概念AOP、IOC、DI、DIP、工厂模式、IOC容器
- 对DIP、IoC、DI、IoC容器的解释,静下心,认真看完就能理解
- IoC容器与依赖注入DI模式
- IoC容器与依赖注入DI模式
- 关于Spirng IOC的注解说明
- Android自定义预定日历,并且显示阴历 (修改版)
- 理解HTTP协议
- TLS and SSL
- Tomcat 启动一直超时
- 1065. 单身狗(25)
- Spirng IoC/DI容器 的helloworld工程
- 编程范式,程序员的编程世界观
- 289. Game of Life
- javascript的垃圾回收机制与内存管理
- React Native 实现热部署、差异化增量热更新
- java后台查询出来的日期用JS解析
- springMVC
- ASP.Net TextBox只读时不能取值的解决方案
- C#之显示转换