Spring IoC/DI容器 的helloworld工程
2017-02-14 11:52
369 查看
第一步:新建一个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); // 打印要有前缀 } }
Spring 对bean的使用 有两种方式
方式一:基于xml的配置方式
方式二:注解
一些想法:
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
相关文章推荐
- Spirng IoC/DI容器 的helloworld工程2-多个配置文件
- spring学习总结(二):IOC & DI 概述及 IOC 容器
- Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)
- spring学习(三)—深入理解DIP、IoC、DI以及IoC容器
- 依赖注入(DI)和控制反转(IOC)的详细讲解 spring容器(spring注解实现,而不是xml配置文件)
- spring——(1)ioc容器和Di依赖注入
- Spring核心概念理解-IoC容器、DI、AOP
- spring 第1天 IOC,DI,国际化,容器事件
- Spring核心容器的详细解析以及IOC和DI和过程全解
- Spring容器的控制反转(IOC)和依赖注入(DI)
- Helloworld之Spring依赖注入/控制反转(DI/IoC)版
- spring深入理解DIP、IOC、DI以及IOC容器
- Spring中的IoC(控制反转)容器初探
- Spring.net(二)----初探IOC容器
- 体验Spring的“IoC容器”对开发过程中的简化 推荐
- (转)关于spring的两个概念:IoC(控制反转) ,DI(依赖注入)
- Spring反转容器IOC
- 在Servlet(或者Filter,或者Listener)中使用spring的IOC容器
- Spring教程系列(一)-IoC与DI
- Spring Ioc 容器实例化容器