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

use*_*968 10 java spring spring-mvc

下面的朋友是我的代码,我试图用Spring运行依赖注入

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

一个bean.xml和一个main方法类.

接口IWriter.java

package DI;
    public interface IWriter {
    public void writer(String s);
}  
Run Code Online (Sandbox Code Playgroud)

类Writer.java

     package DI;

     import org.springframework.stereotype.Service;

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

类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);
    }
} 
Run Code Online (Sandbox Code Playgroud)

另一堂课

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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> 
Run Code Online (Sandbox Code Playgroud)

当我运行代码时Spring容器给出了Writer.java类方法的输出.我没有指定选择哪个实现.Spring如何获得Writer.java的实现?

Ash*_*wal 10

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

@Qualifier( "<implementing class name>" ) 
Run Code Online (Sandbox Code Playgroud)

@Qualifier文档

关于Spring你必须了解的几件事情

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

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


Cha*_*oon 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);
     }
    } 
Run Code Online (Sandbox Code Playgroud)

类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);
   }
  } 
Run Code Online (Sandbox Code Playgroud)

另一堂课

     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);
      }
    }
Run Code Online (Sandbox Code Playgroud)

  • 提交前格式化代码以获得更好的可读性和理解 (4认同)