SpringBoot自动装配初步认识(6)

import com.bdqn.lyrk.springboot.study.pojo.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
        Student student = applicationContext.getBean(Student.class);
        System.out.println(student.getLoginName());
    }
}

当运行成功时:我们可以看到输出Student的loginName属性是test

6.在META-INF/spring.factories编写如下代码:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.bdqn.lyrk.springboot.study.configuration.MyTestAutoConfiguration

注意:此配置文件非常重要,当我们这个jar包被SpringBoot依赖时,spring会读取org.springframework.boot.autoconfigure.EnableAutoConfiguration所定义的配置类并加载

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/57f305e4bd576cefaf9c845f2e667cdb.html