@ImportResource
1、
@ImportResource(locations = {"classpath:beantest.xml"})标注到启动类上,从类路径下加载xml文件,通过Application.getbean获取xml配置对象
一开始写错了// @PropertySource(value = { "class:beantest.xml" })
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.PropertySource; @ImportResource(locations = {"classpath:beantest.xml"}) @SpringBootApplication public class Demo2Application { // @PropertySource(value = { "class:beantest.xml" }) public static void main(String[] args) { SpringApplication.run(Demo2Application.class, args); } }
bean文件
<?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="mao" class="com.example.demo.Maomao"> <property name="name" value="maomaomiaomiao"></property> </bean> </beans>