首页 > 解决方案 > org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“instance”的bean

问题描述

我有一个使用其他应用程序的 jar 的应用程序。我的应用程序称为com_Streams_H2O_ML.java使用HazelcastInsance.

package com.escomled.machinelearning.ml;

import static com.com.machinelearning.ml.PropertiesGenerator.getProperty;

import java.io.File;
import java.util.Properties;

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KStreamBuilder;
import org.springframework.stereotype.Repository;

import com.com.blackboard.Blackboard;
import com.com.machinelearning.ml.Escomled_Streams_H2O_ML;
import com.com.machinelearning.startmap.ContextHolder;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

import hex.genmodel.easy.EasyPredictModelWrapper;
import hex.genmodel.easy.RowData;
import hex.genmodel.easy.exception.PredictException;
import hex.genmodel.easy.prediction.RegressionModelPrediction;

/**
 * 
 * 
 * Creates a new Kafka Streams application for prediction of watts The
 * application uses the GBM model "comPOJO" (built with H2O.ai and python
 * application) to infer messages sent to Kafka topic "comInputTopic". The
 * outcome of model inference is sent to Kafka topic "comOutputTopic".
 *
 */

public class com_Streams_H2O_ML {

    // Name of the generated H2O model

    private static String modelClassName = getProperty("pojoFullName");

    // Prediction Value
    private static double watts = 0;

    public static boolean isNumeric(String str) {
        return str.matches("-?\\d+(\\.\\d+)?"); // match a number with optional '-' and decimal.
    }

    public static void main(final String[] args) throws Exception {
        System.out.println(new File(".").getAbsolutePath());
        // Create H2O object (see gbm_pojo_test.java)
        hex.genmodel.GenModel rawModel;

        rawModel = (hex.genmodel.GenModel) Class.forName(modelClassName).newInstance();

        EasyPredictModelWrapper model = new EasyPredictModelWrapper(
                new EasyPredictModelWrapper.Config().setModel(rawModel).setConvertUnknownCategoricalLevelsToNa(false));

        HazelcastInstance instance = ((Blackboard) ContextHolder.getContext().getBean("blackboard")).getHazelcastInstance();
        IMap<String, String> dimmingMap = instance.getMap("dim_board_map");

        // Configure Kafka Streams Application
        final String bootstrapServers = args.length > 0 ? args[0] : getProperty("bootstrapServers");
        final Properties streamsConfiguration = new Properties();
        // Give the Streams application a unique name. The name must be unique
        // in the Kafka cluster
        // against which the application is run.
        streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "Escomled_Streams_ML");
        // Where to find Kafka broker(s).
        streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);

        // Specify default (de)serializers for record keys and for record
        // values.
        streamsConfiguration.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
        streamsConfiguration.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());

        // For illustrative purposes we disable record caches
        streamsConfiguration.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);
        // In the subsequent lines we define the processing topology of the
        // Streams application.
        final KStreamBuilder builder = new KStreamBuilder();

        // Construct a `KStream` from the input topic "EscomledInputTopic", where
        // message values
        // represent lines of text (for the sake of this example, we ignore
        // whatever may be stored
        // in the message keys).
        String inputTopic = getProperty("inputTopic");
        final KStream<String, String> predictionInput = builder.stream(inputTopic);

        // Stream Processor (in this case 'foreach' to add custom logic, i.e. apply the
        // analytic model)
        predictionInput.foreach((key, value) -> {
            String[] values = value.split(",");
            System.out.println("Key " + key);
            System.out.println("Value " + value);
            if (value != null && !value.equals("") && isNumeric(value)) {
                RowData row = new RowData();
                row.put("Board Number", key);
                row.put("Dimming", value);

                RegressionModelPrediction p = null;
                try {
                    p = model.predictRegression(row);
                    watts = p.value;
                    System.out.println("Prediction for " + key + "/" + value + "% :" + watts + "W");
                } catch (PredictException e) {
                    e.printStackTrace();
                }
                dimmingMap.remove(key);
            } else {
                System.out.println("ENTER CORRECT VALUES");
            }
        });

        // Transform message: Add prediction information
        KStream<String, Object> transformedMessage = predictionInput.mapValues(value -> value + ", " + watts);

        // Send prediction information to Output Topic
        String outputTopic = getProperty("outputTopic");
        transformedMessage.to(outputTopic);

        // Start Kafka Streams Application to process new incoming messages from Input
        // Topic
        final KafkaStreams streams = new KafkaStreams(builder, streamsConfiguration);
        streams.cleanUp();
        streams.start();
        // Add shutdown hook to respond to SIGTERM and gracefully close Kafka
        // Streams
        Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
    }
}

我的appContext.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" xmlns:hz="http://www.hazelcast.com/schema/spring"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.hazelcast.com/schema/spring http://www.hazelcast.com/schema/spring/hazelcast-spring-3.2.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd ">

    <context:annotation-config />
    <context:component-scan base-package="com.escomled" />
    <context:component-scan base-package="com.escomled.*" />
    <import resource="classpath*:config/blackBoard.xml" />
    <task:annotation-driven />

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:///home/escomled/escomled_server/config/escomled.properties</value>
            </list>
        </property>
    </bean>

     <bean id="reportJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean> 

    <bean id="blackboard" class="com.escomled.blackboard.impl.BlackboardImpl">
        <property name="hazelcastInstance" ref="hazelcastClient" />
    </bean>
</beans>

我还有一个名为escomled_common包含com.escomled.blackboard.impl包的应用程序。在那个包中,我blackboard用一个名为BlackboardImpl.java BlackboardImpl.javacontent的类来实现

...@Component("blackboard")
public class BlackboardImpl implements Blackboard {

    private HazelcastInstance hazelcastInstance;

    @Override
    public HazelcastInstance getHazelcastInstance() {
    return hazelcastInstance;
    }

    public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
    this.hazelcastInstance = hazelcastInstance;
    }...

我收到一个错误

线程“主”org.springframework.beans.factory.BeanCreationException 中的异常:创建在类路径资源 [appContext.xml] 中定义的名称为“blackboard”的 bean 时出错:设置 bean 属性“hazelcastInstance”时无法解析对 bean“instance”的引用; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'instance' is defined

有人帮忙吗?

标签: javaspringmaven

解决方案


推荐阅读