code
stringlengths 1
1.05M
| repo_name
stringlengths 6
83
| path
stringlengths 3
242
| language
stringclasses 222
values | license
stringclasses 20
values | size
int64 1
1.05M
|
|---|---|---|---|---|---|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition, beanName, args);
} catch (Exception e) {
throw new BeansException("Instantiation of bean failed", e);
}
addSingleton(beanName, bean);
return bean;
}
protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
Constructor constructorToUse = null;
Class<?> beanClass = beanDefinition.getBeanClass();
Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
for (Constructor ctor : declaredConstructors) {
if (null != args && ctor.getParameterTypes().length == args.length) {
constructorToUse = ctor;
break;
}
}
return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);
}
public InstantiationStrategy getInstantiationStrategy() {
return instantiationStrategy;
}
public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
this.instantiationStrategy = instantiationStrategy;
}
}
|
2302_77879529/spring
|
small-spring-step-03/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
|
Java
|
apache-2.0
| 1,913
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.BeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
* <p>
* BeanDefinition注册表接口
*/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null);
}
@Override
public Object getBean(String name, Object... args) throws BeansException {
return doGetBean(name, args);
}
protected <T> T doGetBean(final String name, final Object[] args) {
Object bean = getSingleton(name);
if (bean != null) {
return (T) bean;
}
BeanDefinition beanDefinition = getBeanDefinition(name);
return (T) createBean(name, beanDefinition, args);
}
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-03/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractBeanFactory.java
|
Java
|
apache-2.0
| 1,266
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanDefinitionRegistry {
/**
* 向注册表中注册 BeanDefinition
*
* @param beanName
* @param beanDefinition
*/
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
}
|
2302_77879529/spring
|
small-spring-step-03/src/main/java/cn/bugstack/springframework/beans/factory/support/BeanDefinitionRegistry.java
|
Java
|
apache-2.0
| 448
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
import java.lang.reflect.Constructor;
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy {
@Override
public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanDefinition.getBeanClass());
enhancer.setCallback(new NoOp() {
@Override
public int hashCode() {
return super.hashCode();
}
});
if (null == ctor) return enhancer.create();
return enhancer.create(ctor.getParameterTypes(), args);
}
}
|
2302_77879529/spring
|
small-spring-step-03/src/main/java/cn/bugstack/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java
|
Java
|
apache-2.0
| 932
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {
private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName, beanDefinition);
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) throw new BeansException("No bean named '" + beanName + "' is defined");
return beanDefinition;
}
}
|
2302_77879529/spring
|
small-spring-step-03/src/main/java/cn/bugstack/springframework/beans/factory/support/DefaultListableBeanFactory.java
|
Java
|
apache-2.0
| 994
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.factory.config.SingletonBeanRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private Map<String, Object> singletonObjects = new HashMap<>();
@Override
public Object getSingleton(String beanName) {
return singletonObjects.get(beanName);
}
protected void addSingleton(String beanName, Object singletonObject) {
singletonObjects.put(beanName, singletonObject);
}
}
|
2302_77879529/spring
|
small-spring-step-03/src/main/java/cn/bugstack/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java
|
Java
|
apache-2.0
| 664
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
* <p>
* Bean 实例化策略
*/
public interface InstantiationStrategy {
Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-03/src/main/java/cn/bugstack/springframework/beans/factory/support/InstantiationStrategy.java
|
Java
|
apache-2.0
| 501
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class SimpleInstantiationStrategy implements InstantiationStrategy {
@Override
public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
Class clazz = beanDefinition.getBeanClass();
try {
if (null != ctor) {
return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
} else {
return clazz.getDeclaredConstructor().newInstance();
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new BeansException("Failed to instantiate [" + clazz.getName() + "]", e);
}
}
}
|
2302_77879529/spring
|
small-spring-step-03/src/main/java/cn/bugstack/springframework/beans/factory/support/SimpleInstantiationStrategy.java
|
Java
|
apache-2.0
| 1,109
|
package cn.bugstack.springframework.test.bean;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class UserService {
private String name;
public UserService() {
}
public UserService(String name) {
this.name = name;
}
public void queryUserInfo() {
System.out.println("查询用户信息:" + name);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("");
sb.append("").append(name);
return sb.toString();
}
}
|
2302_77879529/spring
|
small-spring-step-03/src/test/java/cn/bugstack/springframework/test/bean/UserService.java
|
Java
|
apache-2.0
| 556
|
package cn.bugstack.springframework.beans;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class BeansException extends RuntimeException {
public BeansException(String msg) {
super(msg);
}
public BeansException(String msg, Throwable cause) {
super(msg, cause);
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/BeansException.java
|
Java
|
apache-2.0
| 329
|
package cn.bugstack.springframework.beans;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* bean 属性信息
*/
public class PropertyValue {
private final String name;
private final Object value;
public PropertyValue(String name, Object value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public Object getValue() {
return value;
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/PropertyValue.java
|
Java
|
apache-2.0
| 467
|
package cn.bugstack.springframework.beans;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class PropertyValues {
private final List<PropertyValue> propertyValueList = new ArrayList<>();
public void addPropertyValue(PropertyValue pv) {
this.propertyValueList.add(pv);
}
public PropertyValue[] getPropertyValues() {
return this.propertyValueList.toArray(new PropertyValue[0]);
}
public PropertyValue getPropertyValue(String propertyName) {
for (PropertyValue pv : this.propertyValueList) {
if (pv.getName().equals(propertyName)) {
return pv;
}
}
return null;
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/PropertyValues.java
|
Java
|
apache-2.0
| 756
|
package cn.bugstack.springframework.beans.factory;
import cn.bugstack.springframework.beans.BeansException;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanFactory {
Object getBean(String name) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/BeanFactory.java
|
Java
|
apache-2.0
| 341
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.PropertyValues;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class BeanDefinition {
private Class beanClass;
private PropertyValues propertyValues;
public BeanDefinition(Class beanClass) {
this.beanClass = beanClass;
this.propertyValues = new PropertyValues();
}
public BeanDefinition(Class beanClass, PropertyValues propertyValues) {
this.beanClass = beanClass;
this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
public PropertyValues getPropertyValues() {
return propertyValues;
}
public void setPropertyValues(PropertyValues propertyValues) {
this.propertyValues = propertyValues;
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/config/BeanDefinition.java
|
Java
|
apache-2.0
| 1,009
|
package cn.bugstack.springframework.beans.factory.config;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* Bean 的引用
*/
public class BeanReference {
private final String beanName;
public BeanReference(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/config/BeanReference.java
|
Java
|
apache-2.0
| 368
|
package cn.bugstack.springframework.beans.factory.config;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* 单例注册表
*/
public interface SingletonBeanRegistry {
Object getSingleton(String beanName);
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/config/SingletonBeanRegistry.java
|
Java
|
apache-2.0
| 237
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.PropertyValues;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanReference;
import cn.hutool.core.bean.BeanUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition, beanName, args);
// 给 Bean 填充属性
applyPropertyValues(beanName, bean, beanDefinition);
} catch (Exception e) {
throw new BeansException("Instantiation of bean failed", e);
}
addSingleton(beanName, bean);
return bean;
}
protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
Constructor constructorToUse = null;
Class<?> beanClass = beanDefinition.getBeanClass();
Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
for (Constructor ctor : declaredConstructors) {
if (null != args && ctor.getParameterTypes().length == args.length) {
constructorToUse = ctor;
break;
}
}
return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);
}
/**
* Bean 属性填充
*/
protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
try {
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
String name = propertyValue.getName();
Object value = propertyValue.getValue();
if (value instanceof BeanReference) {
// A 依赖 B,获取 B 的实例化
BeanReference beanReference = (BeanReference) value;
value = getBean(beanReference.getBeanName());
}
// 属性填充
BeanUtil.setFieldValue(bean, name, value);
}
} catch (Exception e) {
throw new BeansException("Error setting property values:" + beanName);
}
}
public InstantiationStrategy getInstantiationStrategy() {
return instantiationStrategy;
}
public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
this.instantiationStrategy = instantiationStrategy;
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
|
Java
|
apache-2.0
| 3,180
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.BeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
* <p>
* BeanDefinition注册表接口
*/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null);
}
@Override
public Object getBean(String name, Object... args) throws BeansException {
return doGetBean(name, args);
}
protected <T> T doGetBean(final String name, final Object[] args) {
Object bean = getSingleton(name);
if (bean != null) {
return (T) bean;
}
BeanDefinition beanDefinition = getBeanDefinition(name);
return (T) createBean(name, beanDefinition, args);
}
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractBeanFactory.java
|
Java
|
apache-2.0
| 1,266
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanDefinitionRegistry {
/**
* 向注册表中注册 BeanDefinition
*
* @param beanName
* @param beanDefinition
*/
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/support/BeanDefinitionRegistry.java
|
Java
|
apache-2.0
| 448
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
import java.lang.reflect.Constructor;
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy {
@Override
public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanDefinition.getBeanClass());
enhancer.setCallback(new NoOp() {
@Override
public int hashCode() {
return super.hashCode();
}
});
if (null == ctor) return enhancer.create();
return enhancer.create(ctor.getParameterTypes(), args);
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java
|
Java
|
apache-2.0
| 932
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {
private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName, beanDefinition);
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) throw new BeansException("No bean named '" + beanName + "' is defined");
return beanDefinition;
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/support/DefaultListableBeanFactory.java
|
Java
|
apache-2.0
| 994
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.factory.config.SingletonBeanRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private Map<String, Object> singletonObjects = new HashMap<>();
@Override
public Object getSingleton(String beanName) {
return singletonObjects.get(beanName);
}
protected void addSingleton(String beanName, Object singletonObject) {
singletonObjects.put(beanName, singletonObject);
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java
|
Java
|
apache-2.0
| 664
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
* <p>
* Bean 实例化策略
*/
public interface InstantiationStrategy {
Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/support/InstantiationStrategy.java
|
Java
|
apache-2.0
| 501
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class SimpleInstantiationStrategy implements InstantiationStrategy {
@Override
public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
Class clazz = beanDefinition.getBeanClass();
try {
if (null != ctor) {
return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
} else {
return clazz.getDeclaredConstructor().newInstance();
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new BeansException("Failed to instantiate [" + clazz.getName() + "]", e);
}
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/main/java/cn/bugstack/springframework/beans/factory/support/SimpleInstantiationStrategy.java
|
Java
|
apache-2.0
| 1,109
|
package cn.bugstack.springframework.test.bean;
import java.util.HashMap;
import java.util.Map;
public class UserDao {
private static Map<String, String> hashMap = new HashMap<>();
static {
hashMap.put("10001", "小傅哥");
hashMap.put("10002", "八杯水");
hashMap.put("10003", "阿毛");
}
public String queryUserName(String uId) {
return hashMap.get(uId);
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/test/java/cn/bugstack/springframework/test/bean/UserDao.java
|
Java
|
apache-2.0
| 422
|
package cn.bugstack.springframework.test.bean;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class UserService {
private String uId;
private UserDao userDao;
public void queryUserInfo() {
System.out.println("查询用户信息:" + userDao.queryUserName(uId));
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
|
2302_77879529/spring
|
small-spring-step-04/src/test/java/cn/bugstack/springframework/test/bean/UserService.java
|
Java
|
apache-2.0
| 598
|
package cn.bugstack.springframework.beans;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class BeansException extends RuntimeException {
public BeansException(String msg) {
super(msg);
}
public BeansException(String msg, Throwable cause) {
super(msg, cause);
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/BeansException.java
|
Java
|
apache-2.0
| 329
|
package cn.bugstack.springframework.beans;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* bean 属性信息
*/
public class PropertyValue {
private final String name;
private final Object value;
public PropertyValue(String name, Object value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public Object getValue() {
return value;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/PropertyValue.java
|
Java
|
apache-2.0
| 467
|
package cn.bugstack.springframework.beans;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class PropertyValues {
private final List<PropertyValue> propertyValueList = new ArrayList<>();
public void addPropertyValue(PropertyValue pv) {
this.propertyValueList.add(pv);
}
public PropertyValue[] getPropertyValues() {
return this.propertyValueList.toArray(new PropertyValue[0]);
}
public PropertyValue getPropertyValue(String propertyName) {
for (PropertyValue pv : this.propertyValueList) {
if (pv.getName().equals(propertyName)) {
return pv;
}
}
return null;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/PropertyValues.java
|
Java
|
apache-2.0
| 756
|
package cn.bugstack.springframework.beans.factory;
import cn.bugstack.springframework.beans.BeansException;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanFactory {
Object getBean(String name) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/BeanFactory.java
|
Java
|
apache-2.0
| 419
|
package cn.bugstack.springframework.beans.factory;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.AutowireCapableBeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.ConfigurableBeanFactory;
/**
* Configuration interface to be implemented by most listable bean factories.
* In addition to {@link ConfigurableBeanFactory}, it provides facilities to
* analyze and modify bean definitions, and to pre-instantiate singletons.
*/
public interface ConfigurableListableBeanFactory extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {
BeanDefinition getBeanDefinition(String beanName) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/ConfigurableListableBeanFactory.java
|
Java
|
apache-2.0
| 798
|
package cn.bugstack.springframework.beans.factory;
/**
* Sub-interface implemented by bean factories that can be part
* of a hierarchy.
*/
public interface HierarchicalBeanFactory extends BeanFactory {
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/HierarchicalBeanFactory.java
|
Java
|
apache-2.0
| 208
|
package cn.bugstack.springframework.beans.factory;
import cn.bugstack.springframework.beans.BeansException;
import java.util.Map;
/**
* Extension of the {@link BeanFactory} interface to be implemented by bean factories
* that can enumerate all their bean instances, rather than attempting bean lookup
* by name one by one as requested by clients. BeanFactory implementations that
* preload all their bean definitions (such as XML-based factories) may implement
* this interface.
* <p>
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface ListableBeanFactory extends BeanFactory{
/**
* 按照类型返回 Bean 实例
* @param type
* @param <T>
* @return
* @throws BeansException
*/
<T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;
/**
* Return the names of all beans defined in this registry.
*
* 返回注册表中所有的Bean名称
*/
String[] getBeanDefinitionNames();
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/ListableBeanFactory.java
|
Java
|
apache-2.0
| 1,019
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.factory.BeanFactory;
/**
* Extension of the {@link cn.bugstack.springframework.beans.factory.BeanFactory}
* interface to be implemented by bean factories that are capable of
* autowiring, provided that they want to expose this functionality for
* existing bean instances.
*/
public interface AutowireCapableBeanFactory extends BeanFactory {
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/config/AutowireCapableBeanFactory.java
|
Java
|
apache-2.0
| 449
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.PropertyValues;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class BeanDefinition {
private Class beanClass;
private PropertyValues propertyValues;
public BeanDefinition(Class beanClass) {
this.beanClass = beanClass;
this.propertyValues = new PropertyValues();
}
public BeanDefinition(Class beanClass, PropertyValues propertyValues) {
this.beanClass = beanClass;
this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
public PropertyValues getPropertyValues() {
return propertyValues;
}
public void setPropertyValues(PropertyValues propertyValues) {
this.propertyValues = propertyValues;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/config/BeanDefinition.java
|
Java
|
apache-2.0
| 1,009
|
package cn.bugstack.springframework.beans.factory.config;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* Bean 的引用
*/
public class BeanReference {
private final String beanName;
public BeanReference(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/config/BeanReference.java
|
Java
|
apache-2.0
| 368
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.factory.HierarchicalBeanFactory;
/**
* Configuration interface to be implemented by most bean factories. Provides
* facilities to configure a bean factory, in addition to the bean factory
* client methods in the {@link cn.bugstack.springframework.beans.factory.BeanFactory}
* interface.
*/
public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {
String SCOPE_SINGLETON = "singleton";
String SCOPE_PROTOTYPE = "prototype";
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/config/ConfigurableBeanFactory.java
|
Java
|
apache-2.0
| 583
|
package cn.bugstack.springframework.beans.factory.config;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* 单例注册表
*/
public interface SingletonBeanRegistry {
Object getSingleton(String beanName);
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/config/SingletonBeanRegistry.java
|
Java
|
apache-2.0
| 285
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.PropertyValues;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanReference;
import cn.hutool.core.bean.BeanUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition, beanName, args);
// 给 Bean 填充属性
applyPropertyValues(beanName, bean, beanDefinition);
} catch (Exception e) {
throw new BeansException("Instantiation of bean failed", e);
}
addSingleton(beanName, bean);
return bean;
}
protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
Constructor constructorToUse = null;
Class<?> beanClass = beanDefinition.getBeanClass();
Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
for (Constructor ctor : declaredConstructors) {
if (null != args && ctor.getParameterTypes().length == args.length) {
constructorToUse = ctor;
break;
}
}
return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);
}
/**
* Bean 属性填充
*/
protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
try {
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
String name = propertyValue.getName();
Object value = propertyValue.getValue();
if (value instanceof BeanReference) {
// A 依赖 B,获取 B 的实例化
BeanReference beanReference = (BeanReference) value;
value = getBean(beanReference.getBeanName());
}
// 属性填充
BeanUtil.setFieldValue(bean, name, value);
}
} catch (Exception e) {
throw new BeansException("Error setting property values:" + beanName);
}
}
public InstantiationStrategy getInstantiationStrategy() {
return instantiationStrategy;
}
public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
this.instantiationStrategy = instantiationStrategy;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
|
Java
|
apache-2.0
| 3,180
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.core.io.DefaultResourceLoader;
import cn.bugstack.springframework.core.io.ResourceLoader;
/**
* Abstract base class for bean definition readers which implement
* the {@link BeanDefinitionReader} interface.
* <p>
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {
private final BeanDefinitionRegistry registry;
private ResourceLoader resourceLoader;
protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
this(registry, new DefaultResourceLoader());
}
public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {
this.registry = registry;
this.resourceLoader = resourceLoader;
}
@Override
public BeanDefinitionRegistry getRegistry() {
return registry;
}
@Override
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractBeanDefinitionReader.java
|
Java
|
apache-2.0
| 1,102
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.BeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
* <p>
* BeanDefinition注册表接口
*/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null);
}
@Override
public Object getBean(String name, Object... args) throws BeansException {
return doGetBean(name, args);
}
@Override
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return (T) getBean(name);
}
protected <T> T doGetBean(final String name, final Object[] args) {
Object bean = getSingleton(name);
if (bean != null) {
return (T) bean;
}
BeanDefinition beanDefinition = getBeanDefinition(name);
return (T) createBean(name, beanDefinition, args);
}
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractBeanFactory.java
|
Java
|
apache-2.0
| 1,406
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.core.io.Resource;
import cn.bugstack.springframework.core.io.ResourceLoader;
/**
* Simple interface for bean definition readers.
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanDefinitionReader {
BeanDefinitionRegistry getRegistry();
ResourceLoader getResourceLoader();
void loadBeanDefinitions(Resource resource) throws BeansException;
void loadBeanDefinitions(Resource... resources) throws BeansException;
void loadBeanDefinitions(String location) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/BeanDefinitionReader.java
|
Java
|
apache-2.0
| 711
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanDefinitionRegistry {
/**
* 向注册表中注册 BeanDefinition
*
* @param beanName
* @param beanDefinition
*/
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
/**
* 使用Bean名称查询BeanDefinition
*
* @param beanName
* @return
* @throws BeansException
*/
BeanDefinition getBeanDefinition(String beanName) throws BeansException;
/**
* 判断是否包含指定名称的BeanDefinition
* @param beanName
* @return
*/
boolean containsBeanDefinition(String beanName);
/**
* Return the names of all beans defined in this registry.
*
* 返回注册表中所有的Bean名称
*/
String[] getBeanDefinitionNames();
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/BeanDefinitionRegistry.java
|
Java
|
apache-2.0
| 1,052
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
import java.lang.reflect.Constructor;
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy {
@Override
public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanDefinition.getBeanClass());
enhancer.setCallback(new NoOp() {
@Override
public int hashCode() {
return super.hashCode();
}
});
if (null == ctor) return enhancer.create();
return enhancer.create(ctor.getParameterTypes(), args);
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java
|
Java
|
apache-2.0
| 932
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.ConfigurableListableBeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.ConfigurableBeanFactory;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry, ConfigurableListableBeanFactory {
private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName, beanDefinition);
}
@Override
public boolean containsBeanDefinition(String beanName) {
return beanDefinitionMap.containsKey(beanName);
}
@Override
public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
Map<String, T> result = new HashMap<>();
beanDefinitionMap.forEach((beanName, beanDefinition) -> {
Class beanClass = beanDefinition.getBeanClass();
if (type.isAssignableFrom(beanClass)) {
result.put(beanName, (T) getBean(beanName));
}
});
return result;
}
@Override
public String[] getBeanDefinitionNames() {
return beanDefinitionMap.keySet().toArray(new String[0]);
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) throw new BeansException("No bean named '" + beanName + "' is defined");
return beanDefinition;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/DefaultListableBeanFactory.java
|
Java
|
apache-2.0
| 1,930
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.factory.config.SingletonBeanRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private Map<String, Object> singletonObjects = new HashMap<>();
@Override
public Object getSingleton(String beanName) {
return singletonObjects.get(beanName);
}
protected void addSingleton(String beanName, Object singletonObject) {
singletonObjects.put(beanName, singletonObject);
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java
|
Java
|
apache-2.0
| 664
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
* <p>
* Bean 实例化策略
*/
public interface InstantiationStrategy {
Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/InstantiationStrategy.java
|
Java
|
apache-2.0
| 501
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class SimpleInstantiationStrategy implements InstantiationStrategy {
@Override
public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
Class clazz = beanDefinition.getBeanClass();
try {
if (null != ctor) {
return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
} else {
return clazz.getDeclaredConstructor().newInstance();
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new BeansException("Failed to instantiate [" + clazz.getName() + "]", e);
}
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/support/SimpleInstantiationStrategy.java
|
Java
|
apache-2.0
| 1,109
|
package cn.bugstack.springframework.beans.factory.xml;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanReference;
import cn.bugstack.springframework.beans.factory.support.AbstractBeanDefinitionReader;
import cn.bugstack.springframework.beans.factory.support.BeanDefinitionRegistry;
import cn.bugstack.springframework.core.io.Resource;
import cn.bugstack.springframework.core.io.ResourceLoader;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.IOException;
import java.io.InputStream;
/**
* Bean definition reader for XML bean definitions.
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
super(registry);
}
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {
super(registry, resourceLoader);
}
@Override
public void loadBeanDefinitions(Resource resource) throws BeansException {
try (InputStream inputStream = resource.getInputStream()){
doLoadBeanDefinitions(inputStream);
} catch (IOException | ClassNotFoundException e) {
throw new BeansException("IOException parsing XML document from " + resource, e);
}
}
@Override
public void loadBeanDefinitions(Resource... resources) throws BeansException {
for (Resource resource : resources) {
loadBeanDefinitions(resource);
}
}
@Override
public void loadBeanDefinitions(String location) throws BeansException {
ResourceLoader resourceLoader = getResourceLoader();
Resource resource = resourceLoader.getResource(location);
loadBeanDefinitions(resource);
}
protected void doLoadBeanDefinitions(InputStream inputStream) throws ClassNotFoundException {
Document doc = XmlUtil.readXML(inputStream);
Element root = doc.getDocumentElement();
NodeList childNodes = root.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
// 判断元素
if (!(childNodes.item(i) instanceof Element)) continue;
// 判断对象
if (!"bean".equals(childNodes.item(i).getNodeName())) continue;
// 解析标签
Element bean = (Element) childNodes.item(i);
String id = bean.getAttribute("id");
String name = bean.getAttribute("name");
String className = bean.getAttribute("class");
// 获取 Class,方便获取类中的名称
Class<?> clazz = Class.forName(className);
// 优先级 id > name
String beanName = StrUtil.isNotEmpty(id) ? id : name;
if (StrUtil.isEmpty(beanName)) {
beanName = StrUtil.lowerFirst(clazz.getSimpleName());
}
// 定义Bean
BeanDefinition beanDefinition = new BeanDefinition(clazz);
// 读取属性并填充
for (int j = 0; j < bean.getChildNodes().getLength(); j++) {
if (!(bean.getChildNodes().item(j) instanceof Element)) continue;
if (!"property".equals(bean.getChildNodes().item(j).getNodeName())) continue;
// 解析标签:property
Element property = (Element) bean.getChildNodes().item(j);
String attrName = property.getAttribute("name");
String attrValue = property.getAttribute("value");
String attrRef = property.getAttribute("ref");
// 获取属性值:引入对象、值对象
Object value = StrUtil.isNotEmpty(attrRef) ? new BeanReference(attrRef) : attrValue;
// 创建属性信息
PropertyValue propertyValue = new PropertyValue(attrName, value);
beanDefinition.getPropertyValues().addPropertyValue(propertyValue);
}
if (getRegistry().containsBeanDefinition(beanName)) {
throw new BeansException("Duplicate beanName[" + beanName + "] is not allowed");
}
// 注册 BeanDefinition
getRegistry().registerBeanDefinition(beanName, beanDefinition);
}
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/beans/factory/xml/XmlBeanDefinitionReader.java
|
Java
|
apache-2.0
| 4,621
|
package cn.bugstack.springframework.core.io;
import cn.bugstack.springframework.util.ClassUtils;
import cn.hutool.core.lang.Assert;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class ClassPathResource implements Resource {
private final String path;
private ClassLoader classLoader;
public ClassPathResource(String path) {
this(path, (ClassLoader) null);
}
public ClassPathResource(String path, ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
this.path = path;
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
@Override
public InputStream getInputStream() throws IOException {
InputStream is = classLoader.getResourceAsStream(path);
if (is == null) {
throw new FileNotFoundException(
this.path + " cannot be opened because it does not exist");
}
return is;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/core/io/ClassPathResource.java
|
Java
|
apache-2.0
| 1,029
|
package cn.bugstack.springframework.core.io;
import cn.hutool.core.lang.Assert;
import java.net.MalformedURLException;
import java.net.URL;
public class DefaultResourceLoader implements ResourceLoader {
@Override
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
if (location.startsWith(CLASSPATH_URL_PREFIX)) {
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
}
else {
try {
URL url = new URL(location);
return new UrlResource(url);
} catch (MalformedURLException e) {
return new FileSystemResource(location);
}
}
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/core/io/DefaultResourceLoader.java
|
Java
|
apache-2.0
| 756
|
package cn.bugstack.springframework.core.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileSystemResource implements Resource {
private final File file;
private final String path;
public FileSystemResource(File file) {
this.file = file;
this.path = file.getPath();
}
public FileSystemResource(String path) {
this.file = new File(path);
this.path = path;
}
@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(this.file);
}
public final String getPath() {
return this.path;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/core/io/FileSystemResource.java
|
Java
|
apache-2.0
| 699
|
package cn.bugstack.springframework.core.io;
import java.io.IOException;
import java.io.InputStream;
public interface Resource {
InputStream getInputStream() throws IOException;
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/core/io/Resource.java
|
Java
|
apache-2.0
| 188
|
package cn.bugstack.springframework.core.io;
public interface ResourceLoader {
/**
* Pseudo URL prefix for loading from the class path: "classpath:"
*/
String CLASSPATH_URL_PREFIX = "classpath:";
Resource getResource(String location);
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/core/io/ResourceLoader.java
|
Java
|
apache-2.0
| 263
|
package cn.bugstack.springframework.core.io;
import cn.hutool.core.lang.Assert;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class UrlResource implements Resource{
private final URL url;
public UrlResource(URL url) {
Assert.notNull(url,"URL must not be null");
this.url = url;
}
@Override
public InputStream getInputStream() throws IOException {
URLConnection con = this.url.openConnection();
try {
return con.getInputStream();
}
catch (IOException ex){
if (con instanceof HttpURLConnection){
((HttpURLConnection) con).disconnect();
}
throw ex;
}
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/core/io/UrlResource.java
|
Java
|
apache-2.0
| 807
|
package cn.bugstack.springframework.util;
public class ClassUtils {
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back to system class loader...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
}
return cl;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/main/java/cn/bugstack/springframework/util/ClassUtils.java
|
Java
|
apache-2.0
| 581
|
package cn.bugstack.springframework.test.bean;
import java.util.HashMap;
import java.util.Map;
public class UserDao {
private static Map<String, String> hashMap = new HashMap<>();
static {
hashMap.put("10001", "小傅哥");
hashMap.put("10002", "八杯水");
hashMap.put("10003", "阿毛");
}
public String queryUserName(String uId) {
return hashMap.get(uId);
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/test/java/cn/bugstack/springframework/test/bean/UserDao.java
|
Java
|
apache-2.0
| 422
|
package cn.bugstack.springframework.test.bean;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class UserService {
private String uId;
private UserDao userDao;
public String queryUserInfo() {
return userDao.queryUserName(uId);
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
|
2302_77879529/spring
|
small-spring-step-05/src/test/java/cn/bugstack/springframework/test/bean/UserService.java
|
Java
|
apache-2.0
| 561
|
package cn.bugstack.springframework.beans;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class BeansException extends RuntimeException {
public BeansException(String msg) {
super(msg);
}
public BeansException(String msg, Throwable cause) {
super(msg, cause);
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/BeansException.java
|
Java
|
apache-2.0
| 329
|
package cn.bugstack.springframework.beans;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* bean 属性信息
*/
public class PropertyValue {
private final String name;
private final Object value;
public PropertyValue(String name, Object value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public Object getValue() {
return value;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/PropertyValue.java
|
Java
|
apache-2.0
| 467
|
package cn.bugstack.springframework.beans;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class PropertyValues {
private final List<PropertyValue> propertyValueList = new ArrayList<>();
public void addPropertyValue(PropertyValue pv) {
this.propertyValueList.add(pv);
}
public PropertyValue[] getPropertyValues() {
return this.propertyValueList.toArray(new PropertyValue[0]);
}
public PropertyValue getPropertyValue(String propertyName) {
for (PropertyValue pv : this.propertyValueList) {
if (pv.getName().equals(propertyName)) {
return pv;
}
}
return null;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/PropertyValues.java
|
Java
|
apache-2.0
| 756
|
package cn.bugstack.springframework.beans.factory;
import cn.bugstack.springframework.beans.BeansException;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanFactory {
Object getBean(String name) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/BeanFactory.java
|
Java
|
apache-2.0
| 419
|
package cn.bugstack.springframework.beans.factory;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.AutowireCapableBeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanPostProcessor;
import cn.bugstack.springframework.beans.factory.config.ConfigurableBeanFactory;
/**
* Configuration interface to be implemented by most listable bean factories.
* In addition to {@link ConfigurableBeanFactory}, it provides facilities to
* analyze and modify bean definitions, and to pre-instantiate singletons.
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface ConfigurableListableBeanFactory extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {
BeanDefinition getBeanDefinition(String beanName) throws BeansException;
void preInstantiateSingletons() throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/ConfigurableListableBeanFactory.java
|
Java
|
apache-2.0
| 1,012
|
package cn.bugstack.springframework.beans.factory;
/**
* Sub-interface implemented by bean factories that can be part
* of a hierarchy.
*/
public interface HierarchicalBeanFactory extends BeanFactory {
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/HierarchicalBeanFactory.java
|
Java
|
apache-2.0
| 208
|
package cn.bugstack.springframework.beans.factory;
import cn.bugstack.springframework.beans.BeansException;
import java.util.Map;
/**
* Extension of the {@link BeanFactory} interface to be implemented by bean factories
* that can enumerate all their bean instances, rather than attempting bean lookup
* by name one by one as requested by clients. BeanFactory implementations that
* preload all their bean definitions (such as XML-based factories) may implement
* this interface.
* <p>
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface ListableBeanFactory extends BeanFactory{
/**
* 按照类型返回 Bean 实例
* @param type
* @param <T>
* @return
* @throws BeansException
*/
<T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;
/**
* Return the names of all beans defined in this registry.
*
* 返回注册表中所有的Bean名称
*/
String[] getBeanDefinitionNames();
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/ListableBeanFactory.java
|
Java
|
apache-2.0
| 1,019
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.BeanFactory;
/**
* Extension of the {@link cn.bugstack.springframework.beans.factory.BeanFactory}
* interface to be implemented by bean factories that are capable of
* autowiring, provided that they want to expose this functionality for
* existing bean instances.
*/
public interface AutowireCapableBeanFactory extends BeanFactory {
/**
* 执行 BeanPostProcessors 接口实现类的 postProcessBeforeInitialization 方法
*
* @param existingBean
* @param beanName
* @return
* @throws BeansException
*/
Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException;
/**
* 执行 BeanPostProcessors 接口实现类的 postProcessorsAfterInitialization 方法
*
* @param existingBean
* @param beanName
* @return
* @throws BeansException
*/
Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/config/AutowireCapableBeanFactory.java
|
Java
|
apache-2.0
| 1,160
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.PropertyValues;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class BeanDefinition {
private Class beanClass;
private PropertyValues propertyValues;
public BeanDefinition(Class beanClass) {
this.beanClass = beanClass;
this.propertyValues = new PropertyValues();
}
public BeanDefinition(Class beanClass, PropertyValues propertyValues) {
this.beanClass = beanClass;
this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
public PropertyValues getPropertyValues() {
return propertyValues;
}
public void setPropertyValues(PropertyValues propertyValues) {
this.propertyValues = propertyValues;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/config/BeanDefinition.java
|
Java
|
apache-2.0
| 1,009
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.ConfigurableListableBeanFactory;
/**
* Allows for custom modification of an application context's bean definitions,
* adapting the bean property values of the context's underlying bean factory.
*
* 允许自定义修改 BeanDefinition 属性信息
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanFactoryPostProcessor {
/**
* 在所有的 BeanDefinition 加载完成后,实例化 Bean 对象之前,提供修改 BeanDefinition 属性的机制
*
* @param beanFactory
* @throws BeansException
*/
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/config/BeanFactoryPostProcessor.java
|
Java
|
apache-2.0
| 855
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.BeansException;
/**
* Factory hook that allows for custom modification of new bean instances,
* e.g. checking for marker interfaces or wrapping them with proxies.
*
* 用于修改新实例化 Bean 对象的扩展点
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanPostProcessor {
/**
* 在 Bean 对象执行初始化方法之前,执行此方法
*
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
/**
* 在 Bean 对象执行初始化方法之后,执行此方法
*
* @param bean
* @param beanName
* @return
* @throws BeansException
*/
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/config/BeanPostProcessor.java
|
Java
|
apache-2.0
| 993
|
package cn.bugstack.springframework.beans.factory.config;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* Bean 的引用
*/
public class BeanReference {
private final String beanName;
public BeanReference(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/config/BeanReference.java
|
Java
|
apache-2.0
| 368
|
package cn.bugstack.springframework.beans.factory.config;
import cn.bugstack.springframework.beans.factory.HierarchicalBeanFactory;
/**
* Configuration interface to be implemented by most bean factories. Provides
* facilities to configure a bean factory, in addition to the bean factory
* client methods in the {@link cn.bugstack.springframework.beans.factory.BeanFactory}
* interface.
*/
public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {
String SCOPE_SINGLETON = "singleton";
String SCOPE_PROTOTYPE = "prototype";
void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/config/ConfigurableBeanFactory.java
|
Java
|
apache-2.0
| 652
|
package cn.bugstack.springframework.beans.factory.config;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*
* 单例注册表
*/
public interface SingletonBeanRegistry {
Object getSingleton(String beanName);
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/config/SingletonBeanRegistry.java
|
Java
|
apache-2.0
| 285
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.PropertyValues;
import cn.bugstack.springframework.beans.factory.config.AutowireCapableBeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanPostProcessor;
import cn.bugstack.springframework.beans.factory.config.BeanReference;
import cn.hutool.core.bean.BeanUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory implements AutowireCapableBeanFactory {
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition, beanName, args);
// 给 Bean 填充属性
applyPropertyValues(beanName, bean, beanDefinition);
// 执行 Bean 的初始化方法和 BeanPostProcessor 的前置和后置处理方法
bean = initializeBean(beanName, bean, beanDefinition);
} catch (Exception e) {
throw new BeansException("Instantiation of bean failed", e);
}
addSingleton(beanName, bean);
return bean;
}
protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
Constructor constructorToUse = null;
Class<?> beanClass = beanDefinition.getBeanClass();
Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
for (Constructor ctor : declaredConstructors) {
if (null != args && ctor.getParameterTypes().length == args.length) {
constructorToUse = ctor;
break;
}
}
return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);
}
/**
* Bean 属性填充
*/
protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
try {
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
String name = propertyValue.getName();
Object value = propertyValue.getValue();
if (value instanceof BeanReference) {
// A 依赖 B,获取 B 的实例化
BeanReference beanReference = (BeanReference) value;
value = getBean(beanReference.getBeanName());
}
// 属性填充
BeanUtil.setFieldValue(bean, name, value);
}
} catch (Exception e) {
throw new BeansException("Error setting property values:" + beanName);
}
}
public InstantiationStrategy getInstantiationStrategy() {
return instantiationStrategy;
}
public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
this.instantiationStrategy = instantiationStrategy;
}
private Object initializeBean(String beanName, Object bean, BeanDefinition beanDefinition) {
// 1. 执行 BeanPostProcessor Before 处理
Object wrappedBean = applyBeanPostProcessorsBeforeInitialization(bean, beanName);
// 待完成内容:invokeInitMethods(beanName, wrappedBean, beanDefinition);
invokeInitMethods(beanName, wrappedBean, beanDefinition);
// 2. 执行 BeanPostProcessor After 处理
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
return wrappedBean;
}
private void invokeInitMethods(String beanName, Object wrappedBean, BeanDefinition beanDefinition) {
}
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessBeforeInitialization(result, beanName);
if (null == current) return result;
result = current;
}
return result;
}
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessAfterInitialization(result, beanName);
if (null == current) return result;
result = current;
}
return result;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
|
Java
|
apache-2.0
| 5,132
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.core.io.DefaultResourceLoader;
import cn.bugstack.springframework.core.io.ResourceLoader;
/**
* Abstract base class for bean definition readers which implement
* the {@link BeanDefinitionReader} interface.
* <p>
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {
private final BeanDefinitionRegistry registry;
private ResourceLoader resourceLoader;
protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
this(registry, new DefaultResourceLoader());
}
public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {
this.registry = registry;
this.resourceLoader = resourceLoader;
}
@Override
public BeanDefinitionRegistry getRegistry() {
return registry;
}
@Override
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractBeanDefinitionReader.java
|
Java
|
apache-2.0
| 1,159
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.BeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanPostProcessor;
import cn.bugstack.springframework.beans.factory.config.ConfigurableBeanFactory;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
* <p>
* BeanDefinition注册表接口
*/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements ConfigurableBeanFactory {
/** BeanPostProcessors to apply in createBean */
private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null);
}
@Override
public Object getBean(String name, Object... args) throws BeansException {
return doGetBean(name, args);
}
@Override
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return (T) getBean(name);
}
protected <T> T doGetBean(final String name, final Object[] args) {
Object bean = getSingleton(name);
if (bean != null) {
return (T) bean;
}
BeanDefinition beanDefinition = getBeanDefinition(name);
return (T) createBean(name, beanDefinition, args);
}
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor){
this.beanPostProcessors.remove(beanPostProcessor);
this.beanPostProcessors.add(beanPostProcessor);
}
/**
* Return the list of BeanPostProcessors that will get applied
* to beans created with this factory.
*/
public List<BeanPostProcessor> getBeanPostProcessors() {
return this.beanPostProcessors;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/AbstractBeanFactory.java
|
Java
|
apache-2.0
| 2,228
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.core.io.Resource;
import cn.bugstack.springframework.core.io.ResourceLoader;
/**
* Simple interface for bean definition readers.
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanDefinitionReader {
BeanDefinitionRegistry getRegistry();
ResourceLoader getResourceLoader();
void loadBeanDefinitions(Resource resource) throws BeansException;
void loadBeanDefinitions(Resource... resources) throws BeansException;
void loadBeanDefinitions(String location) throws BeansException;
void loadBeanDefinitions(String... locations) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/BeanDefinitionReader.java
|
Java
|
apache-2.0
| 785
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface BeanDefinitionRegistry {
/**
* 向注册表中注册 BeanDefinition
*
* @param beanName
* @param beanDefinition
*/
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
/**
* 使用Bean名称查询BeanDefinition
*
* @param beanName
* @return
* @throws BeansException
*/
BeanDefinition getBeanDefinition(String beanName) throws BeansException;
/**
* 判断是否包含指定名称的BeanDefinition
* @param beanName
* @return
*/
boolean containsBeanDefinition(String beanName);
/**
* Return the names of all beans defined in this registry.
*
* 返回注册表中所有的Bean名称
*/
String[] getBeanDefinitionNames();
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/BeanDefinitionRegistry.java
|
Java
|
apache-2.0
| 1,052
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
import java.lang.reflect.Constructor;
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy {
@Override
public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanDefinition.getBeanClass());
enhancer.setCallback(new NoOp() {
@Override
public int hashCode() {
return super.hashCode();
}
});
if (null == ctor) return enhancer.create();
return enhancer.create(ctor.getParameterTypes(), args);
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java
|
Java
|
apache-2.0
| 932
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.ConfigurableListableBeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.ConfigurableBeanFactory;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry, ConfigurableListableBeanFactory {
private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>();
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName, beanDefinition);
}
@Override
public boolean containsBeanDefinition(String beanName) {
return beanDefinitionMap.containsKey(beanName);
}
@Override
public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
Map<String, T> result = new HashMap<>();
beanDefinitionMap.forEach((beanName, beanDefinition) -> {
Class beanClass = beanDefinition.getBeanClass();
if (type.isAssignableFrom(beanClass)) {
result.put(beanName, (T) getBean(beanName));
}
});
return result;
}
@Override
public String[] getBeanDefinitionNames() {
return beanDefinitionMap.keySet().toArray(new String[0]);
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws BeansException {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) throw new BeansException("No bean named '" + beanName + "' is defined");
return beanDefinition;
}
@Override
public void preInstantiateSingletons() throws BeansException {
beanDefinitionMap.keySet().forEach(this::getBean);
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/DefaultListableBeanFactory.java
|
Java
|
apache-2.0
| 2,134
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.factory.config.SingletonBeanRegistry;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private Map<String, Object> singletonObjects = new HashMap<>();
@Override
public Object getSingleton(String beanName) {
return singletonObjects.get(beanName);
}
protected void addSingleton(String beanName, Object singletonObject) {
singletonObjects.put(beanName, singletonObject);
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java
|
Java
|
apache-2.0
| 664
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
* <p>
* Bean 实例化策略
*/
public interface InstantiationStrategy {
Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/InstantiationStrategy.java
|
Java
|
apache-2.0
| 501
|
package cn.bugstack.springframework.beans.factory.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class SimpleInstantiationStrategy implements InstantiationStrategy {
@Override
public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
Class clazz = beanDefinition.getBeanClass();
try {
if (null != ctor) {
return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
} else {
return clazz.getDeclaredConstructor().newInstance();
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new BeansException("Failed to instantiate [" + clazz.getName() + "]", e);
}
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/support/SimpleInstantiationStrategy.java
|
Java
|
apache-2.0
| 1,109
|
package cn.bugstack.springframework.beans.factory.xml;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanReference;
import cn.bugstack.springframework.beans.factory.support.AbstractBeanDefinitionReader;
import cn.bugstack.springframework.beans.factory.support.BeanDefinitionRegistry;
import cn.bugstack.springframework.core.io.Resource;
import cn.bugstack.springframework.core.io.ResourceLoader;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.IOException;
import java.io.InputStream;
/**
* Bean definition reader for XML bean definitions.
* <p>
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
super(registry);
}
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {
super(registry, resourceLoader);
}
@Override
public void loadBeanDefinitions(Resource resource) throws BeansException {
try {
try (InputStream inputStream = resource.getInputStream()) {
doLoadBeanDefinitions(inputStream);
}
} catch (IOException | ClassNotFoundException e) {
throw new BeansException("IOException parsing XML document from " + resource, e);
}
}
@Override
public void loadBeanDefinitions(Resource... resources) throws BeansException {
for (Resource resource : resources) {
loadBeanDefinitions(resource);
}
}
@Override
public void loadBeanDefinitions(String location) throws BeansException {
ResourceLoader resourceLoader = getResourceLoader();
Resource resource = resourceLoader.getResource(location);
loadBeanDefinitions(resource);
}
@Override
public void loadBeanDefinitions(String... locations) throws BeansException {
for (String location : locations) {
loadBeanDefinitions(location);
}
}
protected void doLoadBeanDefinitions(InputStream inputStream) throws ClassNotFoundException {
Document doc = XmlUtil.readXML(inputStream);
Element root = doc.getDocumentElement();
NodeList childNodes = root.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
// 判断元素
if (!(childNodes.item(i) instanceof Element)) continue;
// 判断对象
if (!"bean".equals(childNodes.item(i).getNodeName())) continue;
// 解析标签
Element bean = (Element) childNodes.item(i);
String id = bean.getAttribute("id");
String name = bean.getAttribute("name");
String className = bean.getAttribute("class");
// 获取 Class,方便获取类中的名称
Class<?> clazz = Class.forName(className);
// 优先级 id > name
String beanName = StrUtil.isNotEmpty(id) ? id : name;
if (StrUtil.isEmpty(beanName)) {
beanName = StrUtil.lowerFirst(clazz.getSimpleName());
}
// 定义Bean
BeanDefinition beanDefinition = new BeanDefinition(clazz);
// 读取属性并填充
for (int j = 0; j < bean.getChildNodes().getLength(); j++) {
if (!(bean.getChildNodes().item(j) instanceof Element)) continue;
if (!"property".equals(bean.getChildNodes().item(j).getNodeName())) continue;
// 解析标签:property
Element property = (Element) bean.getChildNodes().item(j);
String attrName = property.getAttribute("name");
String attrValue = property.getAttribute("value");
String attrRef = property.getAttribute("ref");
// 获取属性值:引入对象、值对象
Object value = StrUtil.isNotEmpty(attrRef) ? new BeanReference(attrRef) : attrValue;
// 创建属性信息
PropertyValue propertyValue = new PropertyValue(attrName, value);
beanDefinition.getPropertyValues().addPropertyValue(propertyValue);
}
if (getRegistry().containsBeanDefinition(beanName)) {
throw new BeansException("Duplicate beanName[" + beanName + "] is not allowed");
}
// 注册 BeanDefinition
getRegistry().registerBeanDefinition(beanName, beanDefinition);
}
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/beans/factory/xml/XmlBeanDefinitionReader.java
|
Java
|
apache-2.0
| 4,849
|
package cn.bugstack.springframework.context;
import cn.bugstack.springframework.beans.factory.ListableBeanFactory;
/**
* Central interface to provide configuration for an application.
* This is read-only while the application is running, but may be
* reloaded if the implementation supports this.
*
* 应用上下文
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface ApplicationContext extends ListableBeanFactory {
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/context/ApplicationContext.java
|
Java
|
apache-2.0
| 475
|
package cn.bugstack.springframework.context;
import cn.bugstack.springframework.beans.BeansException;
/**
* SPI interface to be implemented by most if not all application contexts.
* Provides facilities to configure an application context in addition
* to the application context client methods in the
* {@link cn.bugstack.springframework.context.ApplicationContext} interface.
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public interface ConfigurableApplicationContext extends ApplicationContext {
/**
* 刷新容器
*
* @throws BeansException
*/
void refresh() throws BeansException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/context/ConfigurableApplicationContext.java
|
Java
|
apache-2.0
| 663
|
package cn.bugstack.springframework.context.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.ConfigurableListableBeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanFactoryPostProcessor;
import cn.bugstack.springframework.beans.factory.config.BeanPostProcessor;
import cn.bugstack.springframework.context.ConfigurableApplicationContext;
import cn.bugstack.springframework.core.io.DefaultResourceLoader;
import java.util.Map;
/**
* Abstract implementation of the {@link cn.bugstack.springframework.context.ApplicationContext}
* interface. Doesn't mandate the type of storage used for configuration; simply
* implements common context functionality. Uses the Template Method design pattern,
* requiring concrete subclasses to implement abstract methods.
* <p>
* 抽象应用上下文
* <p>
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {
@Override
public void refresh() throws BeansException {
// 1. 创建 BeanFactory,并加载 BeanDefinition
refreshBeanFactory();
// 2. 获取 BeanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
// 3. 在 Bean 实例化之前,执行 BeanFactoryPostProcessor (Invoke factory processors registered as beans in the context.)
invokeBeanFactoryPostProcessors(beanFactory);
// 4. BeanPostProcessor 需要提前于其他 Bean 对象实例化之前执行注册操作
registerBeanPostProcessors(beanFactory);
// 5. 提前实例化单例Bean对象
beanFactory.preInstantiateSingletons();
}
protected abstract void refreshBeanFactory() throws BeansException;
protected abstract ConfigurableListableBeanFactory getBeanFactory();
private void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
Map<String, BeanFactoryPostProcessor> beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
for (BeanFactoryPostProcessor beanFactoryPostProcessor : beanFactoryPostProcessorMap.values()) {
beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
}
}
private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
Map<String, BeanPostProcessor> beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class);
for (BeanPostProcessor beanPostProcessor : beanPostProcessorMap.values()) {
beanFactory.addBeanPostProcessor(beanPostProcessor);
}
}
@Override
public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
return getBeanFactory().getBeansOfType(type);
}
@Override
public String[] getBeanDefinitionNames() {
return getBeanFactory().getBeanDefinitionNames();
}
@Override
public Object getBean(String name) throws BeansException {
return getBeanFactory().getBean(name);
}
@Override
public Object getBean(String name, Object... args) throws BeansException {
return getBeanFactory().getBean(name, args);
}
@Override
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return getBeanFactory().getBean(name, requiredType);
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/context/support/AbstractApplicationContext.java
|
Java
|
apache-2.0
| 3,477
|
package cn.bugstack.springframework.context.support;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.ConfigurableListableBeanFactory;
import cn.bugstack.springframework.beans.factory.support.DefaultListableBeanFactory;
/**
* Base class for {@link cn.bugstack.springframework.context.ApplicationContext}
* implementations which are supposed to support multiple calls to {@link #refresh()},
* creating a new internal bean factory instance every time.
* Typically (but not necessarily), such a context will be driven by
* a set of config locations to load bean definitions from.
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
private DefaultListableBeanFactory beanFactory;
@Override
protected void refreshBeanFactory() throws BeansException {
DefaultListableBeanFactory beanFactory = createBeanFactory();
loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;
}
private DefaultListableBeanFactory createBeanFactory() {
return new DefaultListableBeanFactory();
}
protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);
@Override
protected ConfigurableListableBeanFactory getBeanFactory() {
return beanFactory;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/context/support/AbstractRefreshableApplicationContext.java
|
Java
|
apache-2.0
| 1,435
|
package cn.bugstack.springframework.context.support;
import cn.bugstack.springframework.beans.factory.support.DefaultListableBeanFactory;
import cn.bugstack.springframework.beans.factory.xml.XmlBeanDefinitionReader;
/**
* Convenient base class for {@link cn.bugstack.springframework.context.ApplicationContext}
* implementations, drawing configuration from XML documents containing bean definitions
* understood by an {@link cn.bugstack.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
*
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public abstract class AbstractXmlApplicationContext extends AbstractRefreshableApplicationContext {
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory, this);
String[] configLocations = getConfigLocations();
if (null != configLocations){
beanDefinitionReader.loadBeanDefinitions(configLocations);
}
}
protected abstract String[] getConfigLocations();
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/context/support/AbstractXmlApplicationContext.java
|
Java
|
apache-2.0
| 1,124
|
package cn.bugstack.springframework.context.support;
import cn.bugstack.springframework.beans.BeansException;
/**
* Standalone XML application context, taking the context definition files
* from the class path, interpreting plain paths as class path resource names
* that include the package path (e.g. "mypackage/myresource.txt"). Useful for
* test harnesses as well as for application contexts embedded within JARs.
* <p>
* XML 文件应用上下文
* <p>
*
*
*
*
*
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
private String[] configLocations;
public ClassPathXmlApplicationContext() {
}
/**
* 从 XML 中加载 BeanDefinition,并刷新上下文
*
* @param configLocations
* @throws BeansException
*/
public ClassPathXmlApplicationContext(String configLocations) throws BeansException {
this(new String[]{configLocations});
}
/**
* 从 XML 中加载 BeanDefinition,并刷新上下文
* @param configLocations
* @throws BeansException
*/
public ClassPathXmlApplicationContext(String[] configLocations) throws BeansException {
this.configLocations = configLocations;
refresh();
}
@Override
protected String[] getConfigLocations() {
return configLocations;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/context/support/ClassPathXmlApplicationContext.java
|
Java
|
apache-2.0
| 1,414
|
package cn.bugstack.springframework.core.io;
import cn.bugstack.springframework.util.ClassUtils;
import cn.hutool.core.lang.Assert;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class ClassPathResource implements Resource {
private final String path;
private ClassLoader classLoader;
public ClassPathResource(String path) {
this(path, (ClassLoader) null);
}
public ClassPathResource(String path, ClassLoader classLoader) {
Assert.notNull(path, "Path must not be null");
this.path = path;
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
@Override
public InputStream getInputStream() throws IOException {
InputStream is = classLoader.getResourceAsStream(path);
if (is == null) {
throw new FileNotFoundException(
this.path + " cannot be opened because it does not exist");
}
return is;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/core/io/ClassPathResource.java
|
Java
|
apache-2.0
| 1,029
|
package cn.bugstack.springframework.core.io;
import cn.hutool.core.lang.Assert;
import java.net.MalformedURLException;
import java.net.URL;
public class DefaultResourceLoader implements ResourceLoader {
@Override
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
if (location.startsWith(CLASSPATH_URL_PREFIX)) {
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
}
else {
try {
URL url = new URL(location);
return new UrlResource(url);
} catch (MalformedURLException e) {
return new FileSystemResource(location);
}
}
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/core/io/DefaultResourceLoader.java
|
Java
|
apache-2.0
| 756
|
package cn.bugstack.springframework.core.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileSystemResource implements Resource {
private final File file;
private final String path;
public FileSystemResource(File file) {
this.file = file;
this.path = file.getPath();
}
public FileSystemResource(String path) {
this.file = new File(path);
this.path = path;
}
@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(this.file);
}
public final String getPath() {
return this.path;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/core/io/FileSystemResource.java
|
Java
|
apache-2.0
| 699
|
package cn.bugstack.springframework.core.io;
import java.io.IOException;
import java.io.InputStream;
public interface Resource {
InputStream getInputStream() throws IOException;
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/core/io/Resource.java
|
Java
|
apache-2.0
| 188
|
package cn.bugstack.springframework.core.io;
public interface ResourceLoader {
/**
* Pseudo URL prefix for loading from the class path: "classpath:"
*/
String CLASSPATH_URL_PREFIX = "classpath:";
Resource getResource(String location);
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/core/io/ResourceLoader.java
|
Java
|
apache-2.0
| 263
|
package cn.bugstack.springframework.core.io;
import cn.hutool.core.lang.Assert;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class UrlResource implements Resource{
private final URL url;
public UrlResource(URL url) {
Assert.notNull(url,"URL must not be null");
this.url = url;
}
@Override
public InputStream getInputStream() throws IOException {
URLConnection con = this.url.openConnection();
try {
return con.getInputStream();
}
catch (IOException ex){
if (con instanceof HttpURLConnection){
((HttpURLConnection) con).disconnect();
}
throw ex;
}
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/core/io/UrlResource.java
|
Java
|
apache-2.0
| 807
|
package cn.bugstack.springframework.util;
public class ClassUtils {
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back to system class loader...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
}
return cl;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/main/java/cn/bugstack/springframework/util/ClassUtils.java
|
Java
|
apache-2.0
| 581
|
package cn.bugstack.springframework.test.bean;
import java.util.HashMap;
import java.util.Map;
public class UserDao {
private static Map<String, String> hashMap = new HashMap<>();
static {
hashMap.put("10001", "小傅哥");
hashMap.put("10002", "八杯水");
hashMap.put("10003", "阿毛");
}
public String queryUserName(String uId) {
return hashMap.get(uId);
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/test/java/cn/bugstack/springframework/test/bean/UserDao.java
|
Java
|
apache-2.0
| 422
|
package cn.bugstack.springframework.test.bean;
/**
* 作者:DerekYRC https://github.com/DerekYRC/mini-spring
*/
public class UserService {
private String uId;
private String company;
private String location;
private UserDao userDao;
public String queryUserInfo() {
return userDao.queryUserName(uId) + "," + company + "," + location;
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/test/java/cn/bugstack/springframework/test/bean/UserService.java
|
Java
|
apache-2.0
| 952
|
package cn.bugstack.springframework.test.common;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.PropertyValues;
import cn.bugstack.springframework.beans.factory.ConfigurableListableBeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanFactoryPostProcessor;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userService");
PropertyValues propertyValues = beanDefinition.getPropertyValues();
propertyValues.addPropertyValue(new PropertyValue("company", "改为:字节跳动"));
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/test/java/cn/bugstack/springframework/test/common/MyBeanFactoryPostProcessor.java
|
Java
|
apache-2.0
| 927
|
package cn.bugstack.springframework.test.common;
import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanPostProcessor;
import cn.bugstack.springframework.test.bean.UserService;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("userService".equals(beanName)) {
UserService userService = (UserService) bean;
userService.setLocation("改为:北京");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
|
2302_77879529/spring
|
small-spring-step-06/src/test/java/cn/bugstack/springframework/test/common/MyBeanPostProcessor.java
|
Java
|
apache-2.0
| 769
|