SpringBoot中配置文件Propertise文件中配置随机值

mgk.num=${random.int} 
mgk.bignumber=${random.long}
mgk.value=${random.value}
mgk.uuid=${random.uuid}

将上面数据放在application.properties中

 

 
  
package com.mgk.demov1.annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Student {
    @Value("${mgk.uuid}")
    private String uuid;
    @Value("${mgk.value}")
    private String value;

    @Override
    public String toString() {
        return "Student{" +
                "uuid='" + uuid + '\'' +
                ", value='" + value + '\'' +
                '}';
    }
}
 

注入Student后可以看到值

SpringBoot中配置文件Propertise文件中配置随机值_第1张图片

package com.mgk.demov1;

import com.mgk.demov1.annotation.Config;
import com.mgk.demov1.annotation.SpringUtil;
import com.mgk.demov1.annotation.Student;
import com.mgk.demov1.listeners.MyListenerv2;
import com.mgk.demov1.listeners.MyListenery;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@RestController
public class Demov1Application {

    @Autowired
    private Student student;

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

    @RequestMapping("/")
    public String hello()
    {
        System.out.println(student);
        return "success";
    }
}

你可能感兴趣的:(SpringBoot中配置文件Propertise文件中配置随机值)