替代xml配置文件
创建一个类,作为配置类,用来代替xml。但是Spring不会无缘无故认识这个类
所以需要在类上面添加@Configuration
并且在@ComponentScan中,输入需要扫描的类所在包
@Configuration
@ComponentScan(basePackages = "com.syz.spring5")
public class SpringConfig {
}
最后在测试类中,不需要加载xml文件,而是加载配置类
@Test
public void testService(){
ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
//getBean中传入Component等注解中的值,没有就默认类名的首字母小写版本
UserService userService=context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}



