public class Student { private String name; private String[] className; public Student() { } public Student(String name, String[] className) { this.name = name; this.className = className; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getClassName() { return className; } public void setClassName(String[] className) { this.className = className; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", className=" + Arrays.toString(className) + ‘}‘; } }
<bean id="student" class="com.orz.spring.bean.Student"> <property name="name" value="李华"/> <property name="className"> <array> <value>JAVA</value> <value>PHP</value> <value>PYTHON</value> </array> </property> </bean>
@Test
public void test1()
{
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
Student student = applicationContext.getBean("student", Student.class);
System.out.println(student);
}
Student{name=‘李华‘, className=[JAVA, PHP, PYTHON]}
Spring-IoC-DI-基于xml的依赖注入-使用set方法进行注入(案例五:注入数组属性)
原文:https://www.cnblogs.com/orzjiangxiaoyu/p/13836137.html