那么既然说出了问题,肯定就有解决方法,不然你以为我跟你玩呢。
首先这个注解是由Java提供的,它用来修饰一个非静态的void方法。它会在服务器加载Servlet的时候运行,并且只运行一次。
改造:@Component
public class SystemConstant {
public static String surroundings;
@Value("${spring.profiles.active}")
public String environment;
@PostConstruct
public void initialize() {
System.out.println("初始化环境...");
surroundings = this.environment;
}
}
结果:
我们可以看到在项目启动的时候进行了初始化



