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;
===================================================
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;
===================================================
转码声明:以上内容基于搜索引擎转码技术对网站内容进行转码阅读,自身不保存任何数据,请您支持正版