ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
下面将 AccountServiceImpl 类注入到 Spring 的配置文件`ApplicationContext.xml`中。 <br/> 步骤如下: **1. `AccountServiceImpl `类** ```java public class AccountServiceImpl { public void print() { System.out.println("AccountServiceImpl"); } } ``` **2. 将`AccountServiceImpl`注册到IoC容器中** *`resources/ApplicationContext.xml`* ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="accountServiceImpl" class="com.learn.boot03.service.impl.AccountServiceImpl"/> </beans> ``` **3. 在启动类上标注注解`@ImportResource`引入Spring配置文件** ```java @SpringBootApplication @ImportResource({"classpath:ApplicationContext.xml"}) public class Boot03Application { public static void main(String[] args) { SpringApplication.run(Boot03Application.class, args); } } ``` **4. 测试** ```java @SpringBootTest class Boot03ApplicationTests { @Autowired private ApplicationContext applicationContext = null; @Test void contextLoads() { //从IoC容器中获取AccountServiceImpl对象 AccountServiceImpl impl = (AccountServiceImpl) applicationContext.getBean("accountServiceImpl"); impl.print(); //AccountServiceImpl } } ```