4.1节当中我将Scope改成了prototype,为啥返回的还是同一对象?
package com.imooc.test.beanannotation; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.beanannotation.BeanAnnotation; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class) public class TestBeanAnnotation extends UnitTestBase { public TestBeanAnnotation() { super("classpath*:spring-beanannotation.xml"); } @Test public void testScpoe() { BeanAnnotation bean = super.getBean("beanAnnotation"); bean.myHashCode(); bean = super.getBean("beanAnnotation"); bean.myHashCode(); } }
package com.imooc.beanannotation; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; //@Component("bean") @Scope("prototype") @Component public class BeanAnnotation { public void myHashCode() { System.out.println("BeanAnnotation : " + this.hashCode()); } }
两个bean输出的hashcode是一样的
BeanAnnotation : 2039473569 BeanAnnotation : 2039473569