您的位置:

Springboot获取详解

一、获取Springboot版本信息

1、通过pom.xml文件获取版本信息

<properties>
    <java.version>1.8</java.version>
    <spring-boot.version>1.5.22.RELEASE</spring-boot.version>
</properties>

2、通过Java代码获取版本信息

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private Environment environment;

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(DemoApplication.class);
        app.run(args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Spring Boot version: " + environment.getProperty("spring-boot.version"));
    }
}

二、获取配置文件属性值

1、通过@Value注解获取单个属性值

@Service
public class MyService {
    @Value("${my.property}")
    private String myProperty;

    public String getMyProperty() {
        return myProperty;
    }
}

2、通过@ConfigurationProperties注解获取多个属性值

@ConfigurationProperties(prefix = "my")
public class MyProperties {
    private String property1;
    private String property2;

    public String getProperty1() {
        return property1;
    }

    public void setProperty1(String property1) {
        this.property1 = property1;
    }

    public String getProperty2() {
        return property2;
    }

    public void setProperty2(String property2) {
        this.property2 = property2;
    }
}

@Service
public class MyService {
    private MyProperties properties;

    @Autowired
    public MyService(MyProperties properties) {
        this.properties = properties;
    }

    public MyProperties getProperties() {
        return properties;
    }
}

三、获取Bean对象

1、通过@Autowired注解获取Bean对象

@Service
public class MyService {

    @Autowired
    private MyObject myObject;

    public MyObject getMyObject() {
        return myObject;
    }
}

2、通过@Bean注解创建Bean对象

@Configuration
public class MyConfiguration {

    @Bean
    public MyObject myObject() {
        return new MyObject();
    }
}

3、通过@ConditionalOnClass注解创建Bean对象

@Configuration
@ConditionalOnClass(MyOtherObject.class)
public class MyConfiguration {

    @Bean
    public MyObject myObject() {
        return new MyObject();
    }
}

四、获取Springboot启动器依赖

1、通过pom.xml文件获取依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>{version}</version>
</dependency>

2、通过Maven仓库搜索获取依赖信息

访问地址:https://mvnrepository.com/app/search?q=spring-boot-starter-web

3、通过Spring Initializr获取依赖信息

访问地址:https://start.spring.io/

五、获取Springboot启动时间

1、通过SpringApplicationRunListener获取启动时间

public class MyApplicationRunListener implements SpringApplicationRunListener {

    private final long startTime = System.currentTimeMillis();

    public MyApplicationRunListener(SpringApplication application, String[] args) {
    }

    @Override
    public void starting() {
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("Spring Boot started in " + (System.currentTimeMillis() - startTime) + " ms");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
    }
}

public class MyApplicationRunListenerFactory implements SpringApplicationRunListenerFactory {

    @Override
    public SpringApplicationRunListener createSpringApplicationRunListener(SpringApplication application, String[] args) {
        return new MyApplicationRunListener(application, args);
    }
}

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(DemoApplication.class);
        app.addListeners(new MyApplicationRunListenerFactory());
        app.run(args);
    }
}

2、通过@EventListener注解获取启动时间

@SpringBootApplication
public class DemoApplication {

    @EventListener(ApplicationReadyEvent.class)
    public void afterStartup() {
        Instant endTime = Instant.now();
        Instant startTime = DemoApplication.startTime;
        Duration duration = Duration.between(startTime, endTime);
        System.out.println("Spring Boot started in " + duration.toMillis() + " ms");
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}