1.写一个Student类,将类注册到spring容器中 2.学生类的要求:成员变量要有整型,字符串,数组,List集合,Map集合,Set集合 3.spring配置文件要求:配置文件名必须是<applicationContext.xml> 4.运行结果要求:要用Junit测试运行,运行结果要打印Student类中注入的所有属性值
时间: 2024-02-25 19:53:29 浏览: 74
好的,我知道了。以下是符合您要求的代码:
```java
// Student类
public class Student {
private int id;
private String name;
private int[] scores;
private List<String> courses;
private Map<String, Integer> courseScores;
private Set<String> hobbies;
// 构造方法
public Student(int id, String name, int[] scores, List<String> courses, Map<String, Integer> courseScores, Set<String> hobbies) {
this.id = id;
this.name = name;
this.scores = scores;
this.courses = courses;
this.courseScores = courseScores;
this.hobbies = hobbies;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int[] getScores() {
return scores;
}
public void setScores(int[] scores) {
this.scores = scores;
}
public List<String> getCourses() {
return courses;
}
public void setCourses(List<String> courses) {
this.courses = courses;
}
public Map<String, Integer> getCourseScores() {
return courseScores;
}
public void setCourseScores(Map<String, Integer> courseScores) {
this.courseScores = courseScores;
}
public Set<String> getHobbies() {
return hobbies;
}
public void setHobbies(Set<String> hobbies) {
this.hobbies = hobbies;
}
}
```
```xml
<!-- applicationContext.xml配置文件 -->
<beans>
<!-- 注册Student类 -->
<bean id="student" class="com.example.Student">
<!-- 注入属性值 -->
<constructor-arg value="1"/>
<constructor-arg value="张三"/>
<constructor-arg>
<array>
<value>80</value>
<value>90</value>
<value>85</value>
</array>
</constructor-arg>
<constructor-arg>
<list>
<value>语文</value>
<value>数学</value>
<value>英语</value>
</list>
</constructor-arg>
<constructor-arg>
<map>
<entry key="语文" value="80"/>
<entry key="数学" value="90"/>
<entry key="英语" value="85"/>
</map>
</constructor-arg>
<constructor-arg>
<set>
<value>篮球</value>
<value>游泳</value>
</set>
</constructor-arg>
</bean>
</beans>
```
```java
// Junit测试类
public class StudentTest {
// 注入Student对象
@Autowired
private Student student;
// 测试方法
@Test
public void test() {
// 打印属性值
System.out.println("学生ID:" + student.getId());
System.out.println("学生姓名:" + student.getName());
System.out.println("学生成绩:" + Arrays.toString(student.getScores()));
System.out.println("学生课程:" + student.getCourses());
System.out.println("学生成绩单:" + student.getCourseScores());
System.out.println("学生爱好:" + student.getHobbies());
}
}
```
请将以上代码保存为相应的文件后,运行Junit测试类即可看到运行结果,符合您的要求。
阅读全文