作业一
标签(空格分隔): 未分类
Student
package com.niit.quickstart.bean;
import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;
@Configurationpublic class Student {
@Value("张三")
private String name;
@Value("#{20-2}")
private String age;
public String getName() {
return name;
}
public String getAge() {
return age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
Teacherpackage com.niit.quickstart.bean;
import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;
@Configurationpublic class Teacher {
@Value("许老师")
private String name;
@Value("Spring Boot")
private String subject;
public String getName() {
return name;
}
public String getSubject() {
return subject;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", subject='" + subject + '\'' +
'}';
}
}
Courserpackage com.niit.quickstart.bean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Componentpublic class Course { @Resource private Student student;
@Resource Teacher teacher;
@Override
public String toString() {
return "Course{" +
"student=" + student +
", teacher=" + teacher +
'}';
}
}
Applicationpackage com.niit.quickstart;
import com.niit.quickstart.bean.Course;import com.niit.quickstart.bean.Student;import com.niit.quickstart.bean.Teacher;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;
@SpringBootApplicationpublic class DemoApplication {
public static void main(String[] args) {
ApplicationContext ac = SpringApplication.run(DemoApplication.class,args);
System.out.println("使用Spring Boot开始注入Course的bean,它由Teacher和Student的bean装配而成:");
// Student student = (Student) ac.getBean("student");// Teacher teacher = (Teacher) ac.getBean("teacher");
Course course = (Course) ac.getBean("course");
System.out.println(course.toString());
}
}
网友评论