name | Bean xml | Bean annotation |
---|---|---|
conf xml | bean not change, xml define bean | conf xml scan |
conf class | bean not change, conf class define bean | conf class scan |
1.Bean xml, conf xml
//Bean
public class Singer{
public void sing(){
//do something
}
//conf xml
<beans ...>
<bean id="Singer" class="xx.Singer"/>
</bean>
</beans>
2.Bean xml, conf class
//Bean
public class Singer{
public void sing(){
//do something
}
//conf class
@Configuration
public class SingerConfiguration {
@Bean
public Singer singer() {
return new Singer();
}
}
- Bean annotation, conf xml
@Component("singer)
@Scope("prototype")
public class Singer {
private String lyric = "I played a quick game of chess
with the salt and pepper shaker";
public void sing() {
// commented to avoid console pollution
//System.out.println(lyric);
} }
@Component("abstractLookupBean")
public class AbstractLookupDemoBean implements DemoBean {
@Lookup("singer")
public Singer getMySinger() {
return null; // overriden dynamically
}
@Override
public void doSomething() {
getMySinger().sing();
}
}
<beans ...>
<context:component-scan
base-package="xxx.annotated"/>
</beans>
4.Bean annotation, conf class
public class LookupConfigDemo {
@Configuration
@ComponentScan(basePackages = {"xxx.annotated"})
public static void main(){
}
}