spring2.5IOC控制反转详解
基本的代码结构
1 IOC包下
基本的spring创建对象
将类添加到配置文件中,由容器创建。
- <?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">
- <!--
- 把HelloWorld这个类纳入spring容器
- id为bean的唯一标识
- 正规的写法:
- 类的第一个字母变成小写,其余不变
- class为类的全名
- -->
- <bean id="helloWorld" class="cn.itcast.spring0401.ioc.HelloWorld"></bean>
- </beans>
helloworld类
- package cn.itcast.spring0401.ioc;
- public class HelloWorld {
- public void say(){
- System.out.println("hello");
- }
- }
测试类
这就是控制反转
- package cn.itcast.spring0401.ioc;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * 控制反转
- * @author Administrator
- *
- */
- public class IOCTest {
- /**
- * 启动spring容器
- * 创建spring容器对象就相当于启动spring容器
- * spring容器做的工作:
- * * 创建对象
- */
- @Test
- public void testHelloWorld(){
- ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/ioc/applicationContext.xml");
- HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
- helloWorld.say();
- }
- }
2 alias包,别名
配置文件,可以给类起别名
- <?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">
- <bean id="helloWorld" class="cn.itcast.spring0401.alias.HelloWorld"></bean>
- <!--
- name与bean中的id对应
- alias 名字
- -->
- <alias alias="狗蛋" name="helloWorld"/>
- <alias alias="王三麻子" name="helloWorld"/>
- </beans>
测试方法
- package cn.itcast.spring0401.alias;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class AliasTest {
- @Test
- public void test(){
- ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/alias/applicationContext.xml");
- HelloWorld helloWorld = (HelloWorld)context.getBean("王三麻子");
- helloWorld.say();
- }
- }
输出结果还是一样的。
3 createbean 创建对象的几种方法
* 利用默认的构造函数
* 利用静态工厂方法
* 利用实例工厂方法
配置文件
- <?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">
- <bean id="helloWorld" class="cn.itcast.spring0401.createbean.HelloWorld"></bean>
- <!--
- -->
- <bean id="helloWorld1" class="cn.itcast.spring0401.createbean.HelloWorldFactory" factory-method="createBean"></bean>
- </beans>
工厂类
- package cn.itcast.spring0401.createbean;
- public class HelloWorldFactory {
- /**
- * 工厂方法
- * @return
- */
- public static HelloWorld createBean(){
- return new HelloWorld();
- }
- }
helloworld
测试类
- package cn.itcast.spring0401.createbean;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class CreateBeanTest {
- /**
- * 在HelloWorld中写如下代码
- * public HelloWorld(){
- System.out.println("new helloworld");
- }
- 输出为:"new helloworld"
- 说明
- * spring容器默认调用类的默认的构造器来创建对象的
- * 如果在HelloWorld中,没有默认的构造器,则spring在创建helloWorld对象时,会报错
- 因为找不到默认的构造器
- */
- @Test
- public void testConstructor(){
- ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/createbean/applicationContext.xml");
- HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
- helloWorld.say();
- }
- /**
- * spring容器做的事情:
- * * spring容器调用了工厂类的工厂方法
- * * 真正创建对象new HelloWorld()是由程序员来完成的
- */
- @Test
- public void testFactory(){
- ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/createbean/applicationContext.xml");
- HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld1");
- helloWorld.say();
- }
- }
4init包,创建对象的时机
默认情况下,在spring启动的时候,创建纳入spring容器中所有的bean
在spring容器启动的时候,可以检查错误
但是如果bean的属性中有数据,会过早的加载到内存中,所以如果bean中有数据
应该把数据的对象的声明放在方法中
* 如果在spring的配置文件中,有lazy-init为true,则context.getBean(“beanId”)时
才要创建对象
缺点:在spring容器启动的时候,是检验不出错误的
配置文件
- <?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">
- <bean id="helloWorld" class="cn.itcast.spring0401.init.HelloWorl" lazy-init="true"></bean>
- </beans>
helloworld
测试
- package cn.itcast.spring0401.init;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class InitTest {
- /**
- * * 在默认情况下,spring容器启动的时候,就把所有的纳入spring容器的bean创建对象
- * 缺点:
- * 如果一个对象中有属性,比如这个属性为集合,在创建这个对象的过程中,集合中有数据
- * 这样采用默认的启动形式,就会导致数据过早的加载到内存中
- * * 可以在spring的配置文件中:
- * <bean id="helloWorld" class=".." lazy-init="true">
- * 延迟bean的创建时间,在context.getBean时才要创建bean的对象
- * 如果spring的配置文件书写错误,如果所有的spring的bean都采用lazy-init="true"这种形式
- 则在启动web服务器的时候,发现不了spring容器的错误,这样是不利于排错的
- */
- @Test
- public void test(){
- ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/init/applicationContext.xml");
- HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
- helloWorld.say();
- }
- }
5scope 对象作用范围,单例多里
在配置文件中,scope为
“singleton”
* 默认值
* spring产生的bean只有一个实例
* 处于单例模式的bean的创建、初始化、销毁都是受spring容器管理的
* 在容器关闭的时候执行销毁工作
“prototype”
* 多例
* spring容器负责该bean的创建、初始化,但是销毁工作程序员做
* 无论该bean的lazy-init为什么值,都在context.getBean时创建对象
* 如果该bean中有资源对象,手动关闭
配置
- <?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">
- <!--
- scope
- 决定bean的范围
- singleton 单例 默认
- prototype 原型 多例
- -->
- <bean id="helloWorld" class="cn.itcast.spring0401.scope.HelloWorld" scope="singleton"></bean>
- </beans>
测试
- package cn.itcast.spring0401.scope;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class ScopeTest {
- /**
- * 由spring容器产生的bean默认是单例模式
- * 在spring的配置文件中:
- * scope:
- * singleton 默认的形式
- * 如果写默认的形式,一个集合或者一个数据出现在了类的属性中,这个数据将成为全局的数据(共享数据),应该
- * 注意并发问题
- * 当spring容器中的bean是多例,则不管配置文件中的lazy-init为default、false还是true,在
- * context.getBean时才要为bean创建对象
- */
- @Test
- public void test(){
- ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/scope/applicationContext.xml");
- HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
- helloWorld.s.add("aaa");
- helloWorld.s.add("bb");
- HelloWorld helloWorld1 = (HelloWorld)context.getBean("helloWorld");
- helloWorld1.s.add("cc");
- System.out.println(helloWorld.s.size());
- }
- }
6initdestory
对象的初始化,销毁
配置文件
- <?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">
- <!--
- init-method
- 对象的初始化方法
- destroy-method
- 对象的销毁方法
- -->
- <bean id="helloWorld" class="cn.itcast.spring0401.initdestroy.HelloWorld" init-method="init" destroy-method="destroy" scope="prototype"></bean>
- </beans>
测试
- package cn.itcast.spring0401.initdestroy;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class InitDestroyTest {
- /**
- * 在spring的配置文件中
- * init-method="init"
- * 说明在创建完该对象后,立刻执行init方法,用来进行初始化
- * destroy-method="destroy"
- * * 当该bean为单例模式,才能调用该方法
- * destroy方法在容器销毁的时候被调用
- * * 当该bean为多例时,spring容器不负责他的销毁工作
- * * 如果该bean为多例时,当不用该bean时,应该手动销毁资源文件
- */
- @Test
- public void test(){
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/initdestroy/applicationContext.xml");
- HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
- helloWorld.say();
- context.destroy();//销毁spring容器
- }
- }
hello
spring2.5IOC控制反转详解的更多相关文章
- 分享知识-快乐自己:Spring_IOC(控制反转)详解
IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...
- Spring IOC(控制反转)详解及示例
控制反转——Spring通过一种称作控制反转(IOC)的技术促进了低耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象.你可以认为IoC与JN ...
- coding++:Spring_IOC(控制反转)详解
IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...
- M1卡区块控制位详解
M1卡区块控制位详解 Mifare 1S50/Mifare 1S70 每个扇区的密码和存取控制都是独立的,可以根据实际需要设定各自的密码及存取 控制.存取控制为4个字节,共32位,扇区中的每个块(包括 ...
- php_ThinkPHP的RBAC(基于角色权限控制)详解
一.什么是RBAC 基于角色的访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注. 在RBAC中,权限与角色相关联,用户通 ...
- ThinkPHP框架下基于RBAC的权限控制模式详解
这几天因为要做一个项目,需要可以对Web应用中通用功能进行封装,其中一个很重要的涉及到了对用户.角色和权限部分的灵活管理.所以基于TP框架自己封装了一个对操作权限和菜单权限进行灵活配置的可控制模式. ...
- struts2标签库----控制标签详解
前面几篇文章我们介绍了有关struts2的基本内容,简单理解了整个框架的运作流程.从本篇开始我们逐渐了解和使用struts2为我们提供的标签库技术,使用这些标签可以大大降低我们表现层的开发难 ...
- 单片机pwm控制基本原理详解
前言 PWM是Pulse Width Modulation的缩写,它的中文名字是脉冲宽度调制,一种说法是它利用微处理器的数字输出来对模拟电路进行控制的一种有效的技术,其实就是使用数字信号达到一个模拟信 ...
- 两种RBAC权限控制模型详解
序言 由于最近一直卡在权限控制这个坎上,原来设计的比较简单的权限控制思路已经无法满足比较复杂一些的场景,因此一直在探索一种在大部分场景下比较通用的权限模型. 首先,这里说明一下两种RBAC权限模型分别 ...
随机推荐
- php 对问卷结果进行统计
背景: 由于具体工作的原因,我做了一份纸质的问卷调查表,调查表的主要内容是让用户对10项要求(编号为A,B....)进行优先级排序,所以我得到的结果是好几百份类似于A>I>H>G&g ...
- C# 向批处理文件输入字符
先记录个无关标题哒~ 刚刚学习用C#,在用VS进行图形界面编程时,点界面中添加的空间,VS界面右侧会出现该控件的属性页,但是这个属性页并不全, 只列出了部分重要的属性,一开始还以为是没有对应的属性方法 ...
- Max Min
def main(): n = int(raw_input()) k = int(raw_input()) k_arr = [] min_dif = 9999999999 # 根据input要求,规定 ...
- MyFirstStruts2
package com.sdlc.action; public class HelloWorldAction { private String msg; public void setMessage( ...
- Verilog 模块参数重定义(转)
Verilog重载模块参数: 当一个模块引用另外一个模块时,高层模块可以改变低层模块用parameter定义的参数值,改变低层模块的参数值可采用以下两种方式: 1)defparam 重定义参数语法:d ...
- 使用PHPExcel报错 Fatal error: Class 'ZipArchive' not found
Windows PHP5+Apache2.2 解决方法: 打开php.ini 找到: extension=php_xsl.dllextension=php_zip.dll 将其前面的';'去掉. 找到 ...
- win7 x64 jdk1.7.0_51
1:我的 jdk与jre默认安装在:D:\Program Files\Java 2:配置环境变量(系统变量): (1)新建JAVA_HOME (2)新建CLASSPATH (3)编辑Path,%JAV ...
- Linux用户基础
http://itercast.com/lecture/21 操作系统通过用户.组概念来管理使用计算机的人 用户代表一个使用计算机的使用者,操作系统通过用户概念限制一个使用者能够访问的资源 组用来组织 ...
- wireshark使用心得
关于pcap文件的文件解析网上资料有很多,我在这就不说明了 心得一:wireshark Runtime Error 一般来说,wireshark不适合长时间捕获包,也就是随着时间增长,总会报出上述错误 ...
- Python Cookie HTTP获取cookie并处理
Cookie模块同样是Python标准库中的一员,它定义了一些类来解析和创建HTTP 的 cookie头部信息. 一.创建和设置Cookie >>> import Cookie #导 ...