如下是SpringBoot项目中的测试类代码:测试GridFsTemplate存储文件到MongoDB数据库中
package com.xuecheng.manage_cms; import org.bson.types.ObjectId; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.gridfs.GridFsTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @SpringBootTest @RunWith(SpringRunner.class) public class GridFSTemplateTest { /** * 这里的GridFsTemplate直接使用依赖注入 * 没有明白这里的GridFsTemplate是如何加载到容器中的? * 在启动类和配置类中都没有找到相关的代码 */ @Autowired GridFsTemplate gridFsTemplate; /** * 存文件 * @throws FileNotFoundException */ @Test public void test1() throws FileNotFoundException { File file = new File("D:/index_banner.ftl"); FileInputStream fis = new FileInputStream(file); ObjectId objectId = gridFsTemplate.store(fis, "index_banner.ftl", ""); String fileId = objectId.toString(); System.out.println(fileId); } //存文件(资料中复制的代码) @Test public void testStore() throws FileNotFoundException { //定义file File file =new File("d:/index_banner222.ftl"); //定义fileInputStream FileInputStream fileInputStream = new FileInputStream(file); ObjectId objectId = gridFsTemplate.store(fileInputStream, "index_banner222.ftl"); System.out.println(objectId); } }
疑问:疑问:GridFsTemplate是如何加载到IOC容器中的?
这里直接使用@Autowired就开始依赖注入,但是GridFsTemplate类是如何进入IOC容器中的呢?