积极加强网站建设,关键词快速排名怎么做,外国购物平台网站大全,引流网站怎么做ImportResource注解用于导入Spring的配置文件#xff0c;让配置文件里面的内容生效#xff1b;(就是以前写的springmvc.xml、applicationContext.xml) Spring Boot里面没有Spring的配置文件#xff0c;我们自己编写的配置文件#xff0c;也不能自动识别#xff1b; 想让Sp…ImportResource注解用于导入Spring的配置文件让配置文件里面的内容生效(就是以前写的springmvc.xml、applicationContext.xml) Spring Boot里面没有Spring的配置文件我们自己编写的配置文件也不能自动识别 想让Spring的配置文件生效加载进来ImportResource标注在一个配置类上。 注意这个注解是放在主入口函数的类上而不是测试类上
不使用ImportResource()注解程序根本不能对我们spring的配置文件进行加载所以我们需要将spring配置文件加载到容器里。
package com.yangzhenxu.firstspringboot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;ImportResource(locations classpath:applicationContext.xml)
SpringBootApplication
RestController
public class FirstSpringbootApplication {public static void main(String[] args) {SpringApplication.run(FirstSpringbootApplication.class, args);}}package com.yangzhenxu.firstspringboot;import com.yangzhenxu.firstspringboot.bean.Person;
import javafx.application.Application;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;SpringBootTest
class FirstSpringbootApplicationTests {AutowiredApplicationContext applicationContext;Testvoid testapplication() {Object a applicationContext.getBean(dog1);System.out.println(a);}}?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean iddog1 classcom.yangzhenxu.firstspringboot.bean.Dogproperty namename valuezhangxue/property nameage value27//bean/beans