1.Spring怎麼知道注入哪一個實現?
As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring’s component scan enabled, Spring framework can find out the (interface, implementation) pair. If component scan is not enabled, then you have to define the bean explicitly in your application-config.xml (or equivalent spring configuration file).
若是Spring配置了component scan,而且要注入的接口只有一個實現的話,那麼spring框架能夠自動將interface於實現組裝起來。若是沒有配置component scan,那麼你必須在application-config.xml(或等同的配置文件)定義這個bean。spring
2.須要@Qualifier和@Resource註解嗎?
Once you have more than one implementation, then you need to qualify each of them and during auto-wiring, you would need to use the @Qualifier annotation to inject the right implementation, along with @Autowired annotation. If you are using @Resource (J2EE semantics), then you should specify the bean name using the name attribute of this annotation.
一旦一個接口有多個實現,那麼就須要每一個特殊化識別而且在自動裝載過程當中使用@Qualifier和@Autowired一塊兒使用來標明。若是是使用@Resource註解,那麼你應該使用resource中屬性名稱來標註@Autowired.app