Skip to content

Commit

Permalink
为bean填充属性
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekYRC committed Nov 24, 2020
1 parent c198cda commit 1e2efce
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 96 deletions.
51 changes: 10 additions & 41 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,49 +84,18 @@ class HelloService {
测试:BeanFactoryTest
```
@Test
public void testBeanFactory() throws Exception {
public void testPopulateBeanWithPropertyValues() throws Exception {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
PropertyValues propertyValues = new PropertyValues();
propertyValues.addPropertyValue(new PropertyValue("foo", "hello"));
propertyValues.addPropertyValue(new PropertyValue("bar", "world"));
BeanDefinition beanDefinition = new BeanDefinition(HelloService.class, propertyValues);
beanFactory.registerBeanDefinition("helloService", beanDefinition);
HelloService helloService = (HelloService) beanFactory.getBean("helloService");
System.out.println(helloService.toString());
assertThat(helloService.getFoo()).isEqualTo("hello");
assertThat(helloService.getBar()).isEqualTo("world");
}
public class HelloService {
private String foo;
private String bar;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
@Override
public String toString() {
return "HelloService{" +
"foo='" + foo + '\'' +
", bar='" + bar + '\'' +
'}';
}
propertyValues.addPropertyValue(new PropertyValue("name", "derek"));
propertyValues.addPropertyValue(new PropertyValue("age", 18));
BeanDefinition beanDefinition = new BeanDefinition(Person.class, propertyValues);
beanFactory.registerBeanDefinition("person", beanDefinition);
Person person = (Person) beanFactory.getBean("person");
System.out.println(person);
assertThat(person.getName()).isEqualTo("derek");
assertThat(person.getAge()).isEqualTo(18);
}
```

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
</properties>

<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/springframework/beans/PropertyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class PropertyValue {

private final String name;

private final String value;
private final Object value;

public PropertyValue(String name, String value) {
public PropertyValue(String name, Object value) {
this.name = name;
this.value = value;
}
Expand All @@ -21,7 +21,7 @@ public String getName() {
return name;
}

public String getValue() {
public Object getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public PropertyValue[] getPropertyValues() {

public PropertyValue getPropertyValue(String propertyName) {
for (int i = 0; i < this.propertyValueList.size(); i++) {
PropertyValue pv = (PropertyValue) this.propertyValueList.get(i);
PropertyValue pv = this.propertyValueList.get(i);
if (pv.getName().equals(propertyName)) {
return pv;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.beans.PropertyValues;

/**
* BeanDefinition实例保存bean的信息,包括class类型、方法构造参数、是否为单例等,此处简化只包含class类型
* BeanDefinition实例保存bean的信息,包括class类型、方法构造参数、bean属性、bean的scope等,此处简化只包含class类型和bean属性
*
* @author derekyi
* @date 2020/11/22
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.springframework.beans.factory.support;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.PropertyValues;
Expand Down Expand Up @@ -35,6 +36,16 @@ protected Object doCreateBean(String beanName, BeanDefinition beanDefinition) {
return bean;
}

/**
* 实例化bean
*
* @param beanDefinition
* @return
*/
protected Object createBeanInstance(BeanDefinition beanDefinition) {
return getInstantiationStrategy().instantiate(beanDefinition);
}

/**
* 为bean填充属性
*
Expand All @@ -43,29 +54,18 @@ protected Object doCreateBean(String beanName, BeanDefinition beanDefinition) {
*/
protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
try {
Class beanClass = beanDefinition.getBeanClass();


for (PropertyValue propertyValue : beanDefinition.getPropertyValues().getPropertyValues()) {
String name = propertyValue.getName();
String value = propertyValue.getValue();
Object value = propertyValue.getValue();

//通过属性的set方法设置属性
Class<?> type = beanClass.getDeclaredField(name).getType();
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method method = beanClass.getDeclaredMethod(methodName, new Class[]{type});
method.invoke(bean, new Object[]{value});
//通过反射设置属性
BeanUtil.setFieldValue(bean, name, value);
}
} catch (Exception ex) {
throw new BeansException("Error setting property values for bean: " + beanName, ex);
}
}


protected Object createBeanInstance(BeanDefinition beanDefinition) {
return getInstantiationStrategy().instantiate(beanDefinition);
}

public InstantiationStrategy getInstantiationStrategy() {
return instantiationStrategy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,29 @@
public class BeanFactoryTest {

@Test
public void testBeanFactory() throws Exception {
public void testGetBean() throws Exception {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
PropertyValues propertyValues = new PropertyValues();
propertyValues.addPropertyValue(new PropertyValue("foo", "hello"));
propertyValues.addPropertyValue(new PropertyValue("bar", "world"));
BeanDefinition beanDefinition = new BeanDefinition(HelloService.class, propertyValues);
BeanDefinition beanDefinition = new BeanDefinition(HelloService.class);
beanFactory.registerBeanDefinition("helloService", beanDefinition);

HelloService helloService = (HelloService) beanFactory.getBean("helloService");
System.out.println(helloService.toString());
assertThat(helloService.getFoo()).isEqualTo("hello");
assertThat(helloService.getBar()).isEqualTo("world");
assertThat(helloService).isNotNull();
helloService.sayHello();
}

@Test
public void testPopulateBeanWithPropertyValues() throws Exception {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
PropertyValues propertyValues = new PropertyValues();
propertyValues.addPropertyValue(new PropertyValue("name", "derek"));
propertyValues.addPropertyValue(new PropertyValue("age", 18));
BeanDefinition beanDefinition = new BeanDefinition(Person.class, propertyValues);
beanFactory.registerBeanDefinition("person", beanDefinition);

Person person = (Person) beanFactory.getBean("person");
System.out.println(person);
assertThat(person.getName()).isEqualTo("derek");
assertThat(person.getAge()).isEqualTo(18);
}
}

32 changes: 4 additions & 28 deletions src/test/java/org/springframework/beans/factory/HelloService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,8 @@
* @date 2020/11/22
*/
public class HelloService {

private String foo;

private String bar;

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}

@Override
public String toString() {
return "HelloService{" +
"foo='" + foo + '\'' +
", bar='" + bar + '\'' +
'}';
public String sayHello() {
System.out.println("hello");
return "hello";
}
}
}
36 changes: 36 additions & 0 deletions src/test/java/org/springframework/beans/factory/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.springframework.beans.factory;

/**
* @author derekyi
* @date 2020/11/24
*/
public class Person {

private String name;

private int age;

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

0 comments on commit 1e2efce

Please sign in to comment.