创建自己的标签 + 通过创建的标签来验证 + invoke进行夸程序调用
//主逻辑-------------------------------------------------------------
public void createHtml() {
if (this.jobLockService.getRunPrivilege(JobLockEntity.JOB_jobId_createHtml)) {
// 定义佳兆业生成协议业务调用
//描述:proActiveAware.achieved()
//1.应用上下文中是否有当前class的 Bean
//2.校验当前传参的类型 是否有 @SingleProActive的标签
if (this.proActiveAware.achieved(ProductAgreement.class)) {
//proActiveAware.invoke()方法
//描述: 1.extract() 获取到 唯一的 ProductAgreement类
//2.内部类Result装配当前类的相关信息
//3.将当前装配类返回
this.proActiveAware.invoke(new Execution<ProductAgreement, Void>() {
@Override
public Void execute(ProductAgreement arg0) {
//成功调用ext版本的方法()
return arg0.createHtmlLog();
}
}, ProductAgreement.class);
} else {
//验证失败 调用基线 的 方法
this.createHtmlLog();
}
}
}
//主逻辑-------------------------------------------------------------
//----------------------------------------------ProActiveAware -------------------------------------
package com.guohuai.basic.component.proactive;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class ProActiveAware {
@Autowired
private ApplicationContext context;
public <T extends ProActive> boolean achieved(Class<T> clazz) {
final Map<String, T> map = this.context.getBeansOfType(clazz);
if (map.size() <= 0) {
return false;
}
if (clazz.isAnnotationPresent(SingleProActive.class)) {
if (map.size() > 1) {
Class<?>[] impls = new Class<?>[map.size()];
int i = 0;
for (Map.Entry<String, T> entry : map.entrySet()) {
impls[i] = entry.getValue().getClass();
i++;
}
throw new MultiImplProActiveException(clazz, impls);
}
}
return true;
}
public <T extends ProActive> Map<String, T> extract(Class<T> clazz) {
final Map<String, T> map = this.context.getBeansOfType(clazz);
if (clazz.isAnnotationPresent(SingleProActive.class)) {
if (map.size() > 1) {
Class<?>[] impls = new Class<?>[map.size()];
int i = 0;
for (Map.Entry<String, T> entry : map.entrySet()) {
impls[i] = entry.getValue().getClass();
i++;
}
throw new MultiImplProActiveException(clazz, impls);
} else {
return map;
}
}
TreeMap<String, T> tmap = new TreeMap<String, T>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
T o1t = map.get(o1);
T o2t = map.get(o2);
int x = o1t.priority().compareTo(o2t.priority());
return x == 0 ? o1.compareTo(o2) : x;
}
});
if (null != map && map.size() > 0) {
for (Map.Entry<String, T> entry : map.entrySet()) {
tmap.put(entry.getKey(), entry.getValue());
}
}
return tmap;
}
public <T extends ProActive, R extends Object> List<ProActive.Result<R>> invoke(ProActive.Execution<T, R> execution, Class<T> clazz) {
Map<String, T> proactives = this.extract(clazz);
List<ProActive.Result<R>> result = new ArrayList<ProActive.Result<R>>();
if (null != proactives && proactives.size() > 0) {
for (Entry<String, T> entry : proactives.entrySet()) {
ProActive.Result<R> er = new ProActive.Result<R>();
er.setName(entry.getKey());
er.setClazz(entry.getValue().getClass());
R r = execution.execute(entry.getValue());
er.setResult(r);
result.add(er);
}
}
return result;
}
}
//-----------------------------------------用于跨系统调用的类-----------------------------------------
@SingleProActive
public interface ProductAgreement extends ProActive {
/**
* 佳兆业生成html
* @return
*/
public Void createHtmlLog();
}
//-----------------------------------------用于跨系统调用的类-----------------------------------------
//+ 标签创建----------------------------------------------------------
package com.guohuai.basic.component.proactive;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface SingleProActive {
}
//+ 标签创建----------------------------------------------------------
//----------------------------用于继承 逻辑泛型类---------------------
package com.guohuai.basic.component.proactive;
import lombok.Data;
public abstract interface ProActive {
public abstract Integer priority();
public static abstract interface Execution<T extends ProActive, R extends Object> {
public abstract R execute(T t);
}
@Data
public static class Result<R> {
private String name;
private Class<?> clazz;
private R result;
}
}
//-----------------------------------------------------------------------------