首页 > 代码库 > @Scope("prototype")创建的对象为什么会一样?
@Scope("prototype")创建的对象为什么会一样?
用Annotation的@Scope("prototype")实验两个对象还是相等的,用xml的scope="prototype"就不一样,代码如下:
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.dao.UserDAO;
import com.model.User;
@Scope("prototype")
@Component("u")
public class UserDAOImpl implements UserDAO {
@Override
public void save(User u) {
System.out.println("a user saved");
}
}
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//destroy在ApplicationContext没有要用具体的类来引用
UserService service = (UserService)ctx.getBean("userService");
UserService service2 = (UserService)ctx.getBean("userService");
System.out.println(service==service2);
service.add(new User());
ctx.destroy();
}
}
@Scope("prototype")创建的对象为什么会一样?