Bean的四种实例化方式(也可以说是三种)
bean的实例化方式:
①.构造器实例化(无参数构造器,与构造器的访问权限无关),最标准,使用最多。
②.静态工厂方法实例化(了解)
③.实例工厂方法实例化(了解)
④.实现FactoryBean接口实例化:实例工厂变种:集成其他框架使用:SqlSessionFactoryBean
1、构造器实例化
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("App.xml") //不写名字默认为App-context.xml public class App { @Autowired private ApplicationContext context; @Test public void testOld(){ Teacher teacher1 = new Teacher(); Teacher teacher2 = new Teacher(); System.out.println(teacher1); System.out.println(teacher2); } @Test public void testSpring(){ //用来检验scope是singleton或prototype的不同 Teacher teacher1 = context.getBean(Teacher.class); Teacher teacher2 = context.getBean(Teacher.class); System.out.println(teacher1); System.out.println(teacher2); } }