Spring 集成@Autowired 不工作

Spring Integration @Autowired not working

我正在使用@Autowired 来创建Bean。但是我得到 NullPointer 并且没有创建 Bean。

Sprinng Stater

@ComponentScan("com.api")
public class DoseManagementApplication {

public static void main(String[] args) {
    SpringApplication.run(DoseManagementApplication.class, args);
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("/integration.xml");
    DoseManager doseManager = (DoseManager) context.getBean(DoseManager.class);

    doseManager.doseMangement("");

    context.close();

}

}

Integration.xml

<int:gateway id="doseManager" service-interface="com.cerner.api.gateway.DoseManager"/>

<int:channel id="requestChannnel"></int:channel>

<int:service-activator ref="doseManagerServiceActivator" input-channel="requestChannnel" method="buildDoseMedRelation"> 
</int:service-activator>
<bean id="doseManagerServiceActivator" class="com.cerner.api.activator.DoseManagerServiceActivator"></bean>

DoseManager 接口

package com.api.service;    
@Component
public interface DoseManager {

@Gateway(requestChannel="requestChannnel")
public void doseMangement(String startMsg);
}

服务激活器Class

package com.api.service;
@Component
public class DoseManagerServiceActivator {

@Autowired
private DoseManagerService doseManage;

public void buildDoseMedRelation(Message<?> msg) {
    System.out.println("doseManage== "+doseManage);
}
}

服务Class

package com.api.service;
@Service
public class DoseManagerService {

}

我想弄清楚为什么 @Autowired 不工作。但没有取得任何成功。没有任何服务 class.

你的问题是你使用的是普通的 new ClassPathXmlApplicationContext("/integration.xml");,而不是带有注释支持的那个。

不清楚为什么要创建一个新的应用程序上下文,因为它看起来像是在 Spring 引导中,因为你 SpringApplication.run(DoseManagementApplication.class, args);。从这里您的集成配置被加载到单独的 ClassPathXmlApplicationContext 并且对于 Spring Boot.

完全不可见

我建议您在 DoseManagementApplication 上使用 @SpringBootApplication@ImportResource("classpath:/integration.xml"),然后从 [=17] 调用 getBean()(如果需要) =] 并且根本不使用 ClassPathXmlApplicationContext