首先,实例化目录将忽略当前注入的bean@Lazy的延迟实例化
前言
上一章论述了模式注释声明组件(Bean )的应用。 在此记录Spring实例化的详细信息。
实例化通常用@Component和@Autowired声明,beanpublicclasstestcontroller { @ autowiredprivatetestintertestinter; } publicinterfacetestinter { void say hello (; } @ componentpublicclasstestoneimplementstestinter { public testone (} { system.out.println (testone init ' } ); } @Override public void sayHello () system.out.println ) ) Hellotestone ); }缺省机制表明,IOC容器在启动APP应用程序时开始实例化对象。
忽略当前注入的bean注入未实例化的对象时。 //@ componentpublicclasstestoneimplementstestinter { public testone (} { system.out.println (testone init ' ) }; } publicclasstestcontroller { @ autowiredprivatetestintertestinter; }此时程序抛出异常
@autowired(required=false )当前要注入的beanpublicclasstestcontroller ) @autowired(required=false )专用测试互连网
@Lazy延迟实例化@Lazy注释使Bean成为publicclasstestcontroller { @ auto wired @ lazyprivatetestintertestinter; @getmapping(value='/test ' ) public void test ) ) { testInter.sayHello; } @ component @ lazypublicclasstestoneimplementstestinter { public testone (} { system.out.println (testone init ' ) }; } @Override public void sayHello () system.out.println ) ) Hellotestone ); }
-end-(梦想关注咸鱼吧