Bean configuration via XML

1. Define a squirrel 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. Use XML file to configure 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. Use @ImportResource annotation to assemble XML-defined beans

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 {
}

Guess you like

Origin www.cnblogs.com/xl4ng/p/12682800.html