← 返回首页
Spring基础教程(七)
发表时间:2020-05-12 18:06:38
Bean定义(Annotation方式二)

在上节中我把要注入给属性的对象,配置在SpringConfiguration配置类中。本节我们使用@Component方式来定义bean对象。

项目目录结构:

实现步骤: 1.改写Car实体类 改写Car实体类,使用@Component定义bean.

package com.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component("car2")
public class Car {

    @Value("BENZ")
    private String brand;
    @Value("red")
    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 + '\'' +
                '}';
    }
}

属性的初值也可以写在setter方法上面。例如:

package com.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component("car2")
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;
    }

    @Value("BENZ")
    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }
    @Value("red")
    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}

注意:@Value注解无论是直接写属性上还是写在setter方法上,==本质都是在bean生命周期中属性注入阶段执行的==,而这个阶段一定是在构造方法之后执行,所以使用@Value赋的属性值会覆盖使用构造方法注入的属性值。

2.改写SpringConfiguration配置类

在配置类中我们仅保留Person类型的bean.

package com.config;

import com.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
@ComponentScan(basePackages = "com")
public class SpringConfiguration {

    @Bean(name = {"p1","person"})
    public Person getPerson(){
        return new Person("zhangsan","男",20);
    }

}

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.测试运行