@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ShoppingCart.class})
public class ShoppingCartTest {
private ApplicationContext context = new AnnotationConfigApplicationContext(ShopConfiguration.class);
@Autowired
private ShoppingCart shoppingCart;
@Test
public void testShoppingCartCreation() {
Product battery = (Product) context.getBean("battery");
Product cdrw = (Product) context.getBean("cdrw");
Product dvdrw = (Product) context.getBean("dvder");
ShoppingCart shoppingCart1 = context.getBean(ShoppingCart.class);
shoppingCart1.addItem(battery);
shoppingCart1.addItem(cdrw);
Assert.assertEquals(false, shoppingCart.getItems().isEmpty());
}
}
有人可以解释一下为什么这段代码将为购物车创建两个具有默认范围的对象,以及它们将出现在哪个容器中以及它们之间的区别。