. Spring boot bean 默认创建的bean 为singleton模式
. @Component 注解
. @Value 为属性初始化
. @Value("${}") 可以使用yml里面的配置信息
package com.example.banedemo.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
//@Scope("prototype")
//@Scope("singleton") // sping 默认创建的bean是singleton
public class User {
@Value("${com.example.banedemo.User.name}")
private String name;
@Value("123456")
private String pwd;
@Value("27")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
yml
com:
example:
banedemo:
User:
name: gali
主入口
@SpringBootApplication
public class BanedemoApplication {
public static Logger logger= LoggerFactory.getLogger(BanedemoApplication.class);
public static void main(String[] args) {
ConfigurableApplicationContext ac= SpringApplication.run(BanedemoApplication.class, args);
User c=ac.getBean(Custom.class).getUser();
String s=c.getName();
logger.info(s);
}
}