第一章 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);
}
本文介绍了如何在Spring框架中使用完全注解进行开发,通过`@Configuration`和`@ComponentScan`注解替代XML配置文件,展示了如何创建无XML配置的ApplicationContext实例并获取Bean。
874

被折叠的 条评论
为什么被折叠?



