通过IOC容器创建对象,并为属性赋值 是通过 set 方法注入的,所以实体类必须有set方法 12345678910public class TestIOC { @Test public void test() { //1.获取IOC容器本身这个对象 ApplicationContext ioc = n
根据bean的类型从IOC容器中获取bean的实例1 2 3 4 5 6 7 8 9 10 public class TestIOC { @Test public void test() { //1.获取IOC容器本身这个对象 ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从IOC容器中获取bean对象 Object bean = ioc.getBean("book"); System.out.println(bean); } }
public class TestIOC { @Test public void test() { //1.获取IOC容器本身这个对象 ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从IOC容器中获取bean对象 Book bean = ioc.getBean(Book.class); System.out.println(bean.getBookName()+"---"+bean.getAuthor()+"---"+bean.getPrice()); } }