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

扩展解析变量功能 #9

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
Expand Up @@ -5,6 +5,7 @@
* <p>Description: Nepxion Matrix AOP</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
*
* @author Haojun Ren
* @version 1.0
*/
Expand All @@ -17,66 +18,105 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;

public class RegistrarFactoryBean implements ApplicationContextAware, FactoryBean<Object>, InitializingBean, BeanClassLoaderAware, EnvironmentAware {
protected ApplicationContext applicationContext;
protected Class<?> interfaze;
protected MethodInterceptor interceptor;
protected Object proxy;
protected ClassLoader classLoader;
protected Environment environment;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

public ApplicationContext getApplicationContext() {
return applicationContext;
}

@Override
public Object getObject() throws Exception {
return proxy;
}

@Override
public Class<?> getObjectType() {
return interfaze;
}

@Override
public boolean isSingleton() {
return false;
}

@Override
public void afterPropertiesSet() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.addInterface(interfaze);
proxyFactory.addAdvice(interceptor);
proxyFactory.setOptimize(false);

proxy = proxyFactory.getProxy(classLoader);
resolveVariable(this.environment);
}

protected void resolveVariable(Environment environment) {
}

protected String resolve(String value) {
if (StringUtils.hasText(value)) {
return this.environment.resolvePlaceholders(value);
}
return value;
}

protected String resolveUrl(String url) {
url = resolve(url);
if (StringUtils.hasText(url) && !(url.startsWith("#{") && url.contains("}"))) {
if (!url.contains("://")) {
url = "http://" + url;
}
try {
new URL(url);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(url + " is malformed", e);
}
}
return url;
}

@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}

public Class<?> getInterfaze() {
return interfaze;
}

public void setInterfaze(Class<?> interfaze) {
this.interfaze = interfaze;
}

public MethodInterceptor getInterceptor() {
return interceptor;
}

public void setInterceptor(MethodInterceptor interceptor) {
this.interceptor = interceptor;
}

public class RegistrarFactoryBean implements ApplicationContextAware, FactoryBean<Object>, InitializingBean, BeanClassLoaderAware {
private ApplicationContext applicationContext;
private Class<?> interfaze;
private MethodInterceptor interceptor;
private Object proxy;
private ClassLoader classLoader;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

public ApplicationContext getApplicationContext() {
return applicationContext;
}

@Override
public Object getObject() throws Exception {
return proxy;
}

@Override
public Class<?> getObjectType() {
return interfaze;
}

@Override
public boolean isSingleton() {
return false;
}

@Override
public void afterPropertiesSet() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.addInterface(interfaze);
proxyFactory.addAdvice(interceptor);
proxyFactory.setOptimize(false);

proxy = proxyFactory.getProxy(classLoader);
}

@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

public Class<?> getInterfaze() {
return interfaze;
}

public void setInterfaze(Class<?> interfaze) {
this.interfaze = interfaze;
}

public MethodInterceptor getInterceptor() {
return interceptor;
}

public void setInterceptor(MethodInterceptor interceptor) {
this.interceptor = interceptor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.lang3.builder.ToStringStyle;

import com.nepxion.matrix.registrar.RegistrarFactoryBean;
import org.springframework.core.env.Environment;

public class MyRegistrarFactoryBean extends RegistrarFactoryBean {
private String name;
Expand Down Expand Up @@ -45,7 +46,13 @@ public void setDescription(String description) {
this.description = description;
}

@Override
@Override
protected void resolveVariable(Environment environment) {
resolve(label);
resolveUrl(description);
}

@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import com.nepxion.matrix.registrar.example.aop.MyAnnotation;

@MyAnnotation(name = "a", label = "b", description = "c")
@MyAnnotation(name = "a", label = "b", description = "${myAnno.c:hello}")
public interface MyService1 {
String doA(String id);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
server:
port: 9999


myAnno:
c: localhost:8080