In a simple spring application you register your bean in the spring IoC container by using @Component annotation and then to retrieve the bean you first read the spring configuration file and then retrieve the bean from container using:

ClassPathXMLApplicationContext context = new ClassPathXMLApplicationContext("spring config file")


Coach theCoach=context.getBean("beanId","classname") 

Now, you can call the methods on theCoach.

How are we retrieving the bean from the container as we are not using

context.getBean();

Is the DispatcherServlet handling this?

After editing-

/*********************Spring Application*******************************/ applicationContext.xml

<beans _______>

<context:component-scan base-package="packageName"/>

</beans>

Coach.java

public interface Coach{

public String getDailyWorkOut();

}

TennisCoach.java

@Component                                                                                                   

public class TennisCoach implements Coach{

public String getDailyWorkOut(){

return "practise back hand volley"; 

}

ApplicationDemo.java

public class ApplicationDemo{

public static void main(String[] args){

ClassPathXMLApplicationContext context = new ClassPathXMLApplicationContext("applicationContext.xml");

Coach theCoach=context.getBean("tennisCoach",Coach.class)

theCoach.getDailyWorkOut();

}

}

/*********************Spring Application*******************************/

Now for Spring MVC-

/*****************Spring-MVC Application**************************/

web.xml

<web-app>
<servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigurationLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
</servlet>

    <servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

applicationContext.xml

<beans _______>

<context:component-scan base-package="packageName"/>

</beans>

Coach.java

public interface Coach{

public String getDailyWorkOut();

}

TennisCoach.java

@Component                                                                                                   

public class TennisCoach implements Coach{

@RequestMapping("/")
public String getDailyWorkOut(){

return "practise back hand volley"; 

}
/*********************Spring-MVC Application*********************/

What I want to know is -

That in above given spring application I am retrieving bean from the container using context.getBean(), how is the Coach bean being retrieved in Spring-MVC application?

2 Answers

0
Vladyslav Diachenko On

Yes, you can just create field with annotation @Autowired and spring inject it for you. Make sure your class where you are going to use this bean is also spring bean.

0
Dmytro Patserkovskyi On

In you example you are retrieving bean via bean lookup from ApplicationContext.

Coach theCoach=context.getBean("tennisCoach",Coach.class)

In this case you know exact class name you need(such you are an author of your bean), and you simply get it from the context.


For DispatcherServlet it is not so easy, because it knows nothing about beans you've added to the context.

The only option it has is a full scanning of all defined in the context beans and detecting anything it can recognise (Controller, RestController, RequestMapping). Example of such detector is AbstractDetectingUrlHandlerMapping with it's implementations. SpringMvc has various implementations of such detectors, you can implement your own if you need.