Black Swan Black Swan - 3 years ago 170
Java Question

How to pass service bean into spring boot controller using xml configuration

I am not comfortable with annotations. In my spring boot application i am retrieving service bean in controller by application context.(i have skipped the naming convention)

Say s1 = (Say) applicationContext.getBean("s1");


It seems that my application is tightly coupled with spring, it is making boilerplate code. So is there any way to autowire the service bean into the controller by xml configuration?

i have tried the following way. But i am getting error like "org.springframework.beans.factory.NoSuchBeanDefinitionException". Below i have given code :

Controller:

@RestController
public class Controller_Test implements SpringApplicationContextInterface
{
@Autowired
private Say s1;

@RequestMapping(value = "/test10")
public String Test1()
{
s1.test();
return "@RequestMapping(value = test10)";
}


}

public interface SpringApplicationContextInterface
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanConfig.xml");

}


XML config file :

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!--<context:annotation-config />-->

<bean class="com.fossians.maven_courses.Say" />
</beans>


I have also tried with :

<bean class="com.fossians.maven_courses.Say" id="s1" />


Error :

***************************
APPLICATION FAILED TO START
***************************

Description:

Field s1 in com.fossians.maven_courses.Controller_Test required a bean of type 'com.fossians.maven_courses.Say' that could not be found.


Action:

Consider defining a bean of type 'com.fossians.maven_courses.Say' in your configuration.
...........................


Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'controller_Test': Unsatisfied dependency expressed through field 's1'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fossians.maven_courses.Say' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
................................................


Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fossians.maven_courses.Say' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
..............................

Answer Source

One possible solution is:

  1. First verify that the xml bean file is located in the resource folder.
  2. Verify that the bean definition has the id definied

<bean class="com.fossians.maven_courses.Say" id="s1">

  1. Got to the Main application Class and after the SpringBoot annotation add the import of the bean xml file, in this case I used spring-config.xml

@SpringBootApplication @ImportResource("classpath:spring-config.xml")

  1. Go to the Controller class and Autowired the Application Context

@Autowired private ApplicationContext context;

  1. If you need to get the s1 bean throught application context then use this:

((Say)context.getBean("s1")).yourMethod();

  1. Also you can Autowire s1 service on your controller

@Autowired private Say sayBean;

  1. You also can use @Service annotation in your Say class and autowired to the controller, that depends on your needs.

Hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download