笔趣迷 » 其他小说 » 窗外悄然飞雪 » 2023-05-09

2023-05-09

    packagechap02;

    importorg.ntext.annotation.Bean;

    importorg.ntext.nfiguration;

    @Configuration

    publicclassAppContext{

    @Bean

    publicGreetergreeter{

    Greeterg=newGreeter;

    g.setFormat(“%s,welcome!“);

    returng;

    }

    }

    ===============================================

    publicclassGreeter{

    privateStringformat;

    publicStringgreet(Stringguest){

    returnString.format(format,guest);

    }

    publicvoidsetFormat(Stringformat){

    this.format=format;

    }

    }

    ==================================================

    importorg.ntext.annotation.AnnotationConfigApplicationContext;

    publicclassMain{

    publicstaticvoidmain(String[]args){

    AnnotationConfigApplicationContextctx=

    newAnnotationConfigApplicationContext(AppContext.class);

    Greeterg1=ctx.getBean(“greeter“,Greeter.class);

    Greeterg2=ctx.getBean(“greeter“,Greeter.class);

    System.out.println(“g1==g2)=“+(g1==g2));

    ctx.close;

    }

    }

    ===================================================

    importorg.ntext.annotation.AnnotationConfigApplicationContext;

    publicclassMain2{

    AnnotationConfigApplicationContextctx=

    newAnnotationConfigApplicationContext(AppContext.class);

    Greeterg=ctx.getBean(“greeter“,Greeter.class);

    Stringmsg=g.greet(“spring“);

    System.out.println(msg);

    ctx.close;

    ===================================================