Skip to content

Commit

Permalink
Merge pull request #296 from lets-blade/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hellokaton authored Nov 26, 2018
2 parents e6096d5 + 130b89a commit 6757d03
Show file tree
Hide file tree
Showing 36 changed files with 709 additions and 215 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ Run with `Maven`:
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.12.ALPHA</version>
<version>2.0.12.BETA</version>
</dependency>
```

or `Gradle`:

```sh
compile 'com.bladejava:blade-mvc:2.0.12.ALPHA'
compile 'com.bladejava:blade-mvc:2.0.12.BETA'
```

Write the `main` method and the `Hello World`:
Expand Down
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.12.ALPHA</version>
<version>2.0.12.BETA</version>
</dependency>
```

Expand All @@ -70,7 +70,7 @@
或者 `Gradle`:

```sh
compile 'com.bladejava:blade-mvc:2.0.12.ALPHA'
compile 'com.bladejava:blade-mvc:2.0.12.BETA'
```

编写 `main` 函数写一个 `Hello World`
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.12.ALPHA</version>
<version>2.0.12.BETA</version>
<packaging>jar</packaging>

<name>blade</name>
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/blade/Blade.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.blade.mvc.route.RouteMatcher;
import com.blade.mvc.ui.template.DefaultEngine;
import com.blade.mvc.ui.template.TemplateEngine;
import com.blade.security.web.cors.CorsConfiger;
import com.blade.security.web.cors.CorsMiddleware;
import com.blade.server.Server;
import com.blade.server.netty.NettyServer;
Expand Down Expand Up @@ -534,9 +535,20 @@ public Class<?> bootClass() {
* @return blade
*/
public Blade enableCors(boolean enableCors) {
this.enableCors(enableCors, new CorsConfiger());
return this;
}

/**
* Set whether to config cors
* @param enableCors enable cors
* @param corsConfig config cors
* @return blade
*/
public Blade enableCors(boolean enableCors, CorsConfiger corsConfig) {
this.environment.set(ENV_KEY_CORS_ENABLE, enableCors);
if (enableCors) {
this.use(new CorsMiddleware());
this.use(new CorsMiddleware(corsConfig));
}
return this;
}
Expand Down Expand Up @@ -841,6 +853,7 @@ public Blade start(Class<?> mainCls, String... args) {

this.bootClass = mainCls;
eventManager.fireEvent(EventType.SERVER_STARTING, new Event().attribute("blade", this));

Thread thread = new Thread(() -> {
try {
server.start(Blade.this);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/blade/ioc/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface Injector {
*/
void injection(Object bean);

void injection(Object bean, Object value);

}
2 changes: 2 additions & 0 deletions src/main/java/com/blade/ioc/Ioc.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface Ioc {
*/
<T> T addBean(Class<T> type);

Object createBean(Class<?> type);

/**
* Set bean, e.g aop proxy
*
Expand Down
52 changes: 34 additions & 18 deletions src/main/java/com/blade/ioc/SimpleIoc.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.blade.ioc;

import com.blade.ioc.annotation.Bean;
import com.blade.ioc.bean.BeanDefine;
import com.blade.kit.IocKit;
import com.blade.mvc.WebContext;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
Expand Down Expand Up @@ -30,12 +33,12 @@ public void addBean(Object bean) {
@Override
public void addBean(String name, Object bean) {
BeanDefine beanDefine = new BeanDefine(bean);
addBean(name, beanDefine);
// add interface
put(name, beanDefine);
// add interface、put to pool
Class<?>[] interfaces = beanDefine.getType().getInterfaces();
if (interfaces.length > 0) {
for (Class<?> interfaceClazz : interfaces) {
this.addBean(interfaceClazz.getName(), beanDefine);
this.put(interfaceClazz.getName(), beanDefine);
}
}
}
Expand All @@ -59,8 +62,21 @@ public void setBean(Class<?> type, Object proxyBean) {
*/
@Override
public <T> T addBean(Class<T> type) {
Object bean = addBean(type, true);
return type.cast(bean);
Bean beanAnnotation = type.getAnnotation(Bean.class);
boolean isSingleton = null == beanAnnotation || beanAnnotation.singleton();
if (isSingleton) {
Object bean = put(type, true);
return type.cast(bean);
}
return null;
}

@Override
public Object createBean(Class<?> type) {
BeanDefine beanDefine = createBeanDefine(type, true);
IocKit.initInjection(this, Objects.requireNonNull(beanDefine));
IocKit.injectionValue(WebContext.blade().environment(), beanDefine);
return beanDefine.getBean();
}

@Override
Expand All @@ -69,7 +85,7 @@ public <T> T getBean(Class<T> type) {
try {
return type.cast(bean);
} catch (Exception e) {
e.printStackTrace();
log.error("get bean error", e);
}
return null;
}
Expand All @@ -90,7 +106,7 @@ public List<BeanDefine> getBeanDefines() {

@Override
public BeanDefine getBeanDefine(Class<?> type) {
return this.getBeanDefine(type, true);
return pool.get(type.getName());
}

@Override
Expand Down Expand Up @@ -129,7 +145,7 @@ public void clearAll() {
/**
* Add user-defined objects
*/
private void addBean(String name, BeanDefine beanDefine) {
private void put(String name, BeanDefine beanDefine) {
if (pool.put(name, beanDefine) != null) {
log.warn("Duplicated Bean: {}", name);
}
Expand All @@ -138,40 +154,40 @@ private void addBean(String name, BeanDefine beanDefine) {
/**
* Register @Bean marked objects
*/
private Object addBean(Class<?> type, boolean singleton) {
return addBean(type.getName(), type, singleton);
private Object put(Class<?> type, boolean isSingleton) {
return put(type.getName(), type, isSingleton);
}

/**
* Register @Bean marked objects
*/
private Object addBean(String name, Class<?> beanClass, boolean singleton) {
BeanDefine beanDefine = this.getBeanDefine(beanClass, singleton);
private Object put(String name, Class<?> beanClass, boolean isSingleton) {
BeanDefine beanDefine = this.createBeanDefine(beanClass, isSingleton);

if (pool.put(name, beanDefine) != null) {
log.warn("Duplicated Bean: {}", name);
}

// add interface
// add interface、put to pool
Class<?>[] interfaces = beanClass.getInterfaces();
if (interfaces.length > 0) {
for (Class<?> interfaceClazz : interfaces) {
if (null != this.getBean(interfaceClazz)) {
break;
}
this.addBean(interfaceClazz.getName(), beanDefine);
this.put(interfaceClazz.getName(), beanDefine);
}
}

return beanDefine.getBean();
return Objects.requireNonNull(beanDefine).getBean();
}

private BeanDefine getBeanDefine(Class<?> beanClass, boolean singleton) {
private BeanDefine createBeanDefine(Class<?> beanClass, boolean isSingleton) {
try {
Object object = beanClass.newInstance();
return new BeanDefine(object, beanClass, singleton);
return new BeanDefine(object, beanClass, isSingleton);
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
log.error("get BeanDefine error", e);
}
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/blade/ioc/annotation/Bean.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@

String value() default "";

boolean singleton() default true;

}
4 changes: 3 additions & 1 deletion src/main/java/com/blade/ioc/annotation/Inject.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.lang.annotation.Target;

/**
* Automatic injection
* Automatic initInjection
*
* @author <a href="mailto:biezhi.me@gmail.com" target="_blank">biezhi</a>
* @since 1.5
Expand All @@ -17,4 +17,6 @@

String value() default "";

boolean singleton() default true;

}
26 changes: 17 additions & 9 deletions src/main/java/com/blade/ioc/bean/BeanDefine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
*/
public class BeanDefine {

private Object bean;
private Object bean;
private Class<?> type;
private boolean isSingle;
private boolean isSingleton;
private boolean fieldHasPrototype;

public BeanDefine(Object bean) {
this(bean, bean.getClass());
Expand All @@ -19,13 +20,13 @@ public BeanDefine(Object bean) {
public BeanDefine(Object bean, Class<?> type) {
this.bean = bean;
this.type = type;
this.isSingle = true;
this.isSingleton = true;
}

public BeanDefine(Object bean, Class<?> type, boolean isSingle) {
public BeanDefine(Object bean, Class<?> type, boolean isSingleton) {
this.bean = bean;
this.type = type;
this.isSingle = isSingle;
this.isSingleton = isSingleton;
}

public Object getBean() {
Expand All @@ -44,12 +45,19 @@ public void setType(Class<?> type) {
this.type = type;
}

public boolean isSingle() {
return isSingle;
public boolean isSingleton() {
return isSingleton;
}

public void setSignle(boolean isSingle) {
this.isSingle = isSingle;
public void setSingleton(boolean isSingleton) {
this.isSingleton = isSingleton;
}

public boolean isFieldHasPrototype() {
return fieldHasPrototype;
}

public void setFieldHasPrototype(boolean fieldHasPrototype) {
this.fieldHasPrototype = fieldHasPrototype;
}
}
12 changes: 3 additions & 9 deletions src/main/java/com/blade/ioc/bean/ClassDefine.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public final class ClassDefine {

private static final ConcurrentHashMap<Class<?>, ClassDefine> pool = new ConcurrentHashMap<Class<?>, ClassDefine>(128);
private static final ConcurrentHashMap<Class<?>, ClassDefine> pool = new ConcurrentHashMap<>(128);

private final Class<?> clazz;

Expand Down Expand Up @@ -54,13 +54,12 @@ public List<ClassDefine> getInterfaces() {
return Collections.emptyList();
}
List<ClassDefine> results = new ArrayList<>(interfaces.length);
for (Class<?> intf : interfaces) {
results.add(ClassDefine.create(intf));
for (Class<?> interface_ : interfaces) {
results.add(ClassDefine.create(interface_));
}
return results;
}

// ------------------------------------------------------------------
public Annotation[] getAnnotations() {
return clazz.getAnnotations();
}
Expand All @@ -69,15 +68,10 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return clazz.getAnnotation(annotationClass);
}

public <T extends Annotation> boolean isAnnotationPresent(Class<T> annotationClass) {
return clazz.isAnnotationPresent(annotationClass);
}

public Field[] getDeclaredFields() {
return clazz.getDeclaredFields();
}

// ------------------------------------------------------------------
public int getModifiers() {
return clazz.getModifiers();
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/blade/ioc/bean/FieldInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.blade.ioc.Injector;
import com.blade.ioc.Ioc;
import com.blade.kit.IocKit;

import java.lang.reflect.Field;

Expand All @@ -21,6 +22,14 @@ public FieldInjector(Ioc ioc, Field field) {
this.field = field;
}

public Class<?> getType() {
return field.getType();
}

public boolean isSingleton() {
return IocKit.isSingleton(field.getType());
}

@Override
public void injection(Object bean) {
try {
Expand All @@ -29,11 +38,24 @@ public void injection(Object bean) {
if (value == null) {
throw new IllegalStateException("Can't inject bean: " + fieldType.getName() + " for field: " + field);
}
injection(bean, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void injection(Object bean, Object value) {
try {
field.setAccessible(true);
field.set(bean, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public boolean hasInjectFields() {
return field.getType().getDeclaredFields().length > 0;
}

}
Loading

0 comments on commit 6757d03

Please sign in to comment.