标签:
使用:
当你引用framework-core时,需要在加载spring配置文件classpath*:spring/applicationContext*.xml,可以在web.xml中这样配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 当你如上面这样做了时,项目下的classpath*:*.properties文件都会加载到spring里面,在spring配置文件中可以尽情使用(但是spring自定义标签的项目不可以,如dubbo,dubbo默认加载dubbo.properties文件),然后会把所有的配置放在内存cn.bidlink.nbl.framework.core.utils.web.PropertyUtil中,可使用PropertyUtil.get("key")获得value。也可使用spring自带注解@Value完成注入,#{p[‘‘]}为固定形式。方式如下
public class PropertyUtilTest {
@Value("#{p[‘redis.host‘]}")
private String redisHost;
@Test
public void testPropertyAnnotation(){
Assert.assertEquals(redisHost, "10.4.0.122:6380,10.4.0.123:6379");
}
@Test
public void testPropertyUtil(){
String redisHost = PropertyUtil.get("redis.host");
Assert.assertEquals(redisHost, "10.4.0.122:6380,10.4.0.123:6379");
}
}
添加dubbo.properties配置文件
dubbo_register_address=zookeeper://10.1.1.62:2181?backup=10.1.1.63:2182,10.1.1.64:2183,10.1.1.65:2184,10.1.1.60:2185,10.1.1.61:2186 dubbo_application_name=nbl-framework dubbo_scan_package=cn.bidlink.nblproducer端,实现上添加@DubboService:
public interface TestDubboService {
public int getNum();
}
@DubboService
public class TestDubboServiceImpl implements TestDubboService {
@Override
public int getNum() {
return 1;
}
} consumer端:
public class DubboTest {
private TestDubboService testDubboService;
@Autowired
public void setTestDubboService(TestDubboService testDubboService) {
this.testDubboService = testDubboService;
}
@Test
public void testReferenceServiceFilter(){
Assert.assertEquals(testDubboService.getNum(), 1);
}
}
标签:
原文地址:http://my.oschina.net/sourcecoding/blog/521019