创建配置类
package com.xxx.spring.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration//作为配置类,替换xml配置文件,不使用xml方式进行包扫描 @ComponentScan(basePackages = {"com.xxx.spring"}) public class MyConfig { }
测试代码
@Test public void testAnno(){ //加载配置类 ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); UserService userService = context.getBean("userService", UserService.class); userService.add(); }
输出:
UserDaoImpl2...
service add...
标签:xml,配置文件,配置,context,userService,替代,class From: https://www.cnblogs.com/ixtao/p/17107181.html