Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fst,kroy序列化注解扫描,注册 #155

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alibaba.dubbo.common.serialize.support.annotation;

import java.lang.annotation.*;

/**
* <p>Created with IntelliJ IDEA. </p>
* <p>User: Stony </p>
* <p>Date: 2016/7/1 </p>
* <p>Time: 11:35 </p>
* <p>Version: 1.0 </p>
* 序列化优化注解扫描,注册到
* @see com.alibaba.dubbo.common.serialize.support.SerializableClassRegistry
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Optimizer {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.alibaba.dubbo.config.spring.processor;

import com.alibaba.dubbo.common.serialize.support.SerializableClassRegistry;
import com.alibaba.dubbo.config.spring.scan.OptimizerScanner;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
* <p>Created with IntelliJ IDEA. </p>
* <p>User: Stony </p>
* <p>Date: 2016/7/1 </p>
* <p>Time: 11:31 </p>
* <p>Version: 1.0 </p>
* <h3>序列化XML优化配置</h3>
* <pre class="code">
* &lt;bean id="optimizerAnnotationScan" class="com.alibaba.dubbo.config.spring.processor.OptimizerAnnotationRegistryPostProcessor" &gt;
* &lt;property name="basePackage" value="com.stone.beans" /&gt;
* &lt;/bean&gt;
* </pre>
*/
public class OptimizerAnnotationRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor,InitializingBean,Ordered {

private String basePackage;
public static final String OPTIMIZER_BEAN_LIST = "G_OPTIMIZER_BEAN_LIST";
private List<Class> classes;

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
OptimizerScanner optimizerScanner = new OptimizerScanner(registry);
try {
classes = optimizerScanner.scanBeanClass(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(null != classes){
for (Class c : classes) {
SerializableClassRegistry.registerClass(c);
}
}
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
if(classes != null) {
beanFactory.registerSingleton(OPTIMIZER_BEAN_LIST, classes);
}else{
beanFactory.registerSingleton(OPTIMIZER_BEAN_LIST, Collections.EMPTY_LIST);
}
}

public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}

public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 10;
}

@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.basePackage, "property basePackage is required");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.alibaba.dubbo.config.spring.scan;

import com.alibaba.dubbo.common.serialize.support.annotation.Optimizer;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
* <p>Created with IntelliJ IDEA. </p>
* <p>User: Stony </p>
* <p>Date: 2016/7/1 </p>
* <p>Time: 14:09 </p>
* <p>Version: 1.0 </p>
* <h3>Optimizer注解扫描</h3>
*/
public class OptimizerScanner extends ClassPathBeanDefinitionScanner {
private volatile boolean isInit = false;
private final Object monitor = new Object();

/**
* 不加载默认的过滤器
* @param registry bean 注册接口
*/
public OptimizerScanner(BeanDefinitionRegistry registry) {
this(registry, false);
}

/**
*
* @param registry bean 注册接口
* @param b 是否默认过滤器加载
*/
public OptimizerScanner(BeanDefinitionRegistry registry, boolean b) {
super(registry, b);
init();
}

public void init(){
if(isInit){
return;
}
setIncludeAnnotationConfig(true);
addIncludeFilter(new AnnotationTypeFilter(Optimizer.class));
addExcludeFilter(new TypeFilter() {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
String className = metadataReader.getClassMetadata().getClassName();
return className.endsWith("package-info");
}
});
synchronized (this.monitor) {
this.isInit = true;
}
}

/**
* 返回 Optimizer 注解类Class
* @param basePackages
* @return
* @throws ClassNotFoundException
*/
public List<Class> scanBeanClass(String... basePackages) throws ClassNotFoundException {
Assert.notEmpty(basePackages, "At least one base package must be specified");
ClassLoader beanClassLoader = OptimizerScanner.class.getClassLoader();
List<Class> beanClass = new LinkedList<Class>();
for (String basePackage : basePackages) {
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
for (BeanDefinition candidate : candidates) {
beanClass.add(resolveClassName(candidate.getBeanClassName(), beanClassLoader));
}
}
return beanClass;
}

protected Class<?> resolveClassName(String className, ClassLoader beanClassLoader) throws ClassNotFoundException {
return ClassUtils.forName(className, beanClassLoader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void init() {
registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true));
registerBeanDefinitionParser("optimizer", new OptimizerAnnotationBeanDefinitionParser());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.alibaba.dubbo.config.spring.schema;


import com.alibaba.dubbo.common.serialize.support.SerializableClassRegistry;
import com.alibaba.dubbo.config.spring.scan.OptimizerScanner;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

import java.util.LinkedList;
import java.util.List;

/**
* <p>Created with IntelliJ IDEA. </p>
* <p>User: Stony </p>
* <p>Date: 2016/7/1 </p>
* <p>Time: 14:05 </p>
* <p>Version: 1.0 </p>
* <p>将Optimizer 注解类Class注册到SerializableClassRegistry中</p>
*/
public class OptimizerAnnotationBeanDefinitionParser implements BeanDefinitionParser {

public static final String OPTIMIZER_BEAN_LIST = "G_OPTIMIZER_BEAN_LIST";
private List<Class> classes;

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
XmlReaderContext readerContext = parserContext.getReaderContext();
OptimizerScanner optimizerScanner = new OptimizerScanner(readerContext.getRegistry());

try {
String basePackage = element.getAttribute("base-package");
classes = optimizerScanner.scanBeanClass(StringUtils.tokenizeToStringArray(basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

if(null != classes){
for (Class c : classes) {
SerializableClassRegistry.registerClass(c);
}
}

RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(LinkedList.class);
beanDefinition.setLazyInit(false);
if (classes != null) {
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, classes);
}
parserContext.getRegistry().registerBeanDefinition(OPTIMIZER_BEAN_LIST, beanDefinition);
return beanDefinition;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1178,5 +1178,14 @@
<xsd:documentation><![CDATA[ The service url parameter ]]></xsd:documentation>
</xsd:annotation>
</xsd:element>


<xsd:element name="optimizer">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="base-package" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.dubbo.config.spring.facade;


import com.alibaba.dubbo.common.serialize.support.annotation.Optimizer;

/**
* <p>Created with IntelliJ IDEA. </p>
* <p>User: Stony </p>
* <p>Date: 2016/7/4 </p>
* <p>Time: 10:19 </p>
* <p>Version: 1.0 </p>
*/
@Optimizer
public class TOrder {
int id;
String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "TOrder{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.alibaba.dubbo.config.spring.facade;


import com.alibaba.dubbo.common.serialize.support.annotation.Optimizer;

/**
* <p>Created with IntelliJ IDEA. </p>
* <p>User: Stony </p>
* <p>Date: 2016/7/4 </p>
* <p>Time: 10:19 </p>
* <p>Version: 1.0 </p>
*/
@Optimizer
public class TUser {
int id;
String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "TUser{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.alibaba.dubbo.config.spring.facade2;

import com.alibaba.dubbo.common.serialize.support.annotation.Optimizer;

/**
* <p>Created with IntelliJ IDEA. </p>
* <p>User: Stony </p>
* <p>Date: 2016/7/5 </p>
* <p>Time: 12:00 </p>
* <p>Version: 1.0 </p>
*/
@Optimizer
public class TAccount {
int id;
String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "TAccount{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Loading