Java @Autowire调用期间未实例化服务类

Java @Autowire调用期间未实例化服务类,java,spring,autowired,spring-boot,stomp,Java,Spring,Autowired,Spring Boot,Stomp,我试图在我的消息传递应用程序中使用@Service类,但是当我尝试从泛型类中使用时,该类没有通过@Autowire实例化。它仅在我使用控制器时被实例化 这是我的控制器: package hello; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; import org

我试图在我的消息传递应用程序中使用@Service类,但是当我尝试从泛型类中使用时,该类没有通过@Autowire实例化。它仅在我使用控制器时被实例化

这是我的控制器:

    package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import hello.Application;

@Controller
public class HelloController {


    @Autowired
    private MessageSender sender;

    @RequestMapping(value="/", method=RequestMethod.GET)
    public String index() {
        return "index";
    }

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        System.out.println("Sending message...");
        beginRoute(message.getName());
        sender.greet("thunder");
        return new Greeting("Hello, " + message.getName() + "!");
    }

    public void beginRoute(String message) {
        Application.startBody(message);
    }
}
以上对sender.greet的调用已成功

下面是我尝试在其中使用服务的另一个类:

package com.routing.integration;

import hello.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;


@Component
public class modifier {

    @Autowired
    private MessageSender sender;

    public boolean adder(String words) throws Exception {

        sender.greet(words);
    }
}
当我尝试像上面那样调用sender.greet时,我会得到一个NullPointerException,并且在调试时,我发现在调用sender.greet时sender为null。服务类是否未在修改器中实例化

最后,这里是MessageSender服务类:

@Service
public class MessageSender {

    @Autowired
    private SimpMessagingTemplate template;


    @RequestMapping(value="/hello", method=RequestMethod.POST)
    public void greet(String greeting) {
        Greeting text = new  Greeting("Goodbye, " + greeting + "!");
        this.template.convertAndSend("/topic/greetings", text);
    }

}
如何确保@Autowire在每个类中实例化MessageSender

编辑

这里是调用修饰符的地方。它由骆驼路线制成:

    <?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:camel="http://camel.apache.org/schema/spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:core="http://activemq.apache.org/schema/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context.xsd
    http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core">

<camelContext xmlns="http://camel.apache.org/schema/spring">

<route>
        <from uri="activemq:queue:testQSource"/>

        <choice>
            <when>
                <method ref="securityBean"/>
                <log message="Routing message from testQSource to testQDestination queue with data ${body}"/>

                <to uri="activemq:queue:testQDestination"/>
                <to uri="activationBean"/>
                <to uri="accountVerificationBean"/>
                <to uri="billingCheckingBean"/>
                <to uri="deviceConnectorBean"/>
                <to uri="deviceActivatorBean"/>
                <log message="Account activated: ${body}"/>
            </when>
            <otherwise>
                <log message="message went to stomp: ${body}"/>
            </otherwise>
        </choice>
</route>
</camelContext>


<camel:camelContext id="camel-client">
    <camel:template id="camelTemplate" />
</camel:camelContext>

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<bean id="browserBean" class="hello.HelloController"/>
<bean id="securityBean" class="com.routing.integration.modifier"/>
<bean id="activationBean" class="com.routing.integration.ActionApp"/>
<bean id="accountVerificationBean" class="com.routing.integration.AccountVerifier"/>
<bean id="billingCheckingBean" class="com.routing.integration.BillingChecker"/>
<bean id="deviceConnectorBean" class="com.routing.integration.DeviceConnector"/>
<bean id="deviceActivatorBean" class="com.routing.integration.DeviceActivator"/>

</beans>
编辑2

下面是我调用ComponentScan的主要类:

package hello;

import org.apache.camel.ProducerTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"hello", "com.routing.integration"})
public class Application {
    static ApplicationContext context = null;
    static ProducerTemplate camelTemplate = null;
    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
        System.out.println("----------------------\nSpringBootComplete\n----------------------");
        startBody("startup");
    }

    public static void startBody(String message) {
        if (context == null) {
            context = new ClassPathXmlApplicationContext("camelspring.xml");
            camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
        }
        if (message != "startup"){
            camelTemplate.sendBody("activemq:queue:testQSource", message);
        }
    }           
}

您的
应用程序
类应如下所示:

