我正在使用spring尝试数组依赖,这是代码:
Car.java
package beans;
public class Car {
private String[] carnames;
private Engine[] engine;
public void setCarnames(String[] carnames) {
this.carnames = carnames;
}
public void setEngine(Engine[] engine) {
this.engine = engine;
}
public void showData(){
for(String car:carnames){
System.out.println(car);
}
for(Engine e:engine){
System.out.println(e.getModelyear());
}
}
}
Engine.java
package beans;
public class Engine {
private String[] modelyear;
public void setModelyear(String[] modelyear) {
this.modelyear = modelyear;
}
public String[] getModelyear() {
return modelyear;
}
}
spring.xml
<beans>
<bean id="e1" class="beans.Engine">
<property name="modelyear" value="2015"/>
</bean>
<bean id="e2" class="beans.Engine">
<property name="modelyear" value="2016"/>
</bean>
<bean id="e3" class="beans.Engine">
<property name="modelyear" value="2017"/>
</bean>
<bean id="c" class="beans.Car">
<property name="carnames">
<list>
<value> Audi</value>
<value> BMW</value>
<value> Mercedece</value>
</list>
</property>
<property name="engine">
<list>
<ref bean="e1"/>
<ref bean="e2"/>
<ref bean="e3"/>
</list>
</property>
</bean>
</beans>
主要方法
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import beans.Car;
public class Arry_DI_Main {
public static void main(String[] args) {
ApplicationContext ap= new ClassPathXmlApplicationContext("resource/spring.xml");
Car c=(Car) ap.getBean("c");
c.showData();
}
}
,输出就是这个
Audi
BMW
Mercedece
[Ljava.lang.String;@25bbe1b6
[Ljava.lang.String;@5702b3b1
[Ljava.lang.String;@69ea3742
为什么汽车名称是正确的但模型年份是打印一些字符串.. 它应该打印它的指定值,这是错误的吗?? ??