美文网首页
spring-4-注解方式生成bean

spring-4-注解方式生成bean

作者: blank_white | 来源:发表于2020-06-22 19:29 被阅读0次
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;
    }
}

相关文章

网友评论

      本文标题:spring-4-注解方式生成bean

      本文链接:https://www.haomeiwen.com/subject/cdpowhtx.html