spring configuration

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

    
    

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();
      }
}
  1. 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();
    }
}


    

4.Bean annotation, conf class

public class LookupConfigDemo {
        @Configuration
        @ComponentScan(basePackages = {"xxx.annotated"})

        public static void main(){
}
}

你可能感兴趣的:(spring configuration)