项目目录结构如下:

把上节的基于XML的Bean定义,改写为注解方式,实现步骤如下:
1.编写Person与Car实体类
在Person类的car属性上添加注解实现自动注入。实现方式有以下两种: 1)@Autowired+@Qualifier 2)@Resource(name="xxx") 或者属性名字和bean的name保持一致。
注意:@Autowired是spring提供的注释,@Resource是JAVA2EE提供的。使用@Resource注解需要添加annotation 依赖。在pom.xml中添加以下依赖:
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
package com.entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import javax.annotation.Resource;
public class Person {
private String name;
private String gender;
private int age;
/*
方式一:@Autowired+@Qualifier
@Autowired
@Qualifier("car2")
*/
//方式二:@Resource(name="xxx") 或者属性名字和bean的name保持一致。
//如果使用@Resource,要求属性名字改为car2;
@Resource(name="car2")
private Car car;
public Person() {
}
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", car=" + car +
'}';
}
}
package com.entity;
public class Car {
private String brand;
private String color;
public Car() {
}
public Car(String brand, String color) {
this.brand = brand;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
'}';
}
}
2.在SpringConfiguration类中定义bean. 这里我们定义一个Person类型的bean和两个Car类型的bean.通过注解的方式实现person bean对象的car属性注入的是car2对象。
package com.config;
import com.entity.Car;
import com.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com")
public class SpringConfiguration {
@Bean(name = {"p1","person"})
public Person getPerson(){
return new Person("zhangsan","男",20);
}
@Bean("car1")
public Car getCar1(){
return new Car("BMW","black");
}
@Bean("car2")
public Car getCar2(){
return new Car("BENZ","red");
}
}
3.编写测试类
package com.test;
import com.config.SpringConfiguration;
import com.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
Person person = (Person) context.getBean("p1");
System.out.println(person);
}
}
4.测试运行

@Autowired与 @Resource区别