Created
March 10, 2016 10:06
-
-
Save dantin/3c733980e0ac72f2cc4d to your computer and use it in GitHub Desktop.
Advanced wire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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