11

下面的朋友是我的代码,我正在尝试使用 Spring 运行依赖注入

我有一个接口,该接口的两个类实现。

一个 bean.xml 和一个主要方法类。

接口 IWriter.java

package DI;
    public interface IWriter {
    public void writer(String s);
}  

类 Writer.java

     package DI;

     import org.springframework.stereotype.Service;

     @Service
     public class Writer implements IWriter {
        public void writer (String s){
            System.out.println(s);
        }
     } 

类 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);
    }
}

主.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 的?

4

5 回答 5

10

如下更改您的代码。

类 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);
     }
    } 

类 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);
      }
    }
于 2013-07-02T11:45:36.153 回答
10

当有多个接口实现并且您在这种情况下使用 @Autowired 时,弹簧绑定任何类。但是如果你想自动装配特定的实现,那么你可以使用

@Qualifier( "<implementing class name>" ) 

@Qualifier 文档

关于Spring,你必须知道的几件事是

  • 所有的 spring bean 都是托管的——它们“存在”在一个容器中,称为“应用程序上下文”。
  • 每个应用程序都有一个指向该上下文的入口点。此外,还有一个地方可以引导应用程序上下文并且所有 bean - 自动装配。在 Web 应用程序中,这可以是启动侦听器。

自动装配是通过将一个 bean 的实例放入另一个 bean 实例中的所需字段来实现的。两个类都应该是bean,即它们应该被定义为存在于应用程序上下文中。

于 2013-07-02T11:27:43.777 回答
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);
     }
    } 

类 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);
      }
    }
于 2013-07-22T09:59:03.707 回答
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=niceNiceWriter 时将被实例化为 IWriter 接口。

除了@ConditionalOnProperty 之外,还有其他选项,例如Conditional@ConditionalOnExpression

于 2020-06-09T04:52:57.533 回答
0

这取决于用例,您可以使用弹簧配置文件、自定义注释或其他答案中提到的使用 @Qualifier (按名称注入)选择接口的实现,这相当于 JSR-330 的 @Named 注释

于 2019-04-19T18:00:24.267 回答