基本的代码结构

1 IOC包下

基本的spring创建对象

将类添加到配置文件中,由容器创建。

Source code    
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <!--
  7. 把HelloWorld这个类纳入spring容器
  8. id为bean的唯一标识
  9. 正规的写法:
  10. 类的第一个字母变成小写,其余不变
  11. class为类的全名
  12. -->
  13. <bean id="helloWorld" class="cn.itcast.spring0401.ioc.HelloWorld"></bean>
  14. </beans>

helloworld类

Source code    
  1. package cn.itcast.spring0401.ioc;
  2. public class HelloWorld {
  3. public void say(){
  4. System.out.println("hello");
  5. }
  6. }

测试类

这就是控制反转

Source code    
  1. package cn.itcast.spring0401.ioc;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. /**
  6. * 控制反转
  7. * @author Administrator
  8. *
  9. */
  10. public class IOCTest {
  11. /**
  12. * 启动spring容器
  13. *         创建spring容器对象就相当于启动spring容器
  14. * spring容器做的工作:
  15. * * 创建对象
  16. */
  17. @Test
  18. public void testHelloWorld(){
  19. ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/ioc/applicationContext.xml");
  20. HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
  21. helloWorld.say();
  22. }
  23. }

2 alias包,别名

配置文件,可以给类起别名

Source code    
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="helloWorld" class="cn.itcast.spring0401.alias.HelloWorld"></bean>
  7. <!--
  8. name与bean中的id对应
  9. alias 名字
  10. -->
  11. <alias alias="狗蛋" name="helloWorld"/>
  12. <alias alias="王三麻子" name="helloWorld"/>
  13. </beans>

测试方法

Source code    
  1. package cn.itcast.spring0401.alias;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class AliasTest {
  6. @Test
  7. public void test(){
  8. ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/alias/applicationContext.xml");
  9. HelloWorld helloWorld = (HelloWorld)context.getBean("王三麻子");
  10. helloWorld.say();
  11. }
  12. }

输出结果还是一样的。

3 createbean  创建对象的几种方法

* 利用默认的构造函数
* 利用静态工厂方法
* 利用实例工厂方法

配置文件

Source code    
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="helloWorld" class="cn.itcast.spring0401.createbean.HelloWorld"></bean>
  7. <!--
  8. -->
  9. <bean id="helloWorld1" class="cn.itcast.spring0401.createbean.HelloWorldFactory" factory-method="createBean"></bean>
  10. </beans>

工厂类

Source code    
  1. package cn.itcast.spring0401.createbean;
  2. public class HelloWorldFactory {
  3. /**
  4. * 工厂方法
  5. * @return
  6. */
  7. public static HelloWorld createBean(){
  8. return new HelloWorld();
  9. }
  10. }

helloworld

Source code    
  1. package cn.itcast.spring0401.createbean;
  2. public class HelloWorld {
  3. public void say(){
  4. System.out.println("hello");
  5. }
  6. public HelloWorld(){
  7. System.out.println("new helloworld");
  8. }
  9. }

测试类

Source code    
  1. package cn.itcast.spring0401.createbean;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class CreateBeanTest {
  6. /**
  7. * 在HelloWorld中写如下代码
  8. *         public HelloWorld(){
  9. System.out.println("new helloworld");
  10. }
  11. 输出为:"new helloworld"
  12. 说明
  13. * spring容器默认调用类的默认的构造器来创建对象的
  14. * 如果在HelloWorld中,没有默认的构造器,则spring在创建helloWorld对象时,会报错
  15. 因为找不到默认的构造器
  16. */
  17. @Test
  18. public void testConstructor(){
  19. ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/createbean/applicationContext.xml");
  20. HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
  21. helloWorld.say();
  22. }
  23. /**
  24. * spring容器做的事情:
  25. * * spring容器调用了工厂类的工厂方法
  26. * * 真正创建对象new HelloWorld()是由程序员来完成的
  27. */
  28. @Test
  29. public void testFactory(){
  30. ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/createbean/applicationContext.xml");
  31. HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld1");
  32. helloWorld.say();
  33. }
  34. }

4init包,创建对象的时机

默认情况下,在spring启动的时候,创建纳入spring容器中所有的bean
在spring容器启动的时候,可以检查错误
但是如果bean的属性中有数据,会过早的加载到内存中,所以如果bean中有数据
应该把数据的对象的声明放在方法中
* 如果在spring的配置文件中,有lazy-init为true,则context.getBean(“beanId”)时
才要创建对象
缺点:在spring容器启动的时候,是检验不出错误的

配置文件

