用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();

}

}

 

相关文章:

  • 2021-11-30
  • 2021-08-25
  • 2021-11-04
  • 2021-07-23
  • 2021-06-02
猜你喜欢
  • 2021-11-08
  • 2022-12-23
  • 2021-09-19
  • 2021-06-01
  • 2021-11-03
  • 2022-01-04
  • 2021-07-07
相关资源
相似解决方案