💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
前面依然会用到配置文件`ApplicationContext.xml`,这个配置文件可以使用注解来代替,这样的话就实现了完全的注解开发 <br/> 开启完全注解开发的步骤如下: **1. maven坐标** ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.19</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> ``` **2. 封装配置类** ```java //使当前类具有与ApplicationContext.xml配置文件同等功能 @Configuration //包扫描,从当前包开始往下层扫描 @ComponentScan(basePackages = {"learn.springallanno"}) //开启AspectJ代理。如果需要AOP编程,则标注,否则不需要 @EnableAspectJAutoProxy(proxyTargetClass = true) public class CustomConfiguration { } ``` **3. 都用注解注册、装配Bean** ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public String getUsername() { return userDao.getUsername(); } } ``` **4. 测试,调用方法成功** ```java public class UserTest { /** 获取IoC容器 */ private final ApplicationContext context = new AnnotationConfigApplicationContext(CustomConfiguration.class); @Test public void testGetUsername() { //获取IoC容器的注册的Bean UserService userService = context.getBean(UserServiceImpl.class); System.out.println(userService.getUsername()); ///[getUsername] } } ``` **** 案例代码:https://gitee.com/flymini/codes03/tree/master/learn-springallanno