一、创建myProperty.properties文件
my.id = 1
my.name = 'ben'
my.money = 100
二、创建类
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Component
@PropertySource(value = "myProperty.properties")
public class Account {
@Value(value = "${my.id}")
private Long id;
@Value(value = "${my.name}")
private String name;
@Value(value = "${my.money}")
private Integer money;
}
三、创建配置类
@Configuration
@ComponentScan(basePackages = {"com.wsh.pojo"})
public class SpringConfig {
}
四、JAVA程序
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
Account account = context.getBean("account", Account.class);
System.out.println(account.toString());
}
输出
Account(id=1, name='ben', money=100)