Spring正在挑选一个接口实现,而不是自己的?

时间:2013-07-02 11:15:54

标签: java spring spring-mvc

下面的朋友是我的代码,我正在尝试使用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的实现?

5 个答案:

答案 0 :(得分:10)

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

@Qualifier( "<implementing class name>" ) 

@Qualifier documentation

你必须了解的关于Spring的一些事情

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

通过将一个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注释