Skip to content

Commit

Permalink
add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
why committed Jun 16, 2020
1 parent 81b2ff3 commit 8128abc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion spring-framework/src/main/java/cases/AutowireByName.java
Expand Up @@ -5,13 +5,16 @@

/**
* 修改 xml 中或者 AutowiredConfig 文件中的 Primary,Service 会注入不同的 bean。
* <p>@Autowired 先匹配类型,如果有多个bean,选择优先级高的
* <p>@Resource 先匹配名称相同的 bean,如果没有则匹配类型,如果有多个类型相同的则匹配优先级高的。
*
* @author wanghuanyu
*/
public class AutowireByName {

public static void main(String[] args) {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:META-INF/cases/autowire-by-name.xml");

Service service = applicationContext.getBean("service", Service.class);
Expand Down
3 changes: 2 additions & 1 deletion spring-framework/src/main/java/cases/Service.java
@@ -1,12 +1,13 @@
package cases;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;

public class Service {

Store store;

@Resource
@Autowired
public void setStore(Store store) {
this.store = store;
}
Expand Down
Expand Up @@ -12,6 +12,6 @@
<bean class="cases.AutowiredConfig"/>
<bean id="service" class="cases.Service"/>

<bean id="remoteStore" class="cases.RemoteStore"/>
<bean id="store" class="cases.RemoteStore"/>

</beans>

0 comments on commit 8128abc

Please sign in to comment.