Skip to content

Instantly share code, notes, and snippets.

@dantin
Created March 10, 2016 10:06
Show Gist options
  • Save dantin/3c733980e0ac72f2cc4d to your computer and use it in GitHub Desktop.
Save dantin/3c733980e0ac72f2cc4d to your computer and use it in GitHub Desktop.
Advanced wire
@Autowired
public void setDessert(Dessert dessert) {
this.dessert = dessert;
}
===
Dessert.java接口有多个实现类
@Component
public class Cake implements Dessert { ... }
@Component
public class Cookies implements Dessert { ... }
@Component
public class IceCream implements Dessert { ... }
这时Spring会弄混解决办法
@Primary注释,可以和@Component和@Bean公用
auto-wire用法
---
@Component
@Primary
public class IceCream implements Dessert { ... }
JavaConf用法
---
@Bean
@Primary
public Dessert iceCream() {
return new IceCream();
}
XML用法
<bean id="iceCream"
class="com.desserteater.IceCream"
primary="true" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment