设计框架包可以作为一个工具给大家用,需要有完全不同设计思路给出来,不同于我们去做一个web服务、网站、
或者一个业务微服务,需要从原来使用视角转换成一个构建者视角。
框架或者工具,更多是框架来管理或者说调用我们,我们来遵循框架原则来实现,框架和业务实现上的核心差异。
java框架,可以依赖注解以及抽象类来实现逻辑,实现基于注解配置,通过注解简化配置方式。子类实现抽象类,
子类中的注解对象可以用过反射方式在框架中被调用,从而实现一个简单的框架。
注解类:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//注解配置信息字段.
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DeskConf {
String getComponent() default "";
String value();
}
抽象类:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//注解配置信息字段.
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DeskConf {
String getComponent() default "";
String value();
}
解析工具:
import org.apache.log4j.Logger;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* 读取注解信息
*/
public class ConfigAnnotationParse {
protected Logger logger = Logger.getLogger(ConfigAnnotationParse.class);
private ConfRegister configRegister;
public ConfigAnnotationParse(ConfRegister configCenter) {
this.configRegister = configCenter;
}
public DeskConf parseNodeAnnotation() throws Exception {
if (configRegister == null)
throw new ExceptionInInitializerError("ConfigRegister 统一配置注册中心必须被继承实现");
Class clazz = configRegister.getClass();
Map<String,String> nodeMap = new HashMap<String,String>();
DeskConf deskConf = null;
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields) {
if (field.isAnnotationPresent(DeskConf.class)) {
deskConf = field.getAnnotation(DeskConf.class);
}
}
return deskConf;
}
}
使用样例:
import org.apache.log4j.Logger;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* 读取注解信息
*/
public class ConfigAnnotationParse {
protected Logger logger = Logger.getLogger(ConfigAnnotationParse.class);
private ConfRegister configRegister;
public ConfigAnnotationParse(ConfRegister configCenter) {
this.configRegister = configCenter;
}
public DeskConf parseNodeAnnotation() throws Exception {
if (configRegister == null)
throw new ExceptionInInitializerError("ConfigRegister 统一配置注册中心必须被继承实现");
Class clazz = configRegister.getClass();
Map<String,String> nodeMap = new HashMap<String,String>();
DeskConf deskConf = null;
Field[] fields = clazz.getDeclaredFields();
for(Field field : fields) {
if (field.isAnnotationPresent(DeskConf.class)) {
deskConf = field.getAnnotation(DeskConf.class);
}
}
return deskConf;
}
}
接口:
public interface ConfService {
/**
* 获取配置信息
* @param name 项目名称.
* @param key key名称.
* @return
*/
String getConf(String name,String key);
/**
* 获得所属模块.
* @return
*/
String getComponent();
}
实现类:
public class ConfServiceImpl implements ConfService {
private ConfRegister myConfRegister;
private DeskConf deskConf;
/**
*
* @param confRegister
* @throws Exception
*/
public ConfServiceImpl(ConfRegister confRegister) throws Exception {
this.myConfRegister = confRegister;
ConfigAnnotationParse configAnnotationParse = new ConfigAnnotationParse(myConfRegister);
deskConf = configAnnotationParse.parseNodeAnnotation();
}
/**
* 获取配置项信息
* @param name 项目名称.
* @param key key名称.
* @return
*/
public String getConf(String name, String key) {
if(deskConf==null)
return null;
String component = deskConf.getComponent();
Conf conf = new Conf();
conf.setComponent(component);
conf.setName(name);
conf.setKey(key);
String value = DBConf.getValue(conf);
return value;
}
/**
* 获得所属模块.
* @return
*/
public String getComponent() {
if(deskConf==null)
return null;
String component = deskConf.value();
return component;
}
}
二、jar包配置使用方式
1、maven依赖
<dependency>
<groupId>com.jd.app.server</groupId>
<artifactId>desk-conf</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
2、配置项注入样例
/**
*
*/
public class TimeConstants extends ConfRegister{
@DeskConf("online")
private static String comp = "";
}
3、spring-config.xml 主配置文件中配置
<bean id="timeConstants" class="com.common.constant.TimeConstants"></bean>
<bean id="confService" class="com.jd.app.server.service.impl.ConfServiceImpl">
<constructor-arg index="0" ref="timeConstants"/>
</bean>
4、使用说明
@Autowired
ConfService confService;
读取监控信息
String value = confService.getConf("findgood","down");
读取模块名称
String com = confService.getComponent();
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px ".AppleSystemUIFont"; color: #040404 } span.s1 { font: 14.0px ".PingFang SC" } div.d1 { width: 474.0px; margin: 15.0px 0.0px 0.0px 15.0px }