org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义名为“ instance”的bean

时间:2018-07-13 11:36:31

标签: java spring maven

我有一个使用其他应用程序罐子的应用程序。我的名为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.java内容

...@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属性时无法解析对bean'instance'的引用'hazelcastInstance';嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义名为'instance'的bean

有人帮助吗?

0 个答案:

没有答案