可以javaspringbean factoryreturnbean列表吗?

Can java spring bean factory return bean list?

我是 java spring 框架的新手。

我想知道 java spring 中的 bean 工厂是否可以 return bean 列表(例如 List<Car>

我对此提出质疑,因为我收到此 No matching factory method found: factory method 'makeCar' 错误消息,但 Factory class

中有 makeCar 函数

我的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>

我的工厂class

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;
    }
}

获取bean的代码

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();
        }
    }
}

完整堆栈跟踪

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.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.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'

要正确创建汽车 bean,您只需将应用程序中的 "property" 标记替换为 "contructor-arg" 标记即可 xml 所有 "makeCar" 调用。像这样:

<bean id="employee1Car" class="myPackage.CarFactory" factory-method="makeCar">
    <constructor-arg>
        <bean id="name1" class="java.util.ArrayList">
            <constructor-arg>
                <list>
                    <value>sonata</value>
                    <value>k9</value>
                </list>
            </constructor-arg>
        </bean>
    </constructor-arg>
    <constructor-arg>
        <bean id="year1" class="java.util.ArrayList">
            <constructor-arg>
                <list>
                    <value>2015</value>
                    <value>2017</value>
                </list>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>

您的应用程序中还有一些错误,即您的工厂方法应该 return SpecificCar 而不是 List。

在 EmployeeCarMain 中你获取了错误的 beans,我假设你想像这样获取它

Employee e = (Employee)context.getBean("employee" + i);

你的异常是这样开始的

Error creating bean with name 'employee1Car' defined

所以问题是您正在尝试获取一个注册为 employee1Car 的 Bean,但没有这样名称的 Bean

要获取所有已注册 bean 的列表,您可以使用 ClassPathXmlApplicationContext

假设您的 XML 文件名为 applicationContext.xml

你可以像这样获取所有已注册的Bean,开始调试你的bean是否已注册

public static void main(String[] args) {
    ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
    String[] beans = context.getBeanDefinitionNames();
        for (String bean : beans) {
            System.out.println(bean);
        }
}

使用下面的依赖项。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

它支持端点数量。 请访问下面link,

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html