简体   繁体   中英

What am I doing wrong in injecting these spring beans? Using Autowiring

This is is a follow up to this question and uses this as a reference.

Here is my modified spring-context.xml

<bean id="sampleApacheConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" lazy-init="true">
    <property name="brokerURL" value="tcp://localhost:61616"/>
    <property name="userName" value=“kodeseeker"/>
    <property name="password" value=“mypassword"/>

</bean>

 <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="sampleApacheConnectionFactory" />
    </bean>

    <!--  Default Destination Queue Definition-->
    <bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="test.Foo"/>
    </bean>

    <!-- JmsTemplate Definition -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="defaultDestination" />
    </bean>

    <!-- Message Sender Definition -->
    <bean id="messageSender" class="com.mypackage.Publisher2">
    </bean>

<!-- Message Receiver Definition -->
    <bean id="messageReceiver" class="com.mypackage.Listener">

    </bean>
       <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destinationName" value="test.Foo" />
        <property name="messageListener" ref="messageReceiver" />
    </bean>
 <context:component-scan base-package="com.mypackage" /> 
    <bean name=“publisher" class=“com.mypackage.Publisher2"/>
        <bean name="updateHandler" class="com.mypackage.UpdateHandlerImpl”/>    

</beans>

And the incoming class that first receives the request.

package com.mypackage

@ComponentScan
@Controller
@Path("/")
public class BaseApiImpl  {

…..
    @Context
    HttpHeaders headers;
    @Autowired
    UpdateHandlerImpl updateHandler;


    @PUT
    @Path("v1/“)
    @Consumes("application/json")
    @Produces({ "application/json" })
    public PutTransactionsResponse updateTransactions(
            UpdateTransactionsRequest entity)
            throws Exception {
     // @formatter:on
        LOGGER.entry();
        try {
        //updateHandler is always Null!
              return updateHandler.handle(new UpdateTransactionsMessage(headers.getRequestHeaders(), entity));

        } catch (Exception e) {
            LOGGER.catching(e);
                   } finally {
            LOGGER.exit();
        }

    }

However, despite annotating the class with @Controller and declaring the beans in the context file . I find that updateHandler is always null. What exactly am I doing wrong here?

Create a Jersey Config class and register it as bean and remove @Component and @ComponentScan on BaseApiImpl class, Hopefully it should work, in my case i am using Spring boot.

import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ApplicationPath; 

@ApplicationPath(value = "/<contextpath>")
public class JerseyConfig extends ResourceConfig{

  public JerseyConfig() {
    packages(true, "<your @Path classes package>");
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM