1 // ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); 2 ApplicationContext app = new FileSystemXmlApplicationContext("D:\\Workspace-java\\idea\\Workspaces\\itheima-javaweb\\SSM1_spring_ioc\\src\\main\\resources\\applicationContext.xml"); 3 UserService userService = (UserService)app.getBean("userService"); 4 userService.save();
1 ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); 2 // ApplicationContext app = new FileSystemXmlApplicationContext("D:\\Workspace-java\\idea\\Workspaces\\itheima-javaweb\\SSM1_spring_ioc\\src\\main\\resources\\applicationContext.xml"); 3 4 // Object userService = app.getBean("userService"); 5 // UserService userService = (UserService)app.getBean("userService"); 6 7 UserService userService = app.getBean(UserService.class); 8 //仅适用于配置文件中只有一个UserServiceImpl时,多个bean虽然id不同,但class一致时,这种方式会报错 9 10 userService.save();