@Configuration
@ComponentScan(basePackages = {"hello", "com.routing.integration"})
@EnableAutoConfiguration
@ImportResource("classpath:camel.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
其中
camel.xml
是包含camel定义的xml配置文件。 如果您需要xml配置中定义的任何bean,如xml中的
camelTemplate
,您可以像这样获得它的引用,例如:

package hello;

import javax.annotation.Resource;

import org.apache.camel.ProducerTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Resource(name="camelTemplate")
    private ProducerTemplate template;

    @RequestMapping("/")
    String home() {
        System.out.println(template);
        return "Hello World!";
    }
}
正如我在评论中所说,不要手动创建应用程序上下文。向
@Component
@Service
@Configuration
@Bean
等注册所需的Bean。如果需要难以在Java代码中复制的xml配置,请使用
@ImportResource

在您拥有的xml配置文件中,您可以使用放置在
hello.HelloController
类上的
@Controller
注释替换
browserBean
bean

您的
activemq
bean可以在Java配置中转换为如下内容:

@Configuration
public class Config {

    @Bean
    public ActiveMQComponent activemq() {
        ActiveMQComponent comp = new ActiveMQComponent();
        comp.setBrokerUrl("tcp://localhost:61616");

        return comp;
    }
}

您的
应用程序
类应如下所示:

@Configuration
@ComponentScan(basePackages = {"hello", "com.routing.integration"})
@EnableAutoConfiguration
@ImportResource("classpath:camel.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
其中
camel.xml
是包含camel定义的xml配置文件。 如果您需要xml配置中定义的任何bean,如xml中的
camelTemplate
,您可以像这样获得它的引用,例如:

package hello;

import javax.annotation.Resource;

import org.apache.camel.ProducerTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Resource(name="camelTemplate")
    private ProducerTemplate template;

    @RequestMapping("/")
    String home() {
        System.out.println(template);
        return "Hello World!";
    }
}
正如我在评论中所说,不要手动创建应用程序上下文。向
@Component
@Service
@Configuration
@Bean
等注册所需的Bean。如果需要难以在Java代码中复制的xml配置,请使用
@ImportResource

在您拥有的xml配置文件中,您可以使用放置在
hello.HelloController
类上的
@Controller
注释替换
browserBean
bean

您的
activemq
bean可以在Java配置中转换为如下内容:

@Configuration
public class Config {

    @Bean
    public ActiveMQComponent activemq() {
        ActiveMQComponent comp = new ActiveMQComponent();
        comp.setBrokerUrl("tcp://localhost:61616");

        return comp;
    }
}

组件扫描中是否包含包com.routing.integration?我看到
modifier
类的包与其他包不同。另外,请不要这样命名您的类…您是否在某处使用
new modifier()
?感谢所有反馈!我做了一个编辑,显示了调用修改器的位置。它通过驼峰路由,并由路由中的bean实例化@迪莫尼,关于类名的事很抱歉。。。如何在组件扫描中包括com.routing.integration@chrylis,我不认为我在任何地方都使用了
new modifier()
,这会有什么不同吗?如果自动连线只适用于控制器,这意味着自动连线机制只适用于
DispatcherServlet
的应用程序上下文。如果
DispatcherServlet
的xml文件中只有
context:annotation-config
,请尝试将相同的
context:annotation-config
也放在根xml中(为
contextConfigLocation
参数定义的一个),然后查看它是如何运行的。我使用Spring boot,所以我没有web.xml,我会在哪个xml文件中找到它?我有一个app-context.xml,但里面只有
。我应该在哪里查找或放置
context:annotation config
?组件扫描中是否包括您的包com.routing.integration?我看到
modifier
类的包与其他包不同。另外,请不要这样命名您的类…您是否在某处使用
new modifier()
?感谢所有反馈!我做了一个编辑,显示了调用修改器的位置。它通过驼峰路由,并由路由中的bean实例化@迪莫尼,关于类名的事很抱歉。。。如何在组件扫描中包括com.routing.integration@chrylis,我不认为我在任何地方都使用了
new modifier()
,这会有什么不同吗?如果自动连线只适用于控制器,这意味着自动连线机制只适用于
DispatcherServlet
的应用程序上下文。如果
DispatcherServlet
的xml文件中只有
context:annotation-config
,请尝试将相同的
context:annotation-config
也放在根xml中(为
contextConfigLocation
参数定义的一个),然后查看它是如何运行的。我使用Spring boot,所以我没有web.xml,我会在哪个xml文件中找到它?我有一个app-context.xml,但里面只有
。我应该在哪里查找或放置
上下文:注释配置