好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring boot环境抽象的实现方法

在实际开发中,开发人员在编写 spring boot 的时候通常要在本地环境测试然后再部署到production环境,这两种环境一般来讲是不同的,最主要的区别就是数据源的不同。

在应用环境中,集成在容器的抽象环境模型有两个方面:profiles和properties。只有给出的profile被激活,一组逻辑命名的bean定义才会在容器中注册。

环境变量对象角色和profiles的关系来决定哪个profiles(如果有)处于当前激活状态,哪个profiles默认被激活。

@profile

基于java类的环境配置

@profile注解可以用来标注@configuration注解的类。表示该特定环境下激活该类下的所有bean。当然也可以专门用来标注@bean,因为许多时候本地环境和production环境的区别只是数据源不同罢了。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@configuration

public class profileconf {

 

 

   @bean

   @profile ( "dev" )

   public userinfo devuserinfo() {

     userinfo userinfo = new userinfo();

     userinfo.setid( 1 );

     userinfo.setname( "dev" );

     return userinfo;

   }

 

   @bean

   @profile ( "production" )

   public userinfo productionuserinfo() {

     userinfo userinfo = new userinfo();

     userinfo.setid( 1 );

     userinfo.setname( "production" );

     return userinfo;

   }

}

激活profile

现在我们已经更新了我们的配置,我们仍然需要说明哪个profile是激活的。如果直接注册@configuration标注的类,这将会看到一个nosuchbeandefinitionexception被抛出,因为容器找不到一个对应的环境下的bean。

?

1

2

3

4

5

6

7

public static void main(string[] args) {

   annotationconfigapplicationcontext context = new annotationconfigapplicationcontext();

   context.getenvironment().setactiveprofiles( "dev" );

   context.register(userconf. class );

   context.refresh();

   system.out.println(context.getbean(userinfo. class ));

}

默认的profile

默认配置文件表示默认启用的配置文件。

?

1

2

3

4

5

6

7

8

@bean

@profile ( "default" )

public userinfo defaultuserinfo() {

   userinfo userinfo = new userinfo();

   userinfo.setid( 1 );

   userinfo.setname( "default" );

   return userinfo;

}

如果没有profile是激活状态,上面的bean将会被创建;这种方式可以被看做是对一个或者多个bean提供了一种默认的定义方式。如果启用任何的profile,那么默认的profile都不会被应用。

属性源抽象

spring 环境抽象提供了可配置的属性源层次结构的搜索操作。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

   public static void main(string[] args) {

     annotationconfigapplicationcontext context = new annotationconfigapplicationcontext();

//    context.getenvironment().setactiveprofiles("dev");

     context.getenvironment().setactiveprofiles( "dev" );

     context.register(profileconf. class );

     context.refresh();

     configurableenvironment environment = context.getenvironment();

     map<string, object> maps = environment.getsystemproperties();

     maps.keyset().foreach(k -> system.out.println(k + "->" + maps.get(k)));

 

     system.out.println( "===========================" );

     map<string, object> environment1 = environment.getsystemenvironment();

     environment1.keyset().foreach(k -> system.out.println(k + "->" + environment1.get(k)));

 

     system.out.println(environment.containsproperty( "java.vm.version" ));

   }

在上面的例子中可以获取environment的两个系统变量以及环境变量。

一个propertysource是对任何key-value资源的简单抽象,并且spring 的标准环境是由两个propertysource配置的,一个表示一系列的jvm 系统属性(system.getproperties()),一个表示一系列的系统环境变量(system.getenv())。

具体的说,当使用standardenvironment时,如果在运行时系统属性或者环境变量中包括foo,那么调用env.containsproperty([java.vm.version])方法将会返回true。

更重要的是,整个机制都是可配置的。也许你有个自定义的属性来源,你想把它集成到这个搜索里面。这也没问题,只需简单的实现和实例化自己的propertysource,并把它添加到当前环境的propertysources集合中:

?

1

2

3

configurableapplicationcontext ctx = new genericapplicationcontext();

mutablepropertysources sources = ctx.getenvironment().getpropertysources();

sources.addfirst( new mypropertysource());

@propertysource

上一篇文章 讲到,基于java的配置很多时候会和xml混合使用。其中@import还可以导入其他java配置类,这里要说的@propertysource注解表示导入.properties文件。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@configuration

@propertysource ( "classpath:user.properties" )

public class userconf {

 

   @autowired

   environment environment;

 

   @bean

   //每次调用就创建一个新的bean

   @scope ( "prototype" )

   public userinfo userinfo() {

     userinfo userinfo = new userinfo();

     userinfo.setid(integer.valueof(environment.getproperty( "user.id" )));

     system.out.println(environment.getproperty( "user.name" ));

     userinfo.setname(environment.getproperty( "user.name" ));

     return userinfo;

   }

}

?

1

2

user.id= 11

user.name=asdasd

任何出现在@propertysource中的资源位置占位符都会被注册在环境变量中的资源解析。

假设]user.name]已经在其中的一个资源中被注册,例如:系统属性或环境变量,占位符将会被正确的值解析。

如果没有,]default/path]将会使用默认值。如果没有默认值,而且无法解释属性,则抛出illegalargumentexception异常。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

查看更多关于spring boot环境抽象的实现方法的详细内容...

  阅读:19次