第一章 Spring完全注解开发【0配置】
1.1 完全注解开发步骤
- 创建配置类
- 在class上面添加注解
- @Configuration:标识当前类是一个配置类,作用:代替XML配置文件
- @ComponentScan:设置组件扫描当前包及其子包
- 使用AnnotationConfigApplicationContext容器对象
1.2 示例代码
/**
* @author Chunsheng Zhang 尚硅谷
* @create 2022/3/28 14:05
*/
@Configuration
@ComponentScan(basePackages = "com.atguigu")
public class SpringConfig {
}
@Test
public void test0Xml(){
//创建容器对象
// ApplicationContext context =
// new ClassPathXmlApplicationContext("applicationContext.xml");
//使用AnnotationConfigApplicationContext容器对象
ApplicationContext context =
new AnnotationConfigApplicationContext(SpringConfig.class);
DeptDaoImpl deptDao = context.getBean("deptDao", DeptDaoImpl.class);
System.out.println("deptDao = " + deptDao);
}