Source code    
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="helloWorld" class="cn.itcast.spring0401.init.HelloWorl" lazy-init="true"></bean>
  7. </beans>

helloworld

Source code    
  1. package cn.itcast.spring0401.init;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class HelloWorld {
  5. public List<String> sList = new ArrayList<String>();
  6. public void say(){
  7. System.out.println("hello");
  8. }
  9. public HelloWorld(){
  10. System.out.println("aaa");
  11. }
  12. }

测试

Source code    
  1. package cn.itcast.spring0401.init;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class InitTest {
  6. /**
  7. * * 在默认情况下,spring容器启动的时候,就把所有的纳入spring容器的bean创建对象
  8. * 缺点:
  9. *     如果一个对象中有属性,比如这个属性为集合,在创建这个对象的过程中,集合中有数据
  10. * 这样采用默认的启动形式,就会导致数据过早的加载到内存中
  11. * * 可以在spring的配置文件中:
  12. * <bean id="helloWorld" class=".." lazy-init="true">
  13. * 延迟bean的创建时间,在context.getBean时才要创建bean的对象
  14. * 如果spring的配置文件书写错误,如果所有的spring的bean都采用lazy-init="true"这种形式
  15. 则在启动web服务器的时候,发现不了spring容器的错误,这样是不利于排错的
  16. */
  17. @Test
  18. public void test(){
  19. ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/init/applicationContext.xml");
  20. HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
  21. helloWorld.say();
  22. }
  23. }

5scope 对象作用范围,单例多里

在配置文件中,scope为
“singleton”
* 默认值
* spring产生的bean只有一个实例
* 处于单例模式的bean的创建、初始化、销毁都是受spring容器管理的
* 在容器关闭的时候执行销毁工作
“prototype”
* 多例
* spring容器负责该bean的创建、初始化,但是销毁工作程序员做
* 无论该bean的lazy-init为什么值,都在context.getBean时创建对象
* 如果该bean中有资源对象,手动关闭

配置

Source code    
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <!--
  7. scope
  8. 决定bean的范围
  9. singleton 单例 默认
  10. prototype 原型 多例
  11. -->
  12. <bean id="helloWorld" class="cn.itcast.spring0401.scope.HelloWorld" scope="singleton"></bean>
  13. </beans>

测试

Source code    
  1. package cn.itcast.spring0401.scope;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class ScopeTest {
  6. /**
  7. * 由spring容器产生的bean默认是单例模式
  8. * 在spring的配置文件中:
  9. *      scope:
  10. * singleton 默认的形式
  11. * 如果写默认的形式,一个集合或者一个数据出现在了类的属性中,这个数据将成为全局的数据(共享数据),应该
  12. * 注意并发问题
  13. * 当spring容器中的bean是多例,则不管配置文件中的lazy-init为default、false还是true,在
  14. * context.getBean时才要为bean创建对象
  15. */
  16. @Test
  17. public void test(){
  18. ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/scope/applicationContext.xml");
  19. HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
  20. helloWorld.s.add("aaa");
  21. helloWorld.s.add("bb");
  22. HelloWorld helloWorld1 = (HelloWorld)context.getBean("helloWorld");
  23. helloWorld1.s.add("cc");
  24. System.out.println(helloWorld.s.size());
  25. }
  26. }

6initdestory

对象的初始化,销毁

配置文件

Source code    
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <!--
  7. init-method
  8. 对象的初始化方法
  9. destroy-method
  10. 对象的销毁方法
  11. -->
  12. <bean id="helloWorld" class="cn.itcast.spring0401.initdestroy.HelloWorld" init-method="init" destroy-method="destroy" scope="prototype"></bean>
  13. </beans>

测试

Source code    
  1. package cn.itcast.spring0401.initdestroy;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class InitDestroyTest {
  6. /**
  7. * 在spring的配置文件中
  8. *     init-method="init"
  9. * 说明在创建完该对象后,立刻执行init方法,用来进行初始化
  10. * destroy-method="destroy"
  11. * * 当该bean为单例模式,才能调用该方法
  12. * destroy方法在容器销毁的时候被调用
  13. * * 当该bean为多例时,spring容器不负责他的销毁工作
  14. * * 如果该bean为多例时,当不用该bean时,应该手动销毁资源文件
  15. */
  16. @Test
  17. public void test(){
  18. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring0401/initdestroy/applicationContext.xml");
  19. HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
  20. helloWorld.say();
  21. context.destroy();//销毁spring容器
  22. }
  23. }

hello

