spring-4-注解方式生成bean

2020-06-22  本文已影响0人  blank_white
package com.spyouth.learner;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

// Student student8 = new Student();
@Component(value = "student8")
public class Student {

    //为属性赋值
    @Value("注解8")
    public String name;
    @Value("28")
    public int age;


    // 通过名称赋值
    @Autowired
    @Qualifier(value = "school")
    public School school;


//    @Autowired   //autoWrite byType
//    public School school;



    public Student() {
    }

    public Student(String name, int age, School school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

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

    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;
    }

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }
}

上一篇 下一篇

猜你喜欢

热点阅读