💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
下面将 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 } } ```