您的位置:首页 > 编程语言 > Java开发

2.笔记JAVA框架学习——IOC概念及Bean配置

2017-11-07 23:26 381 查看
2.笔记JAVA框架学习——IOC概念及Bean配置

IOC

控制反转(Inversionof Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。控制反转一般分为两种类型,依赖注入(Dependency Injection,简称DI)和依赖查找(DependencyLookup)。依赖注入应用比较广泛。

DI

是IOC的另一种表述方式。

DI—Dependency Injection,即“依赖注入”组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中依赖注入的目的并非为软件系统带来更多功能,而是为了提升组件重用的频率,并为系统搭建一个灵活、可扩展的平台。通过依赖注入机制,我们只需要通过简单的配置,而无需任何代码就可指定目标需要的资源,完成自身的业务逻辑,而不需要关心具体的资源来自何处,由谁实现。

当然是应用程序依赖于IoC容器应用程序需要IoC容器来提供对象需要的外部资源

IoC容器注入应用程序某个对象,应用程序依赖的对象  注入某个对象所需要的外部资源(包括对象、资源、常量数据)

  IoC和DI是同一个概念的不同角度描述,由于控制反转概念比较含糊MartinFowler又给出了一个新的名字:“依赖注入”,相对IoC 而言,“依赖注入”明确描述了“被注入对象依赖IoC容器配置依赖对象”。

IOC 容器实现

Spring 提供了两种类型的 IOC 容器实现.

l  BeanFactory: IOC 容器的基本实现.

l  ApplicationContext: 提供了更多的高级特性. 是 BeanFactory的子接口.

BeanFactory 是 Spring 框架的基础设施,面向Spring 本身;ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory

无论使用何种方式, 配置文件时相同的.

ApplicationContext的主要实现类:

l  ClassPathXmlApplicationContext:从 类路径下加载配置文件

l  FileSystemXmlApplicationContext: 从文件系统中加载配置文件

从 IOC 容器中获取 Bean

调用 ApplicationContext 的 getBean() 方法

依赖注入的方式

Spring 支持 3 种依赖注入的方式

l  属性注入

l  构造器注入

l  工厂方法注入(很少使用,不推荐)

属性注入,例如使用setter方法。

构造方法注入, 通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。

构造方法示例

Car.java如下:

publicclass Car {
 
      private String
company;
      private String
brand;
 
      privateintmaxSpeed;
      privatefloatprice;
 
      public Car(String
company, String
brand,
floatprice) {
            super();
            this.company
= company;
            this.brand
= brand;
            this.price
= price;
      }
 
      public Car(String
company, String
brand,
intmaxSpeed) {
            super();
            this.company
= company;
            this.brand
= brand;
            this.maxSpeed
= maxSpeed;
      }
 
      public Car(String
company, String
brand,
intmaxSpeed,
floatprice) {
            super();
            this.company
= company;
            this.brand
= brand;
            this.maxSpeed
= maxSpeed;
            this.price
= price;
      }
 
      @Override
      public String toString() {
            return"Car[company="
+ company +
",brand=" +
brand +
", maxSpeed="
                        +
maxSpeed +
",price=" +
price +
"]";
      }
}
然后在app.xml中定义bean如下:

      <bean
id="car"class="Car">
            <constructor-arg
value="KUGA"index="1"></constructor-arg>
            <constructor-arg
value="ChangAnFord"index="0"></constructor-arg>
            <constructor-arg
value="250000"type="float"></constructor-arg>
      </bean>
 
      <bean
id="car2"class="Car">
            <constructor-arg
value="ChangAnMazda"></constructor-arg>
            <!-- 若字面值中包含特殊字符,
则可以使用 DCDATA
来进行赋值. (了解) -->
            <constructor-arg>
                  <value><![CDATA[<ATARZA>]]></value>
            </constructor-arg>
            <constructor-arg
value="180"type="int"></constructor-arg>
      </bean>

最后在main.java中如下:

import org.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclass Main {
            publicstaticvoid
main(String[] args) {             

            //1.
创建Spring
的 IOC
容器
            ApplicationContext
apx =
new ClassPathXmlApplicationContext("app.xml");      

            Car
car = (Car)
apx.getBean("car");
            System.out.println(car);
           
            Car
car2 = (Car)
apx.getBean("car2");
            System.out.println(car2);
           
      }
}
执行如下:

Hello: Jerry
Car [company=ChangAnFord, brand=KUGA, maxSpeed=0, price=250000.0]
Car[company=ChangAnMazda, brand=<ATARZA>, maxSpeed=180, price=0.0]

友情链接

 http://jinnianshilongnian.iteye.com/blog/1413846

eclipse Tips

outline

单栏选择window->showview->outlline

自动生成构造类等

类名上点右键—> Source –> Generate Gettersand Setters

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: