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

超简单Spring入门(一)Dependency Injection

2011-04-26 17:32 260 查看
首先是article的link:

http://www.vogella.de/articles/SpringDependencyInjection/article.html

什么是Spring?

和JMS,JDBC这类实现某些特殊功能的组件不同,Spring出现的目的是让代码变的更灵活,更简单。

Spring的核心思想是IOC(inversion of control)。

The main idea behind IoC and the Spring Framework itself is that code should be

as simple as possible while still supporting all of the complex requirements of enterprise

applications. In other words, those complexities can’t be wholly ignored but ideally

shouldn’t have a negative impact on developer productivity or software quality. To

accomplish that goal, the responsibility of controlling those complexities should be

inverted from the application code to a framework.


而IOC的具体实现就是dependency injection和mothod invocation。

文章中首先讲的是dependency injection。因为它是Spring框架的思想基础 .

Dependency指一个class调用的其他class。比如:如果类A调用了类B,那么类B就是类A的一个dependency(或者说类B是类A的一个依赖)。

由于面向对象的编程思想,我们希望一个类越独立越好,这样做的优势就不细谈了。

而Injection指一个类调用它的dependency时,调用什么样的对象不是这个类的代码本身决定的,而是由配置或其他更灵活的方式决定的。或者说,实际调用的对象是被“注入”的。

原文中有这样一句话:

A class should not configure itself but should be configured from outside.
而这句话就是Spring要实现的事情。

对于DI来说Spring:

Spring just add some simplifications in using dependency injection by providing a standard way of providing the configuration and by managing the reference to the created objects.

Spring的功能仅仅就是给实现dependency injection提供了一些便利,通过提供一个标准的配置方法,同时管理一些创建对象的参考。

再说的直白一点:Spring框架就是通过配置和管理一个或多个配置文件(或annotation),来实现class的dependency injection。

These classes which are managed by Spring must conform to the JavaBean standard.

In the context of Spring classes are also referred to as beans or as spring beans. 被Spring配置和管理的那些dependency实际上被称作beans或spring beans。

The Spring core container:

1,handles the configuration, generally based on annotations or on an XML file (XMLBeanFactory)

2,manages the selected Java classes via the Bean Factory

The core container uses the so-called bean factory to create new objects. New objects are generally created as Singletons if not specified differently.

spring配置文件示例

接下来文中举了一个例子,非常非常简单。非常有必要自己建一个工程试试。这里仅简单解释一下配置文件和main方法。

文中创建的Spring configuration file name 是:beans.xml (文件名可以是任意的)

内容是:

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="writer" class="org.lillian.Spring.Writer" />

<!--writer是要被inject的dependency-->

<!--换个角度理解,writer是在Spring application中会被auto-wired beans-->

<!-- 将org.lillian.Spring.Writer改为org.lillian.Spring.NiceWriter 再运行Main就能看到Spring按配置调用了不同的类-->

<bean id="mySpringBeanWithDependency" class="org.lillian.Spring.MySpringBeanWithDependency">

<property name="writer" ref="writer" />

<!--在这里对象writer被注入进来。这里writer已经被spring实例化了,所以注入的是一个对象-->

</bean>

<bean id="Reader" class="org.lillian.Spring.Reader" />

<!--注意spring会实例化配置文件中所有的bean,即使这个bean没有被其他任何对象引用-->

</beans>

当然了,涉及的这几个org.lillian.Spring.Writer等类,大家去原文中找吧。非常简单。

如何在代码中使用

下面是main函数,我们看看在main中,怎么使用被spring实例化的对象:

The Spring application context is fully responsible for the creation of and wiring of the objects that make up the application.

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.lillian.Spring.MySpringBeanWithDependency;;

public class Main

{

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(

"META-INF/beans.xml"); //load Spring的配置文件,也就是上面的那个xml

BeanFactory factory = context;

//getBean得到了我们在spring中配置的bean对象, MySpringBeanWithDependency bean,然后就可以运行该实例的方法了。

MySpringBeanWithDependency test = (MySpringBeanWithDependency) factory

.getBean("mySpringBeanWithDependency");

test.run();

}

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