下面的朋友是我的代码,我正在尝试使用Spring运行依赖注入
我有一个接口,该接口的两个类实现。
一个bean.xml和一个主方法类。
接口IWriter.java
package DI;
public interface IWriter {
public void writer(String s);
}
Class Writer.java
package DI;
import org.springframework.stereotype.Service;
@Service
public class Writer implements IWriter {
public void writer (String s){
System.out.println(s);
}
}
Class NiceWriter.java
package DI;
import org.springframework.stereotype.Service;
@Service
public class NiceWriter implements IWriter {
public void writer (String s){
System.out.println("The string is " + s);
}
}
另一个班级
package DI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class MySpringBeanWithDependency {
@Autowired
private IWriter writer;
public void run() {
String s = "This is my test";
writer.writer(s);
}
}
Main.java
package DI;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import DI.MySpringBeanWithDependency;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
BeanFactory factory = context;
MySpringBeanWithDependency test = (MySpringBeanWithDependency) factory.getBean("mySpringBeanWithDependency");
test.run();
}
}
bean.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"
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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="DI" />
</beans>
当我运行代码时,Spring容器给出了Writer.java类方法的输出。我没有指定选择哪个实现。 Spring如何获得Writer.java的实现?
答案 0 :(得分:10)
如果有多个接口实现,并且在这种情况下使用@Autowired,则绑定任何类。但是如果你想自动装配特定的实现,那么你可以使用
@Qualifier( "<implementing class name>" )
你必须了解的关于Spring的一些事情
通过将一个bean的实例放入另一个bean的实例中的所需字段中来实现自动装配。这两个类都应该是bean,即它们应该被定义为存在于应用程序上下文中。
答案 1 :(得分:7)
按如下方式更改您的代码。
Class Writer.java
package DI;
import org.springframework.stereotype.Service;
@Service("writer")
public class Writer implements IWriter {
public void writer (String s){
System.out.println(s);
}
}
Class NiceWriter.java
package DI;
import org.springframework.stereotype.Service;
@Service("niceWriter")
public class NiceWriter implements IWriter {
public void writer (String s){
System.out.println("The string is " + s);
}
}
另一个班级
package DI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class MySpringBeanWithDependency {
@Autowired
@Qualifier("writer")//if you need to autowire Writer service
private IWriter writer;
@Autowired
@Qualifier("niceWriter")//if you need to autowire NiceWriter service
private IWriter niceWriter
public void run() {
String s = "This is my test";
writer.writer(s);
}
}
答案 2 :(得分:1)
试试这个。
Class Writer.java
package DI;
import org.springframework.stereotype.Service;
@Service("writer")
public class Writer implements IWriter {
public void writer (String s){
System.out.println(s);
}
}
Class NiceWriter.java
package DI;
import org.springframework.stereotype.Service;
@Service("niceWriter")
public class NiceWriter implements IWriter {
public void writer (String s){
System.out.println("The string is " + s);
}
}
另一个班级
package DI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class MySpringBeanWithDependency {
@Autowired
private IWriter writer;
@Autowired
private IWriter niceWriter
public void run() {
String s = "This is my test";
writer.writer(s);
}
}
答案 3 :(得分:1)
我想使用application.properties
再显示一个选项。
好处:
基于@ConditionalOnProperty属性的示例代码
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
@Service
@ConditionalOnProperty(value = "writer.type", havingValue = "default")
public class Writer implements IWriter {
@Override
public void writer(String s) {
System.out.println("The string is " + s);
}
}
和
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
@Service
@ConditionalOnProperty(value = "writer.type", havingValue = "nice")
public class NiceWriter implements IWriter {
@Override
public void writer(String s) {
System.out.println("Nice string is " + s);
}
}
当application.properties
包含writer.type=nice
时,将为IWriter界面实例化NiceWriter。
除了@ConditionalOnProperty之外,还有其他选项,例如Conditional,@ConditionalOnExpression。
答案 4 :(得分:0)
这取决于用例,您可以使用spring概要文件,自定义注释或使用@Qualifier(按名称注入)来选择接口的实现,等效于JSR-330的@Named注释>