Skip to content

Commit

Permalink
@Autowired注解
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekYRC committed Dec 27, 2020
1 parent 408a37a commit f90b1d2
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 4 deletions.
41 changes: 41 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -1075,11 +1075,52 @@ public class ValueAnnotationTest {
```

## @Autowired注解
> 分支:autowired-annotation
@Autowired注解的处理见AutowiredAnnotationBeanPostProcessor#postProcessPropertyValues

测试:
```
@Component
public class Car {
}
@Component
public class Person implements InitializingBean, DisposableBean {
@Autowired
private Car car;
}
```
autowired-annotation.xml
```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="org.springframework.test.bean"/>
</beans>
```
```
public class AutowiredAnnotationTest {
@Test
public void testAutowiredAnnotation() throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:autowired-annotation.xml");
Person person = applicationContext.getBean(Person.class);
assertThat(person.getCar()).isNotNull();
}
}
```



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public interface BeanFactory {
* @throws BeansException
*/
<T> T getBean(String name, Class<T> requiredType) throws BeansException;

<T> T getBean(Class<T> requiredType) throws BeansException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.springframework.beans.factory.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author derekyi
* @date 2020/12/27
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})
public @interface Autowired {

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,23 @@ public PropertyValues postProcessPropertyValues(PropertyValues pvs, Object bean,
}
}

//处理@Autowired注解(下一节实现)
//处理@Autowired注解
for (Field field : fields) {
Autowired autowiredAnnotation = field.getAnnotation(Autowired.class);
if (autowiredAnnotation != null) {
Class<?> fieldType = field.getType();
String dependentBeanName = null;
Qualifier qualifierAnnotation = field.getAnnotation(Qualifier.class);
Object dependentBean = null;
if (qualifierAnnotation != null) {
dependentBeanName = qualifierAnnotation.value();
dependentBean = beanFactory.getBean(dependentBeanName, fieldType);
} else {
dependentBean = beanFactory.getBean(fieldType);
}
BeanUtil.setFieldValue(bean, field.getName(), dependentBean);
}
}

return pvs;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.springframework.beans.factory.annotation;

import java.lang.annotation.*;

/**
* @author derekyi
* @date 2020/12/27
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Inherited
@Documented
public @interface Qualifier {

String value() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import org.springframework.beans.factory.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* @author derekyi
Expand Down Expand Up @@ -50,6 +48,22 @@ public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
return result;
}

public <T> T getBean(Class<T> requiredType) throws BeansException {
List<String> beanNames = new ArrayList<>();
for (Map.Entry<String, BeanDefinition> entry : beanDefinitionMap.entrySet()) {
Class beanClass = entry.getValue().getBeanClass();
if (requiredType.isAssignableFrom(beanClass)) {
beanNames.add(entry.getKey());
}
}
if (beanNames.size() == 1) {
return getBean(beanNames.get(0), requiredType);
}

throw new BeansException(requiredType + "expected single bean but found " +
beanNames.size() + ": " + beanNames);
}

@Override
public String[] getBeanDefinitionNames() {
Set<String> beanNames = beanDefinitionMap.keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
return getBeanFactory().getBeansOfType(type);
}

public <T> T getBean(Class<T> requiredType) throws BeansException {
return getBeanFactory().getBean(requiredType);
}

public Object getBean(String name) throws BeansException {
return getBeanFactory().getBean(name);
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/springframework/test/bean/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* @author derekyi
* @date 2020/11/24
*/
@Component
public class Person implements InitializingBean, DisposableBean {

private String name;

private int age;

@Autowired
private Car car;

public void customInitMethod() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.springframework.test.ioc;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.bean.Person;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author derekyi
* @date 2020/12/27
*/
public class AutowiredAnnotationTest {

@Test
public void testAutowiredAnnotation() throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:autowired-annotation.xml");

Person person = applicationContext.getBean(Person.class);
assertThat(person.getCar()).isNotNull();
}
}
16 changes: 16 additions & 0 deletions src/test/resources/autowired-annotation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<bean class="org.springframework.beans.factory.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:car.properties" />
</bean>

<context:component-scan base-package="org.springframework.test.bean"/>

</beans>

0 comments on commit f90b1d2

Please sign in to comment.