Thursday, 13 September 2018

How to enable or disable the Spring Dependency checking using @Autowired annotation

Click here to watch on Youtube:

Click the below Image to Enlarge:
How to enable or disable the Spring Dependency checking using @Autowired annotation

How to enable or disable the Spring Dependency checking using @Autowired annotation

<project xmlns="" xmlns:xsi=""



        <!-- Spring 5 dependencies -->


package com.ram.model;

public class Address
    private String city;
    private String state;
    private String country;

    public Address(String city, String state, String country)
    { = city;
        this.state = state; = country;

    public String toString()
        return "Address [city=" + city + ", state=" + state + ", country="
                + country + "]";


package com.ram.model;

import org.springframework.beans.factory.annotation.Autowired;

public class Employee
    private String name;
    private Address address;

    public Employee(String name)
        System.out.println("Employee(String name) constructor "
                + "is called by the Spring container"); = name;

    public void displayEmployeeDetails()
                "Employee [name=" + name + ", address=" + address + "]");



<beans xmlns=""

    <context:annotation-config />

    <!-- <bean id="address" class="com.ram.model.Address">
        <constructor-arg value="Chennai"></constructor-arg>
        <constructor-arg value="Tamil Nadu"></constructor-arg>
        <constructor-arg value="India"></constructor-arg>
    </bean> -->

    <bean id="employee" class="com.ram.model.Employee">
        <constructor-arg value="Peter"></constructor-arg>


package com.ram.core;

import org.springframework.context.ApplicationContext;

import com.ram.model.Employee;

public class App
    public static void main(String[] args)
        ApplicationContext context = new ClassPathXmlApplicationContext(


        Employee employee = (Employee) context.getBean("employee");
                "Got employee object from the ApplicationContext[Spring Container]");



Aug 20, 2018 9:42:47 AM prepareRefresh
INFO: Refreshing startup date [Mon Aug 20 09:42:47 IST 2018]; root of context hierarchy
Aug 20, 2018 9:42:47 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Employee(String name) constructor is called by the Spring container
Got employee object from the ApplicationContext[Spring Container]
Employee [name=Peter, address=null]

Click the below link to download the code:

Github Link:

Bitbucket Link:

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a comment