Aub*_*ine 27 java spring interface code-injection
到目前为止,没有人能够在Spring Framework中提供正确的接口注入示例.马丁福勒的文章不是凡人,其他一切只是以一种非常混乱的方式定位.我已经浏览过三篇文章,其中人们要么告诉"Spring不直接支持界面注入"("因为我不确切地知道我将如何仅描述setter和构造函数注入")或者"我会在我的讨论中讨论它"其他线程"或者下面会有一些评论说这是错误的例子.我不要求解释,例如我BEG.
注入有三种类型:构造函数,Setter和接口.Spring不直接支持最新版本(正如我观察人们所说).那怎么做呢?
谢谢,
nan*_*oft 13
DI存在两个主要变体,基于构造函数的依赖注入和基于Setter的依赖注入.
另请参阅Spring中未实现的接口注入明确说明它.
所以DI只有两种变体.因此,如果文档没有说明接口注入,那么很明显它不存在.那些认为通过在接口中提供setter方法来完成接口注入的人回答我:
通过接口注入,接口显式定义了可以设置依赖关系的点:
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)
嗨我尝试了一个非常简单的方法,可以澄清你的答案.
以下是我使用两个接口和两个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)
我还参考了另一个在线教程,然后得到了这种解决方案.如果您对此代码有任何问题,请告诉我.它对我有用.
有 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)感到困惑。
归档时间: |
|
查看次数: |
80815 次 |
最近记录: |