5 ways to read configuration of springboot (1): read bean directly

1. Read the bean directly:

/**
 * 学生实体类
 * Created by ASUS on 2018/5/4
 */
public class Student {
    private String name;
    private  int age;
    public String getName() {
        return name;
}
    

    public void setName(String name) {
        this.name = name;
    }

/**
 * springboot startup class
 *
 */
 @SpringBootApplication
 public class Application

{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext= SpringApplication.run(Application.class,args);
        Student student= (Student) applicationContext.getBean("s1");
        Student student1= (Student) applicationContext.getBean("ss");System.out.println("message:"+student.toString());System.out.println("message:"+student1.toString());
}
        
                
    

    /**
      * Declare bean with name s1
      * @return
 */
 @Bean ( name = "s1" )
     public Student s1 (){         
        Student student=new Student("哈哈",12);
        return  student;
}
    

    /**
     * 声明bean,name为ss
     * @return
*/
@Bean(name = "ss")
    public Student ss(){         
        Student student=new Student("哈哈",18);
        return  student;
}
    
}

Test Results:


However, this method is not recommended, because there are many beans to be written in this way, and it is not good, just know it.

My motto: No, I can learn; I fall behind, I can catch up; I fall, I can stand up; I will do it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728760&siteId=291194637