@Autowired 注解是通过 java config 进行 bean 的自动装配
    在使用 @Autowired 的时候,需要在xml配置文件中添加这段代码

    1. <!-- 该 BeanPostProcessor 将自动对标注@Autowired 的 Bean 进行注入 -->
    2. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    案例代码:
    Person.java

    1. package com.baklib.custom;
    2. public class Person {
    3. private String name;
    4. private int age;
    5. public Person() {
    6. }
    7. public Person(String name, int age) {
    8. this.name = name;
    9. this.age = age;
    10. }
    11. public String getName() {
    12. return name;
    13. }
    14. public void setName(String name) {
    15. this.name = name;
    16. }
    17. public int getAge() {
    18. return age;
    19. }
    20. public void setAge(int age) {
    21. this.age = age;
    22. }
    23. }

    Car.java

    1. package com.baklib.custom;
    2. public class Car {
    3. private float price;
    4. private String name;
    5. public Car() {
    6. }
    7. public Car(float price, String name) {
    8. this.price = price;
    9. this.name = name;
    10. }
    11. public float getPrice() {
    12. return price;
    13. }
    14. public void setPrice(float price) {
    15. this.price = price;
    16. }
    17. public String getName() {
    18. return name;
    19. }
    20. public void setName(String name) {
    21. this.name = name;
    22. }
    23. }

    Action.java

    1. package com.baklib.custom;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. public class Action {
    4. // 成员变量的自动装配
    5. @Autowired
    6. private Person person;
    7. @Autowired
    8. private Car car;
    9. public Action() {
    10. }
    11. // 构造方法的自动装配
    12. @Autowired
    13. public Action(Person person, Car car) {
    14. this.person = person;
    15. this.car = car;
    16. }
    17. public void buyCar() {
    18. System.out.println(person.getName() + "今年" + person.getAge() + "岁,花了"
    19. + car.getPrice() + "元买了" + car.getName());
    20. }
    21. }

    MainApp.java

    1. package com.baklib.custom;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. public class MainApp {
    5. public static void main(String[] args) {
    6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    7. Action action = (Action) context.getBean("action");
    8. action.buyCar();
    9. }
    10. }

    配置文件 Beans.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5. <bean id="person" class="com.baklib.custom.Person" >
    6. <property name="name" value="张三" />
    7. <property name="age" value="24" />
    8. </bean>
    9. <bean id="car" class="com.baklib.custom.Car">
    10. <property name="name" value="小轿车" />
    11. <property name="price" value="100000.0" />
    12. </bean>
    13. <bean id="action" class="com.baklib.custom.Action" />
    14. <!-- 该 BeanPostProcessor 将自动对标注@Autowired 的 Bean 进行注入 -->
    15. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    16. </beans>

    编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息

    1. 张三今年24岁,花了100000.0元买了小轿车