用Spring的注解,注入一个有属性id和name的Teacher实例,并将自己所在的学校装配到Teacher中。

主要创建的程序如图:

Teacher类:
public class Teacher {
    private int id;
    private String name;
    private School school;

    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }

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

    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public School getSchool() {
        return school;
    }
}
//最后,通过调用 getter 方法获取 Teacher 的属性,并输出到控制台。

School类:

public class School {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}


TeacherService类:

@Service
public class TeacherService {
    @Autowired
    private Teacher teacher;

    @Autowired
    private School school;

    public void setTeacherId(int id) {
        teacher.setId(id);
    }

    public void setTeacherName(String name) {
        teacher.setName(name);
    }

    public void setTeacherSchool() {
        teacher.setSchool(school);
    }

    public Teacher getTeacher() {
        return teacher;
    }
}


AppConfig类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
//    使用 @Bean 注解来创建 Teacher 和 School 的实例
    @Bean
    public Teacher teacher() {
        return new Teacher();
    }
    @Bean
    public School school() {
        School school = new School();
        school.setName("深圳");
        return school;
    }
}

测试Main类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        TeacherService teacherService = context.getBean(TeacherService.class);
//使用 @Autowired 注解来注入 Teacher 和 School 的实例。
        teacherService.setTeacherId(1);
        teacherService.setTeacherName("小米");
        teacherService.setTeacherSchool();

        Teacher teacher = teacherService.getTeacher();
        System.out.println( teacher.getId()+ teacher.getName()+"is from" + teacher.getSchool().getName());
//        System.out.println("Teacher name: " + teacher.getName());
//        System.out.println("Teacher school: " + teacher.getSchool().getName());
    }
}
//在 Main 类中,创建一个 Spring 应用程序上下文,并使用 AppConfig 类来配置上下文。

最后的运行结果:

大学生的个人作业,如果大佬看到不完善的地方也可以指出,请不要喷。

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值