Я новичок в фреймворке Java Spring.
Интересно, может ли фабрика бобов в Java Spring вернуть список бинов (например, List<Car>
)
Я спрашиваю об этом, потому что получилэто No matching factory method found: factory method 'makeCar'
сообщение об ошибке, но есть функция makeCar
в классе Factory
мой код XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employee1Car" class="myPackage.CarFactory" factory-method="makeCar">
<property name="modelList">
<bean id="name1" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>sonata</value>
<value>k9</value>
</list>
</constructor-arg>
</bean>
</property>
<property name="yearList">
<bean id="year1" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>2015</value>
<value>2017</value>
</list>
</constructor-arg>
</bean>
</property>
</bean>
<bean id="employee2Car" class="myPackage.CarFactory" factory-method="makeCar">
<property name="modelList">
<bean id="name2" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>genesis</value>
<value>camry</value>
</list>
</constructor-arg>
</bean>
</property>
<property name="yearList">
<bean id="year2" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>2013</value>
<value>2014</value>
</list>
</constructor-arg>
</bean>
</property>
</bean>
<bean id="employee3Car" class="myPackage.CarFactory" factory-method="makeCar">
<property name="modelList">
<bean id="name3" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>grandeur</value>
<value>carnival</value>
</list>
</constructor-arg>
</bean>
</property>
<property name="yearList">
<bean id="year3" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>2014</value>
<value>2018</value>
</list>
</constructor-arg>
</bean>
</property>
</bean>
<bean id="employee4Car" class="myPackage.CarFactory" factory-method="makeCar">
<property name="modelList">
<bean id="name4" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>prious</value>
<value>camry</value>
</list>
</constructor-arg>
</bean>
</property>
<property name="yearList">
<bean id="year4" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>2015</value>
<value>2017</value>
</list>
</constructor-arg>
</bean>
</property>
</bean>
<bean id="employee1" class="myPackage.Employee">
<property name="id" value="00301"/>
<property name="name" value="John"/>
<property name="car" ref="employee1Car"/>
</bean>
<bean id="employee2" class="myPackage.Employee">
<property name="id" value="00302"/>
<property name="name" value="James"/>
<property name="car" ref="employee2Car"/>
</bean>
<bean id="employee3" class="myPackage.Employee">
<constructor-arg value="00303"/>
<constructor-arg value="Ann"/>
<constructor-arg ref="employee3Car"/>
</bean>
<bean id="employee4" class="myPackage.Employee">
<constructor-arg value="00415"/>
<constructor-arg value="Mac"/>
<constructor-arg ref="employee4Car"/>
</bean>
</beans>
класс My Factory
package myPackage;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import myPackage.SpecificCar;
public class CarFactory {
static String[] Hyundai = new String[] {"sonata", "grandeur", "genesis", "avante", "i30", "i40", "tucson", "active", "salon", "i20", "veloster", "cona", "accent", "ionic", "maxcruz"};
static String[] Kia = new String[] {"mohave", "k3", "stonic", "spotage", "carnival", "niro", "k5", "stinger", "k9", "ray", "rio", "k7", "sorento", "soul", "morning"};
static String[] Toyota = new String[] {"avalon", "camry", "crown", "century", "prious", "mirai", "siena", "lave", "colora", "paso", "oris", "tank", "aqua", "estima", "landcruzer", "alion"};
public static List<SpecificCar> makeCar(List<String> modelList, List<Integer> yearList) {
List<String> h = Arrays.asList(Hyundai);
List<String> k = Arrays.asList(Kia);
List<String> t = Arrays.asList(Toyota);
List<SpecificCar> res = new ArrayList<SpecificCar>();
for(int i = 0; i < modelList.size(); i++) {
String maker = "";
if (k.contains(modelList.get(i).toLowerCase()))
maker = "Hyundai";
else if (k.contains(modelList.get(i).toLowerCase()))
maker = "Kia";
else if (t.contains(modelList.get(i).toLowerCase()))
maker = "Toyota";
res.add(new SpecificCar(modelList.get(i), yearList.get(i), maker));
}
return res;
}
}
Код для получения бина
package myPackage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class EmployeeCarMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
for(int i = 1; i < 6; i++) {
Employee e = (Employee)context.getBean("car" + i);
e.printInfo();
}
}
}
Full Stack Trace
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employee1Car' defined in class path resource [applicationContext.xml]: No matching factory method found: factory method 'makeCar'
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:417)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:903)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:817)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at myPackage.EmployeeCarMain.main(EmployeeCarMain.java:8)
Я хочу знать, почему я получил это сообщение об ошибке No matching factory method found: factory method 'makeCar'