首页 > 其他 > 详细

@Scope("prototype")创建的对象为什么会一样?

时间:2014-11-01 17:36:07      阅读:214      评论:0      收藏:0      [点我收藏+]

用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")创建的对象为什么会一样?

原文:http://www.cnblogs.com/chuansao/p/4067381.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!