方式一: 注解赋值
构建javaBean:com\springboot\vo\Dog
1:@Component:注册bean到spring容器中
2:添加get set toString方法
3:使用@Value(" ")注解进行属性赋值
package com.springboot.vo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component//注册bean
public class Dog {
@Value("旺财")
private String name;
@Value("3")
private Integer age;
//添加get set toString方法
}
Springboot测试类测试:
@SpringBootTest
class HellospringbootApplicationTests {
/*自动注入*/
@Autowired
private Dog dog;
@Test
void contextLoads() {
/*打印信息*/
System.out.println(dog.toString());
}
}
打印结果: