在application.properties中设置:
spring.profiles.active=dev
使用@Profile(“dev”)注解来标记只在dev profile激活时才生效的代码。
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("dev")
public class DevOnlyBean {
// 这里是只在开发环境生效的代码
}
在上面的例子中,DevOnlyBean类只会在dev profile激活时,也就是开发环境中,才会被Spring容器管理。如果你想要在不同的环境运行不同的代码,你可以通过设置不同的spring.profiles.active来切换环境,Spring Boot会根据当前激活的profile来实例化相应的beans。
5802

被折叠的 条评论
为什么被折叠?