Source code    
  1. package cn.itcast.spring0401.initdestroy;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class HelloWorld {
  5. public void say(){
  6. System.out.println("hello");
  7. }
  8. public void init(){
  9. System.out.println("init");
  10. }

spring2.5IOC控制反转详解的更多相关文章

  1. 分享知识-快乐自己:Spring_IOC(控制反转)详解

    IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...

  2. Spring IOC(控制反转)详解及示例

    控制反转——Spring通过一种称作控制反转(IOC)的技术促进了低耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象.你可以认为IoC与JN ...

  3. coding++:Spring_IOC(控制反转)详解

    IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...

  4. M1卡区块控制位详解

    M1卡区块控制位详解 Mifare 1S50/Mifare 1S70 每个扇区的密码和存取控制都是独立的,可以根据实际需要设定各自的密码及存取 控制.存取控制为4个字节,共32位,扇区中的每个块(包括 ...

  5. php_ThinkPHP的RBAC(基于角色权限控制)详解

    一.什么是RBAC 基于角色的访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注. 在RBAC中,权限与角色相关联,用户通 ...

  6. ThinkPHP框架下基于RBAC的权限控制模式详解

    这几天因为要做一个项目,需要可以对Web应用中通用功能进行封装,其中一个很重要的涉及到了对用户.角色和权限部分的灵活管理.所以基于TP框架自己封装了一个对操作权限和菜单权限进行灵活配置的可控制模式. ...

  7. struts2标签库----控制标签详解

         前面几篇文章我们介绍了有关struts2的基本内容,简单理解了整个框架的运作流程.从本篇开始我们逐渐了解和使用struts2为我们提供的标签库技术,使用这些标签可以大大降低我们表现层的开发难 ...

  8. 单片机pwm控制基本原理详解

    前言 PWM是Pulse Width Modulation的缩写,它的中文名字是脉冲宽度调制,一种说法是它利用微处理器的数字输出来对模拟电路进行控制的一种有效的技术,其实就是使用数字信号达到一个模拟信 ...

  9. 两种RBAC权限控制模型详解

    序言 由于最近一直卡在权限控制这个坎上,原来设计的比较简单的权限控制思路已经无法满足比较复杂一些的场景,因此一直在探索一种在大部分场景下比较通用的权限模型. 首先,这里说明一下两种RBAC权限模型分别 ...

随机推荐

  1. 总结配置搭建tomcat时碰到的一些小问题

    1.环境变量的配置 在配置tomcat的环境变量时始终配置不对,于是首先检查之前java的环境变量是否正确,发现java命令可以用但是javac却找不到,自己又瞎搞一通,终于javac可以用了,但ja ...

  2. winPcap_1_开篇

    什么是WinPcap WinPcap是一个基于Win32平台的,用于捕获网络数据包并进行分析的开源库. 因为有些应用程序需要直接访问网络中的数据包.也就是说,那些应用程序需要访问原始数据包,即没有被操 ...

  3. No-args constructor for class does not exist. Register an InstanceCreator with G

    有时候我们在使用Googel官方的json解析包时,如果自己的实体类中出现代参的构造函数.在1.4的jar中,如果类造型中有参数,就会调用不了无参构造器,(如:HashMap的构造器就会有参数) 参考 ...

  4. linux for循环

    一定要记得写后面的分号:http://www.runoob.com/linux/linux-shell-variable.html 这个页面的课程的循环教程是有问题的 for color in yel ...

  5. MSSQL-资源地址

    1.SQL-SERVER客户端管理工具.下载地址 https://msdn.microsoft.com/en-us/library/mt238290.aspx?f=255&MSPPError= ...

  6. 转载:js实现上传图片时 点击浏览后 就可以看到缩略图 很实用

    转载网址:http://blog.sina.com.cn/s/blog_6094f04d0100o6kj.html <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  7. php array_combine 把array的默认键改成指定的字符串

    array(2) { [0] => array(6) { [0] => string(1) "1" [1] => string(10) "1470650 ...

  8. http_load压力测试

    http_load是基于linux平台的性能测试工具,它体积非常小,仅100KB.它以并行复用的方式运行,可以测试web服务器的吞吐量与负载. 一.安装http_load A.进入/usr/local ...

  9. Spark添加/更改集群节点需要修改的配置文件

    笔记:在配置好了spark后,如果需要添加/删除一个结点需要修改如下配置文件 cd $HADOOP/etc/hadoop 进入hadoop配置文件夹下 修改 slaves,将对应的节点添加/删除 修改 ...

  10. php爬虫的两种思路

    写php爬虫可能最大的问题就是php脚本执行时间的问题了,对于这个问题,我找到了两种解决方法. 第一种通过代码set_time_limit(0)或者ini_set("max_executio ...