既然通过Spring创建对象可以使用注解来实现,那么配置文件其实也可以使用注解进行代替的
替换xml配置文件,使用完全注解开发大概分为以下两个步骤
创建Spring配置类
package com.ctguyxr.spring5.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/*** Created By Intellij IDEA** @author Xinrui Yu* @date 2021/12/9 20:58 星期四*/@Configuration@ComponentScan(basePackages = {"com.ctguyxr.spring5"})public class SpringConfig {}

修改测试类的写法
之前是通过xml文件的方式进行配置,现在更改成配置类的方式,那么测试类的写法肯定得进行修改
只是创建context的方式不同,其他context的操作一致
@Testpublic void test2(){AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean("userService", UserService.class);userService.add();System.out.println(userService);}

