简体   繁体   English

Spring和Java接口

[英]Spring and Java interfaces

While reading some advanced book on developing the enterprise applications, I constantly see the following pattern that could be illustrated by the following example:. 在阅读一些有关开发企业应用程序的高级书籍时,我不断看到以下示例可以说明的以下模式:

public interface Oracle {
    String defineMeaningOfTheLife();
}
public class BookwormOracle implements Oracle {

    public String defineMeaningOfTheLife() {

        return "Use life, dude!";
    }
}

And the main function: 和主要功能:

  public static void main(String[] args) {
XmlBeanDefinitionReader rdr = new XmlBeanDefinitionReader(factory);
        rdr.loadBeanDefinitions(new ClassPathResource(
                "META-INF/spring/xml-bean-factory-config.xml"));

        Oracle oracle = (Oracle) factory.getBean("oracle");
        System.out.println(oracle.defineMeaningOfTheLife());
    }

And the xml config: 和xml配置:

<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.xsd">


<bean id="oracle" name="wiseworm" class="BookwormOracle" />

As far as I understood, it is not possible to instantiate the interface. 据我了解,无法实例化该接口。 But, using Spring framework it seems to me that it is possible to do so. 但是,在我看来,使用Spring框架是可能的。 Since, how does the Spring framework manage to do it? 既然如此,Spring框架如何做到这一点? From pure Java perspective the code 从纯Java角度看,代码

Oracle oracle = new Oracle();

is rather wrong. 是相当错误的。

Spring also needs an implementation of the interface to instanciate the bean and make it available to your application. Spring还需要接口的实现来实例化Bean,并将其提供给您的应用程序。 It is actually what is defined in your context file ( xml-bean-factory-config.xml ): 它实际上是您的上下文文件( xml-bean-factory-config.xml )中定义的内容:

<bean id="oracle" name="wiseworm" class="BookwormOracle" />

In the spring context, you define which implementation of the interface Oracle you want to create. 在spring上下文中,定义要创建的Oracle接口的实现。 When your main method call: 当您的主要方法调用时:

Oracle oracle = (Oracle) factory.getBean("oracle");

it asks to Spring to get the bean with id "oracle" which is an implementation of your Oracle interface. 它要求Spring获取ID为“ oracle”的bean,这是Oracle接口的实现。

You shouldfirst understand about DI(Dependency Injection) and IoC(Inversion of Control) . 您应该首先了解DI(依赖注入)和IoC(控制反转)。 Please google it . 请用谷歌搜索。

I would recommend you this article from Martin Fowler on Ioc. 我会向您推荐Ioc上Martin Fowler的这篇文章。

http://martinfowler.com/bliki/InversionOfControl.html enter link description here http://martinfowler.com/bliki/InversionOfControl.html 在此处输入链接描述

Thanks 谢谢

This line <bean id="oracle" name="wiseworm" class="BookwormOracle" /> is equal to the below java code. 这行<bean id="oracle" name="wiseworm" class="BookwormOracle" />等于下面的java代码。

BookwormOracle oracle = new BookwormOracle();

It just happened to have the name of the variable as oracle in the spring configuration, rather spring actually initializing the concrete class BookwormOracle . 刚好在spring配置中变量名叫oracle,而spring实际上初始化了具体的类BookwormOracle Later you are just asking the spring container to get that bean with the name oracle with below line. 稍后,您只是要求spring容器获取名称为oracle且位于下一行的bean。

Oracle oracle = (Oracle) factory.getBean("oracle");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM