Skip to content

Commit

Permalink
spring 测试binder和environment
Browse files Browse the repository at this point in the history
  • Loading branch information
qinfuxiang committed Nov 22, 2021
1 parent 4b23521 commit 82ba9cf
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fast.boot.autoconfigure.bind;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "binder.test")
public class BinderTest {
private String bname;

private Integer bage;

private BinderInnerTest binderInnerTest;


public String getBname() {
return bname;
}

public void setBname(String bname) {
this.bname = bname;
}

public Integer getBage() {
return bage;
}

public void setBage(Integer bage) {
this.bage = bage;
}

public BinderInnerTest getBinderInnerTest() {
return binderInnerTest;
}

public void setBinderInnerTest(BinderInnerTest binderInnerTest) {
this.binderInnerTest = binderInnerTest;
}

public static class BinderInnerTest{

private String innerName;

private Integer innerage;

public String getInnerName() {
return innerName;
}

public void setInnerName(String innerName) {
this.innerName = innerName;
}

public Integer getInnerage() {
return innerage;
}

public void setInnerage(Integer innerage) {
this.innerage = innerage;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fast.boot.autoconfigure.bootstrap;

import fast.boot.autoconfigure.bind.BinderTest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
* @author qinfuxiang
*/
public class BinderBootstrap {

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(EnableHelloWorldBootstrap.class, args);
ConfigurableEnvironment environment = context.getBean(ConfigurableEnvironment.class);
//配置properties 信息
BindResult<Properties> propertiesBindResult = Binder.get(environment)
.bind("hello-world.properties", Properties.class);
BindResult<Map> mapBindResult = Binder.get(environment)
.bind("hello-world.properties", Map.class);
BindResult<BinderTest> binderTestBindResult = Binder.get(environment).bind("binder.test", Bindable.of(BinderTest.class));
BindResult<List<String>> resultList = Binder.get(environment).bind("binder.test2.list", Bindable.listOf(String.class));
System.out.println();
}

}
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
user.name=hello
binder.test.bname=bindname
binder.test.bage=18
binder.test.binderInnerTest.innerName=bindInnername
binder.test.binderInnerTest.innerage=28

binder.test2.list[0]=valuea
binder.test2.list[1]=valueb
binder.test2.list[2]=valuec
binder.test2.list[3]=valued
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
helloWorld:
properties:
spring:
json:
trusted:
packages: '*'
type:
mapping: fast.cloud.nacos.fastbootkafka.domain.TopicATest:fast.cloud.nacos.fastbootkafka.domain.TopicATest


0 comments on commit 82ba9cf

Please sign in to comment.