main的 启动和配置文件的读取

转载自:https://blog.csdn.net/weixin_40873368/article/details/78884690

(如侵权请留言,会删除)

1.建立简单的springboot项目
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
     //最简单直接的方式,调用SpringApplication类的静态方法run()
     SpringApplication.run(Application.class,args);
    }
}

@SpringBootApplication注解是一个组合注解,是标注此类为springboot项目的启动入口
可用@EnableAutoConfiguration和@ComponentScan代替

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
    //方式二
        SpringApplication springApplication=new SpringApplication(Application.class);
        springApplication.run(args);
    }
}

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
       //启动方式三
        ConfigurableApplicationContext context = SpringApplication.run(Application.class,args);
    }
}

注意run()方法的参数可以指定其他配置类,用@SpringBootConfiguration注解标注
比如:

//实体bean
@Component
public class User {
    public void say(){
        System.out.println("Hello spring boot");
    }
}
//配置类,管理bean
@SpringBootConfiguration
@ComponentScan
public class MyConfig {

    @Bean
    public User user(){
        return new User();
    }

}

//启动入口
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MyConfig.class,args);
        context.getBean(User.class).say();
    }
}

2.配置文件的读取
在springboot中,默认的配置文件为根目录下的application.properties,项目启动时程序会自动读取此配置文件

//默认的application.properties配置文件
my.name=wangpeicai
ip.localhost=127.0.0.1
default.port=8080
tomcat.port=${default.port}

//读取默认的application.properties配置文件
//1.方式一
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context=SpringApplication.run(Application.class,args);
        //通过getEnvironment(),getProperty()方法获取配置属性值
        System.out.println(context.getEnvironment().getProperty("my.name"));
        }

//2.方式二
/**
 * 获取配置文件的值的方式
 * 方式一:直接注入Environment对象通过getProperty方法获取配置
 * 方式二:通过@Value(${属性名})获取
 */
@SpringBootConfiguration
public class MyEnvironment{

    @Autowired//通过注解直接注入对象
    private Environment environment;

    @Value("${default.port}")
    private String defaultPort;
    @Value("${tomcat.port}")
    private String tomcatPort;
    //可以通过此方式给属性设置默认值
    @Value("${my.port:1212}")
    private String myPort;

    public void show(){
        System.out.println(environment.getProperty("ip.localhost"));
        System.out.println(defaultPort);
        System.out.println(tomcatPort);
        System.out.println(myPort);
    }
}

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context=SpringApplication.run(Application.class,args);
        context.getBean(MyEnvironment.class).show();
    }

application.properties文件可以放在config目录下,springboot也会默认去读取此目录下的application.peoperties文件,在这就不多说关于设置自定义名字的默认配置文件以及自定义的默认目录了

//读取其他的配置文件,通过@PropertySource指定文件
//JdbcConfig.properties配置文件
driveClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/springboot
username=root
password=root


@Configuration
//@PropertySource("classpath:jdbcConfig.properties")//加载一个属性文件
@PropertySources({@PropertySource("classpath:jdbcConfig.properties"),@PropertySource("file:/W:/workspace_idea/springbootreadproperty/testConfig.properties")})//加载多个属性文件,file:/ 可以指定系统目录下的属性文件
public class JdbcConfig {
    @Value("${driveClassName}")
    private String driveClassName;
    @Value("${url}")
    private String url;
    @Value("${username}")
    private String username;
    @Value("${password}")
    private String password;

    //从系统目录中读取的文件属性值
    @Value("${my.test}")
    private String myTest;

    public void showJdbcProperties(){
        System.out.println("driveClassName="+driveClassName);
        System.out.println("url="+url);
        System.out.println("username="+username);
        System.out.println("password="+password);
        System.out.println("myTest="+myTest);
    }
}

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context=SpringApplication.run(Application.class,args);
        context.getBean(JdbcConfig.class).showJdbcProperties();
    }

//通过@ConfigurationProperties(prefix = "xxx")的方式指定读取前缀名为xxx的属性值
//wpc.properties配置文件
wpc.name=wpc
wpc.age=100
wpc.sex=man


@Component
@PropertySource("classpath:wpc.properties")
@ConfigurationProperties(prefix = "wpc")//可以通过前缀名注入属性
public class WpcConfig {

    private String name;
    private int age;
    private String sex;

    public void show (){
        System.out.println("wpc.name="+name);
        System.out.println("wpc.age="+age);
        System.out.println("wpc.sex="+sex);
    }
    //可以通过get,set方法注入属性值
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

 

 

posted on 2018-04-19 20:49  凤鸣岐山  阅读(3229)  评论(0编辑  收藏  举报

导航