首页 > 编程语言 > 详细

Spring学习之Ioc

时间:2016-01-27 22:56:00      阅读:219      评论:0      收藏:0      [点我收藏+]

Ioc

  Ioc容器实现了对象的生命周期管理,降低了组件间的耦合度,将耦合推迟到了配置文件中,实现了软件各层间的解耦。

Ioc控制反转

  即应用本身不负责依赖对象的创建及维护,而是由外部容器负责,这样控制权就由应用转移到了外部容器,控制权的转移就是所谓反转。

Spring Ioc搭建

  技术分享

Ioc应用

  1.项目数据库的切换

技术分享

技术分享

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5          xmlns:aop="http://www.springframework.org/schema/aop"
 6          xmlns:tx="http://www.springframework.org/schema/tx"
 7          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 8            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 9            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
10     
11     <!-- 数据库变换 -->
12     <bean id="userdao_mysql" class="dao.UserDaoMysqlImpl"/>
13     <bean id="userdao_oracle" class="dao.UserDaoOracleImpl"/>
14 </beans>
applicationContext.xml
技术分享
 1 package test;
 2 
 3 import org.springframework.beans.factory.BeanFactory;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import dao.UserDao;
 7 
 8 public class Test {
 9     public static void main(String[] args) {
10         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
11         UserDao userDao_mysql = (UserDao)factory.getBean("userdao_mysql");
12         userDao_mysql.add();
13         UserDao userDao_oracle = (UserDao)factory.getBean("userdao_oracle");
14         userDao_oracle.add();
15     }
16 }
Test.java

  

  2.类初始化赋值

    技术分享

    技术分享

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5          xmlns:aop="http://www.springframework.org/schema/aop"
 6          xmlns:tx="http://www.springframework.org/schema/tx"
 7          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 8            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 9            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
10 <!-- 类赋值 -->
11     <bean id="user" class="entity.User">
12         <property name="name" value="Tom2"/>
13         
14         <!-- 集合 -->
15         <property name="users">
16             <list>
17                 <value>Zhengbin1</value>
18                 <value>Zhengbin2</value>
19             </list>
20         </property>
21     </bean>
22     
23     <!-- ref 用来引用类
24          一个 教师 负责多个 学生 -->
25     <bean id="teacher" class="entity.Teacher">
26         <property name="user" ref="user"/>
27     </bean>
28 </beans>
applicationContext.xml
技术分享
 1 package test;
 2 
 3 import org.springframework.beans.factory.BeanFactory;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import entity.Teacher;
 7 import entity.User;
 8 
 9 public class Test1 {
10     public static void main(String[] args) {
11         BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
12         User u = (User)beanFactory.getBean("user");
13         System.out.println(u.getName());
14         System.out.println(u.getUsers());
15         
16         Teacher t = (Teacher)beanFactory.getBean("teacher");
17         System.out.println(t.getUser().getName());
18     }
19 }
Test1.java

  输出:

    技术分享

  

  3.Ioc的类初始化与单例模式

    技术分享

    技术分享

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5          xmlns:aop="http://www.springframework.org/schema/aop"
 6          xmlns:tx="http://www.springframework.org/schema/tx"
 7          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 8            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 9            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
10     
11 <!-- scope 定义bean是否是单例
12          如果为 singleton 则在 BeanFactory 作用范围内,只维护此Bean的一个实例
13          如果为 prototype(原型) 则在 BeanFactory 将为每次Bean请求创建一个新的Bean实例 -->
14     <bean id="clazz" class="entity.Clazz" lazy-init="true" scope="prototype">
15         <property name="name" value="zhengbin"/>
16     </bean>
17 </beans>
applicationContext.xml
技术分享
 1 package test;
 2 
 3 import org.springframework.beans.factory.BeanFactory;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import entity.Clazz;
 7 
 8 public class Test2 {
 9     public static void main(String[] args) {
10 //        Spring 默认情况下,在读取配置文件时,已经完成创建对象,提高了系统性能
11         BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
12         System.out.println("--------------");
13         Clazz c = (Clazz)beanFactory.getBean("clazz");
14         Clazz c1 = (Clazz)beanFactory.getBean("clazz");
15         System.out.println(c.getName());
16         System.out.println(c1.getName());
17         System.out.println(c==c1);
18         c.setName("ZhengBin");
19         System.out.println(c.getName() + "----" + c1.getName());
20     }
21 }
Test2.java

 

  

 

  

Spring学习之Ioc

原文:http://www.cnblogs.com/zhengbin/p/5164729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!