多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
下面将 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 } } ```