Spring接口注入示例

Aub*_*ine 27 java spring interface code-injection

到目前为止,没有人能够在Spring Framework中提供正确的接口注入示例.马丁福勒的文章不是凡人,其他一切只是以一种非常混乱的方式定位.我已经浏览过三篇文章,其中人们要么告诉"Spring不直接支持界面注入"("因为我不确切地知道我将如何仅描述setter和构造函数注入")或者"我会在我的讨论中讨论它"其他线程"或者下面会有一些评论说这是错误的例子.我不要求解释,例如我BEG.

注入有三种类型:构造函数,Setter和接口.Spring不直接支持最新版本(正如我观察人们所说).那怎么做呢?

谢谢,

nan*_*oft 13

根据DI的Variants在春天

DI存在两个主要变体,基于构造函数的依赖注入和基于Setter的依赖注入.

另请参阅Spring中未实现的接口注入明确说明它.

所以DI只有两种变体.因此,如果文档没有说明接口注入,那么很明显它不存在.那些认为通过在接口中提供setter方法来完成接口注入的人回答我:

  1. 为什么spring ref doc提到了界面注入?
  2. 为什么不能通过提供setter方法来接口注入被视为setter注入本身.为什么在引入接口不影响任何东西时为其创建特殊术语,我的意思是它仍然以相同的方式配置.如果它们不同,那么如何通过查看配置找到它.不应该透明,在配置中没有看到实际配置类实现某些接口的impl?
  3. 就像Instantiation使用实例工厂方法Instantiation使用静态工厂方法一样,一些bean属性应该澄清接口注入?

  • 我自己花了相当长的时间解决这个问题后,我相信nanosoft花费了足够的精力来反驳其他答案。至少没有证据或示例可以使使用Spring IoC中带有接口注入的接口的setter / constructor DI感到困惑。再次做好研究和洞察力。 (2认同)

Ste*_*fan 8

通过接口注入,接口显式定义了可以设置依赖关系的点:

interface InjectPerson {
    public void injectHere(Person p);
}

class Company implements InjectPerson {
   Person injectedPerson; 

   public void injectHere(Person p) {
        this.injectedPerson = p;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不,注射是在其他地方完成的.这只是说"一家公司应该能够让一个人注入". (2认同)

Vin*_*dey 6

嗨我尝试了一个非常简单的方法,可以澄清你的答案.

以下是我使用两个接口和两个bean类构建的代码.

名为Job的第一个接口.

public interface Job {
    public void setmyJob(String myJob);
    public String getmyJob();
}
Run Code Online (Sandbox Code Playgroud)

和一个类实现此接口,名称为MyJob

public class MyJob implements Job {
    public String myJob;

    public MyJob() {
        System.out.println("From MyJob default Constructor and the ID= "+this);
    }

    public void setmyJob(String myJob) {
        this.myJob=myJob;
    }

    public String getmyJob() {
        return myJob;
    }
}
Run Code Online (Sandbox Code Playgroud)

在下一步中,我创建了另一个名为Service的接口

public interface Service {
    public void setJob(Job job);
    public Job getJob();
}
Run Code Online (Sandbox Code Playgroud)

然后再次实现此服务接口的另一个类.

public class MyService implements Service {

    public Job job;

    public void setJob(Job job) {
        this.job=job;
        System.out.println("Hello from Myservice: Job ID="+job);
    }

    public Job getJob() {
        return job;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我用主函数在主类上创建并编写如下代码:

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {

    public static void main(String...a) {

        BeanFactory beanfactory=new ClassPathXmlApplicationContext("Beans.xml");

        MyService myservice=(MyService)beanfactory.getBean("myservice");
        System.out.println("Before print");
        System.out.println(myservice.getJob().getmyJob());
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的Beans.xml文件中,我提到了如下代码并且它有效.

 <?xml version="1.0" encoding="UTF-8"?>
 <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-3.0.xsd">


    <bean id="myjob" class="MyJob">
        <property name="myJob" value="My First String"/>
    </bean>

    <bean id="myservice" class="MyService">
        <property name="job" ref="myjob"/>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

我还参考了另一个在线教程,然后得到了这种解决方案.如果您对此代码有任何问题,请告诉我.它对我有用.


Goy*_*cky 5

有 3 种类型的依赖注入:-

1. Constructor Injection(E.g Pico Container, Spring supports it).
2. Setter Injection(E.g Spring supports it).
3. Interface Injection(E.g Avalon, Spring does not support it).
Run Code Online (Sandbox Code Playgroud)

Spring仅支持基于构造函数和setter的注入。看起来你对不同的类型(3)和 spring 支持的类型(其中 2)感到困惑。