Spring IOC相关知识介绍
来源:互联网 发布:数组结束标志 编辑:程序博客网 时间:2024/05/17 06:20
01-spring的ioc例子
1.1spring基本特征
Spring是一个非常活跃的开源框架;它是一个基于Core来构架多层JavaEE系统的框架,它的主要目地是简化企业开发.
Spring以一种非侵入式的方式来管理你的代码,Spring提倡”最少侵入”,这也就意味着你可以适当的时候安装或卸载Spring
1.2开发spring所需的工具
下载地址:http://repo.spring.io/simple/libs-release-local/org/springframework/spring/
Spring的jar包
然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
--spring的核心类库 在spring文档的dist下 dist\spring.jar
--引入的第三方类库 都spring文档的lib下lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar
注:JSR(Java 规范请求)是指向JCP(JavaCommunity Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR(Java 规范请求),以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准
1.3SpringIOC
Spring的控制反转:把对象的创建、初始化、销毁等工作交给spring容器来做。由spring容器控制对象的生命周期。
1.4 spring的ioc操作步骤(HelloWorld)
第一步:创建一个java项目day35-xp-spring-01-ioc-helloworld
第二步:新建一个lib文件夹用来存放spring的核心jar包
第三步:新建一个类HelloWorld.java
/** * @Title: HelloWorld.java * @Package com.xp.spring.ioc.helloworld */package com.xp.spring.ioc.helloworld; /** * ClassName: HelloWorld.java * 创建人: xiongpan * 时间 2016年9月24日.下午7:44:25 *@version 1.0.0 */public class HelloWorld { public void hello() { System.out.println("hello world"); }}
第四步:在类路径下新建配置文件applicaContext.xml来实例化容器
<span style="font-size:18px;"><?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-2.5.xsd"><!-- beans 存放了很多个类 把一个类放入到spring容器中,该类就是bean一个bean 就是描述一个类 id就是标识符 命名规范:类的第一个字母小写,其他字母保持不变class:为类的全名 --><bean id="helloWorld" class="com.xp.spring.ioc.helloworld.HelloWorld" ></bean></beans></span>
第五步:新建一个HelloWorldTest测试类
<span style="font-size:18px;">/** * @Title: HelloWorldTest.java * @Package com.xp.spring.ioc.helloworld.test */package com.xp.spring.ioc.helloworld.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.xp.spring.ioc.helloworld.HelloWorld;/** * ClassName: HelloWorldTest.java * 创建人: xiongpan * 时间 2016年9月24日.下午8:03:12 *@version 1.0.0 */public class HelloWorldTest {@Testpublic void testHelloWorld(){//启动sping容器ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//从spring容器中把对象提取出来HelloWorld helloWorld=(HelloWorld)context.getBean("helloWorld");helloWorld.hello();}}</span>
ClassPathXmlApplicationContext可以在整个类路径中寻找xml文件。其中需要注意的是:
通过这种方式加载。需要将spring的配置文件放到当前项目的classpath路径下
classpath路径指的是当前项目的src目录,该目录是java源文件的存放位置。
02-spring别名
在对bean进行定义时,除了使用id属性来指定名称 之外,为了提供多个名称,需要通过name属性来加以指定。而所有的这些名称都指向同一个bean,在某些情况下提供别名非常有用,比如 为了让应用的每一个组件能更容易的对公共组件进行引用。
然而,在定义bean时就指定所有的别名并不是总是恰当的。有时我们期望 能在当前位置为那些在别处定义的bean引入别名。在XML配置文件中,可用 <alias/> 元素来完成bean别名的定义。如
<span style="font-size:18px;"><beanid="helloWorld" class="com.xp.spring.ioc.alias.HelloWorld"></bean><aliasname="helloWorld" alias="熊盼"/></span>
注意name必须与bean的名称保持一致
03-spring容器创建对象的三种方式
3.1用构造器来实例化
当采用构造器来创建bean实例时,Spring对class并没有特殊的要求,我们通常使用的class都适用。
首先在HelloWorld添加默认的构造方法
<span style="font-size:18px;">public class HelloWorld{ public HelloWorld(){ System.out.println("new instance"); } public void hello() { System.out.println("hello world"); }}</span>
第二步在applicationContext.xml中实例化一个bean
<span style="font-size:18px;"><beanid="helloWorld"class="com.xp.spring.ioc.createobject.method.HelloWorld"></bean></span>
第三步:创建一个CreateObjectMethodTest.java测试默认构造方法
<span style="font-size:18px;">/** *spring容器在默认的情况下使用默认的构造函数创建对象 */ @Test public void testCreateObject_Default(){ ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld)context.getBean("helloWorld"); helloWorld.hello(); }打印结果;new instance hello world</span>
3.2使用静态工厂实例化
<span style="font-size:18px;">第一步创建一个工厂类HelloWorldFactory,packagecom.xp.spring.ioc.createobject.method; publicclass HelloWorldFactory { public static HelloWorld getInstance() { return new HelloWorld(); }}第二步:将类放到spring容器中<beanid="helloWorld2" class="com.xp.spring.ioc.createobject.method.HelloWorldFactory" factory-method="getInstance"></bean>第三步:在测试类中添加如下代码启动spring容器/** * 在spring容器 内部,调用了HelloWorldFactory中的getInstance方法 * 而该方法的内容就是创建对象的过程,是由程序员来完成 */ @Test public voidtestCreateObject_StaticFactory(){ ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld)context.getBean("helloWorld2"); helloWorld.hello(); }</span>
当采用静态工厂方法创建bean时,除了需要指定class 属性外,还需要通过factory-method属性来指定创建bean实例 的工厂方法。Spring将调用此方法(其可选参数接下来介绍)返回实例对象
3.3使用实例工厂方法实例化
<span style="font-size:18px;">第一步:创建一个类packagecom.xp.spring.ioc.createobject.method; publicclass HelloWorldFactory2 { public HelloWorld getInstance(){ return new HelloWorld(); }}第二步:将类放入到spring容器中<!-- factory-bean指向了实力工厂的bean factory-method实例工厂对象的方法 --><bean id="helloWorld3" factory-bean="helloWorldFactory"factory-method="getInstance"></bean></span>
第三步:启动spring容器
<span style="font-size:18px;"> /** * 实例工厂 * 1、spring容器创建一个实例工厂bean * 2、该bean调用了工厂方法getInstance产生对象 */ @Test publicvoid testCreateObject_InstanceFactory(){ ApplicationContextcontext = newClassPathXmlApplicationContext("applicationContext.xml"); HelloWorldhelloWorld = (HelloWorld)context.getBean("helloWorld3"); helloWorld.hello(); }</span>
使用静态工厂方法实例化类似,用来进行实例化的非静态实例工厂方法位 于另外一个bean中,容器将调用该bean的工厂方法来创建一个新的bean实例。为使 用此机制,class属性必须为空,而factory-bean 属性必须指定为当前(或其祖先)容器中包含工厂方法的bean的名称,而该工厂bean的工厂方法本身必须通过factory-method属性来设定。
04-spring容器-lazy与scope结合
<span style="font-size:18px;">第一步:创建一个类HelloWorldpackage com.xp.spring.ioc.scope; public class HelloWorld{ public HelloWorld(){ System.out.println("new instance"); } public void hello(){ System.out.println("hello world"); }}第二步:将类添加容器中<beanid="helloWorld" class="com.xp.spring.ioc.scope.HelloWorld"scope="prototype"></bean>第三步:scope测试类packagecom.xp.spring.ioc.scope.test; import org.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext; importcom.xp.spring.ioc.scope.HelloWorld; public class ScopeTest { /** * 在默认情况下,spring容器产生的对象是单例的 */ @Test public void testScope_Default(){ ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld)context.getBean("helloWorld"); System.out.println(helloWorld); HelloWorld helloWorld2 =(HelloWorld)context.getBean("helloWorld"); System.out.println(helloWorld2); } /** * 如果scope为"prototype",则为多实例 */ @Test public void testScope_Prototype(){ ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld)context.getBean("helloWorld"); HelloWorld helloWorld2 =(HelloWorld)context.getBean("helloWorld"); }}运行结果:newinstancenew instance</span>
4.lazy与scope结合
<span style="font-size:18px;">packagecom.xp.spring.ioc.lazyscope.test;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.xp.spring.ioc.lazyscope.HelloWorld;publicclass ScopeLazyTest { /** * 如果scope为"prototype" * 那么lazy-init属性将失去作用 */ @Test public voidtestScope_Prototype_Lazy_Default(){ ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld)context.getBean("helloWorld"); HelloWorld helloWorld2 =(HelloWorld)context.getBean("helloWorld"); System.out.println(helloWorld); System.out.println(helloWorld2); }}运行结果:newinstancenewinstancecom.xp.spring.ioc.lazyscope.HelloWorld@6765b2e4com.xp.spring.ioc.lazyscope.HelloWorld@45c3cbc4</span>
04-spring容器创建对象的时间
Spring默认在启动时将所有singletonbean提前进行实例化。提前实例化意味着作为初始化的一部分,ApplicationContext会自动创建并配置所有的singleton bean.通常情况下这是件好事。因为这样在配置中有任何错误能立即发现。
Lazy-init=”trueor false”
Lazy-init为false,spring容器将在启动的时候报错(比较好的一种方式)
Lazy-init为true,spring容器将在调用该类的时候出错。
<span style="font-size:18px;">packagecom.xp.spring.ioc.createobject.when.test;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext; importcom.xp.spring.ioc.createobject.when.HelloWorld; publicclass CreateObjectWhenTest { /** * 1、加载spring容器 * 2、spring容器调用默认的构造函数为bean创建对象 * 3、利用context.getBean把对象提取出来 * 说明:该形式更安全 ,如果spring的配置文件中有错误,在启动spring容器的时候就会报错 */ @Test public voidtestCreateObject_When_Default_Lazy_init_false(){ ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld)context.getBean("helloWorld"); helloWorld.hello(); } /** * 1、启动spring容器 * 2、执行context.getBean * 3、spring容器为该bean创建对象 * 在启动spring容器的时候是发现不了错误的 */ @Test public voidtestCreateObject_When_Lazy_init_TRUE(){ ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml"); HelloWorldhelloWorld = (HelloWorld)context.getBean("helloWorld"); helloWorld.hello(); }}</span>
05-spring容器-initdestroy
Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法。
<span style="font-size:18px;"><bean id=“foo” class=“...Foo” init-method=“setup” destory-method=“teardown”/>当foo被载入到Spring容器中时调用init-method方法。当foo从容器中删除时调用destory-method(scope = singleton有效)第一步:在HelloWorld添加init,destroy方法publicvoid init(){ System.out.println("init"); }publicvoid destroy(){ System.out.println("detroy");}第二步:将类放入到spring容器中<bean id="helloWorld" class="com.xp.spring.ioc.initdestroy.HelloWorld" init-method="init" destroy-method="destroy" scope="prototype"></bean>第三步:测试initdestroy方法packagecom.xp.spring.ioc.initdestroy.test; importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext; importcom.xp.spring.ioc.initdestroy.HelloWorld; publicclass InitDestroyTest { /** * 1、启动spring容器 * 2、创建helloWorld对象 * 3、执行init方法 该方法是由spring容器内部调用 * 4、context.getBean把对象提取出来 * 5、对象调用方法 * 6、当执行close方法的时候,执行该对象的destroy方法 是由spring容器内部调用 */ @Test public void testInitDestroy_Scope_Default(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld)context.getBean("helloWorld"); helloWorld.hello(); ClassPathXmlApplicationContextapplicationContext = (ClassPathXmlApplicationContext)context; applicationContext.close(); } /** * 1、启动spring容器 * 2、context.getBean把对象提取出来 * 3、创建helloWorld对象 * 4、执行init方法 该方法是由spring容器内部调用 * 5、对象调用方法 * 说明:不负责销毁 */ @Test public voidtestInitDestroy_Scope_Prototype(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld =(HelloWorld)context.getBean("helloWorld"); helloWorld.hello(); ClassPathXmlApplicationContextapplicationContext = (ClassPathXmlApplicationContext)context; applicationContext.close(); }}</span>
- Spring IOC相关知识介绍
- 关于Spring框架的Ioc&DI的相关知识
- Spring的IoC介绍
- Spring IOC介绍
- Spring IoC介绍
- Spring IOC原理介绍
- spring-IoC介绍
- Spring中ioc介绍
- Spring知识整理【三】Spring IOC/DI
- 缓冲区相关知识介绍
- json相关知识介绍
- 知识图谱相关介绍
- Activity相关知识介绍
- Spring学习(一)---IoC介绍
- Spring IOC BeanFactory简单介绍
- 3.spring IOC、DI 介绍
- Spring相关知识
- spring aop相关知识
- web开发相关
- 聊聊TCP/IP、HTTP和Socket
- PAT 1082. Read Number in Chinese (25)(把九位内的数字翻译成拼音输出)
- 深入理解加密、解密、数字签名和数字证书
- python 初学
- Spring IOC相关知识介绍
- java初始化的加载顺序-----我的理解
- 2、WebService 三要素
- 构建一棵二叉树的镜像树
- Android性能优化典范 - 第6季
- 微信小程序--text组件学习
- 提升代码的效率--复用
- 数字证书及其认证过程
- DIV+CSS布局