Java Spring bean Factory可以返回bean列表吗?

时间:2018-12-05 06:01:39

标签: java spring spring-boot javabeans factory

我是java spring框架的新手。

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

我在问这个问题,因为我收到了这个No matching factory method found: factory method 'makeCar'错误消息,但是Factory类中有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>

我的工厂班级

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

3 个答案:

答案 0 :(得分:1)

要正确地创建大豆,您只需在应用程序xml中为所有“ makeCar”调用将“ property”标签替换为“ contructor-arg”标签即可。像这样:

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

您的应用程序中还存在少量错误,即您的工厂方法应返回SpecificCar而不是List。

在EmployeeCarMain中,您正在获取错误的bean,我想您想像这样获取它

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

答案 1 :(得分:0)

您的异常开始是这样

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

答案 2 :(得分:0)

使用以下依赖项。

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

它支持许多端点。 请访问下面的链接,

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