@Component
public class DiamondDemo {
/**
* 这里null的数据,是通过application.properties配置注入的
*/
private String stringValue;
private int intValue;
private boolean booleanValue;
public String getStringValue() {
return stringValue;
}
public int getIntValue() {
return intValue;
}
public boolean getBooleanValue() {
return booleanValue;
}
}
@DiamondListener(dataId = "com.taobao.middleware:configFromListener.properties")
public class DiamondDataCallbackDemo implements DiamondDataCallback {
// @Autowired
// private ConfigBean configBean;
@Autowired
private Environment environment;
private String dataCahe;
public String getReceivedData() {
return dataCahe;
}
@Override
public void received(String data) {
try {
dataCahe = data;
Properties properties = new Properties();
properties.load(new ByteArrayInputStream(data.getBytes()));
System.out.println("received from diamond listener: " + properties);
// 把properties的值注入到ConfigBean里
// new org.springframework.boot.bind.RelaxedDataBinder(configBean)
// .bind(new org.springframework.beans.MutablePropertyValues(properties));
} catch (Exception e) {
e.printStackTrace();
}
}
}
@RunWith(PandoraBootRunner.class)
@DelegateTo(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { Application.class })
public class DiamondTest {
@Autowired
DiamondDataCallbackDemo callback;
@Autowired
private DiamondDemo diamondDemo;
@Test
public void testGet() {
TestCase.assertNotNull(diamondDemo.getStringValue());
}
@Test
public void testListener() {
// 增加listener后,需要等一会儿才能收到配置信息
// TestCase.assertNotNull(callback.getReceivedData());
}
}