1

I am using @Autowired to get create the Bean. But i am getting NullPointer and the Bean is not getting created.

Spprinng 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 Interface

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

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

Service Activator Class

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

@Autowired
private DoseManagerService doseManage;

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

Service Class

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

}

I am trying to figure out why the @Autowired is not working. but didn't get any success. There is nothing the Service class.

Sam
  • 181
  • 2
  • 4
  • 17

1 Answers1

1

Your problem that you use a plain new ClassPathXmlApplicationContext("/integration.xml");, but not that one with annotations support.

It is not clear why you create a new application context at all since it looks like you are in Spring Boot, because you that SpringApplication.run(DoseManagementApplication.class, args);. From here your integration configuration is loaded in the separate ClassPathXmlApplicationContext and is fully not visible for Spring Boot.

I would suggest you to use a @SpringBootApplication on your DoseManagementApplication together with the @ImportResource("classpath:/integration.xml") and then call getBean() (if you need) from the ApplicationContext context = SpringApplication.run(DoseManagementApplication.class, args); and don't use an ClassPathXmlApplicationContext at all.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • I had tried this @ImportResource("classpath:/integration.xml") ealier but i was not knowing that we can get the ApplicationContext from the SpringApplication.run . Thanks for the help. Its working fine. – Sam Dec 06 '18 at 02:18