通过XML配置Bean

1.定义一个松鼠POJO

package com.springboot.other.pojo;

import com.springboot.chapter3.pojo.definition.Animal;

public class Squirrel implements Animal {
    @Override
    public void use() {
        System.out.println("松鼠可以采集松果");
    }
}

2.使用XML文件配置POJO

<?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="squirrel" class="com.springboot.other.pojo.Squirrel"/>
</beans>

3.使用@ImportResource注解装配XML定义的Bean

package com.springboot.chapter3.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ComponentScan("com.springboot.chapter3.*")
@ImportResource(value = {"classpath:spring-other.xml"})
public class AppConfig {
}

猜你喜欢

转载自www.cnblogs.com/xl4ng/p/12682800.html