SpringBoot复习:(6)Import的用法

两个配置文件如下:

package com.example.demo.config;

import com.example.demo.domain.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig2 {
    @Bean
    public Teacher getTeacher(){
        Teacher teacher = new Teacher();
        teacher.setName("david");
        teacher.setAge(32);
        return teacher;
    }
}

package com.example.demo.config;

import com.example.demo.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@Import(MyConfig2.class)
public class MyConfig {

    @Bean("student")
    public Student getStudent(){
        Student student = new Student();
        student.setAge(31);
        student.setName("solo");
        return student;
    }
}

其中MyConfig这个配置文件类使用了@Import注解 引入了MyConfig2中的配置

你可能感兴趣的:(SpringBoot,spring,boot,java,spring)