Wednesday, 31 October 2018

Spring JavaConfig @Import example | @Import Annotation in Spring JavaConfig | Spring Tutorial

🚀 Master Modular Spring JavaConfig!

Subscribe to Ram N Java for clean architectural patterns, Spring framework tutorials, and code walkthroughs!

🔔 Subscribe to Ram N Java

Mastering Spring JavaConfig with the @Import Annotation

As enterprise Spring applications grow in scale, consolidating all configuration beans into a monolithic class quickly creates technical debt and maintainability challenges. Spring provides the @Import annotation as the standard mechanism to organize, separate, and import modular @Configuration classes cleanly, keeping configuration files modular, focused, and test-friendly.

How @Import Simplifies Spring Configuration

The @Import annotation functions as the pure Java equivalent of the traditional <import resource="..."/> element in XML configuration:

  • Granular Configuration: Distinct domain layers (such as service configurations, persistence settings, or security filters) are isolated in separate classes marked with @Configuration.
  • Centralized Composition: A root or primary configuration class pulls in these discrete configuration classes using @Import({ConfigA.class, ConfigB.class}).
  • Single Entry Point: When booting up AnnotationConfigApplicationContext, developers only need to pass the central class. Spring automatically registers and coordinates all declared beans across the imported classes.

Step-by-Step Implementation Flow

  1. Define Sub-Configurations: Build feature-specific configuration classes (e.g., ConfigA providing BeanA, and ConfigB providing BeanB).
  2. Aggregate with @Import: In your central configuration class (such as AppConfig), annotate the class with @Configuration and @Import({ConfigA.class, ConfigB.class}).
  3. Bootstrap ApplicationContext: Instantiate the Spring container by passing only AppConfig.class into AnnotationConfigApplicationContext.
  4. Verify Bean Availability: Both BeanA and BeanB are instantiated, wired, and accessible from the single application context seamlessly.

Major Architectural Benefits

  • Enhanced Maintainability: Keeps configuration files concise, domain-driven, and easy to navigate.
  • Isolated Unit & Integration Testing: Enables test setups to selectively load only relevant configuration modules rather than the full application context.
  • Explicit Dependencies: Provides clear visibility into configuration dependencies directly through standard Java imports and type references.

Recommended Next Videos

Broaden your Spring JavaConfig and architecture expertise with these related tutorials from our channel:


Click here to watch on Youtube:
https://www.youtube.com/watch?v=zGTO57LGOtI&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge:
Spring JavaConfig @Import example | @Import Annotation in Spring JavaConfig | Spring Tutorial

Spring JavaConfig @Import example | @Import Annotation in Spring JavaConfig | Spring Tutorial
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0%20http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ram.core</groupId>
    <artifactId>SpringDemo</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringDemo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>5.0.2.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring 5 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</project>

Employee.java

package com.ram.model;

public class Employee
{
    private int id;
    private String name;

    public Employee(int id, String name)
    {
        System.out.println("Employee(int id, String name) constructor "
                + "is called by the Spring container");
        this.id = id;
        this.name = name;
    }

    public void displayEmployeeDetails()
    {
        System.out.println("Employee [id=" + id + ", name=" + name + "]");
    }

}

EmployeeConfig.java

package com.ram.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.ram.model.Employee;

/**
 * Annotate with @Configuration to tell Spring that this is the core
 * Spring configuration file, and define bean via @Bean.
 */

@Configuration
public class EmployeeConfig
{

    @Bean(name = "employee")
    public Employee getEmployee()
    {
        return new Employee(36,"Peter");
    }

}

ApplicationBeans.java

package com.ram.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Annotate with @Configuration to tell Spring that this is the core
 * Spring configuration file, and define bean via @Bean.
 */

@Configuration
@Import({EmployeeConfig.class})
public class ApplicationBeans
{

}

App.java

package com.ram.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.ram.config.ApplicationBeans;
import com.ram.model.Employee;

public class App
{
    public static void main(String[] args)
    {
        ApplicationContext context = new AnnotationConfigApplicationContext(
                ApplicationBeans.class);

        System.out.println("---------------------------------------");

        Employee employee = (Employee) context.getBean("employee");
        System.out.println(
                "Got employee object from the ApplicationContext(Spring Container)");
        employee.displayEmployeeDetails();

    }

}

Output

Oct 06, 2018 8:30:01 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5f4da5c3: startup date [Sat Oct 06 08:30:01 IST 2018]; root of context hierarchy
Employee(int id, String name) constructor is called by the Spring container
---------------------------------------
Got employee object from the ApplicationContext(Spring Container)
Employee [id=36, name=Peter]

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/SpringDemo_JavaConfig_import.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/javaee/tree/master/Spring_2018/SpringDemo_JavaConfig_import

Bitbucket Link:
https://bitbucket.org/ramram43210/spring/src/864d48b49c31c0a6d41770bb5dba6b0893945b87/Spring_2018/SpringDemo_JavaConfig_import/?at=master

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
  • How to use getWeekYear() method of Java.util.GregorianCalendar class


    Click here to watch on Youtube:
    https://www.youtube.com/watch?v=onn24oqd4d0&list=UUhwKlOVR041tngjerWxVccw

    GregorianCalendarDemo.java

    import java.util.Date;
    import java.util.GregorianCalendar;

    public class GregorianCalendarDemo
    {
        public static void main(String[] args)
        {
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar
                    .getInstance();

            System.out.println(cal.getTime());

            /*
             * Returns:
             *
             * the week year represented by this GregorianCalendar. If the
             * ERA value is BC, the year is represented by 0 or a negative
             * number: BC 1 is 0, BC 2 is -1, BC 3 is -2, and so on.
             */

           
            int weekYear = cal.getWeekYear();
            System.out.println("weekYear = "+weekYear);
           
            /*
             * Returns: the Gregorian cutover date for this
             * GregorianCalendar object.
             */

            Date date = cal.getGregorianChange();
            System.out.println(date);
        }
    }

    Output

    Fri Sep 21 09:59:22 IST 2018
    weekYear = 2018
    Fri Oct 15 05:30:00 IST 1582

    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev2/java_basics/GregorianCalendarDemo_getWeekYear.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava_2018/GregorianCalendarDemo_getWeekYear

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ca178d0280357a5fff33a91716acad6f25ed6390/BasicJava_2018/GregorianCalendarDemo_getWeekYear/?at=master

    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
  • How to use getWeeksInWeekYear() method of Java.util.GregorianCalendar class


    Click here to watch on Youtube: 
    https://www.youtube.com/watch?v=BhamLRLv89E&list=UUhwKlOVR041tngjerWxVccw

    GregorianCalendarDemo.java

    import java.util.GregorianCalendar;

    public class GregorianCalendarDemo
    {
        public static void main(String[] args)
        {
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar
                    .getInstance();

            System.out.println(cal.getTime());

            /*
             * Returns:the number of weeks in the week year.
             */

            int weeksInWeekYear = cal.getWeeksInWeekYear();
            System.out.println("weeksInWeekYear = " + weeksInWeekYear);

        }
    }

    Output

    Tue Sep 18 10:09:57 IST 2018
    weeksInWeekYear = 52

    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev2/java_basics/GregorianCalendarDemo_getWeeksInWeekYear.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava_2018/GregorianCalendarDemo_getWeeksInWeekYear

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ca178d0280357a5fff33a91716acad6f25ed6390/BasicJava_2018/GregorianCalendarDemo_getWeeksInWeekYear/?at=master

    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
  • How to use getTimeZone() method of Java.util.GregorianCalendar class | Java Date and Time


    Click here to watch on Youtube:
    https://www.youtube.com/watch?v=2Tr6RsJjD0o&list=UUhwKlOVR041tngjerWxVccw

    GregorianCalendarDemo.java

    import java.util.GregorianCalendar;
    import java.util.TimeZone;

    public class GregorianCalendarDemo
    {
        public static void main(String[] args)
        {
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar
                    .getInstance();

            /*
             * Returns:the time zone object associated with this calendar.
             */

            TimeZone timeZone = cal.getTimeZone();
            System.out.println("Time Zone = " + timeZone.getDisplayName());

        }
    }

    Output

    Time Zone = India Standard Time

    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev2/java_basics/GregorianCalendarDemo_getTimeZone.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava_2018/GregorianCalendarDemo_getTimeZone

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ca178d0280357a5fff33a91716acad6f25ed6390/BasicJava_2018/GregorianCalendarDemo_getTimeZone/?at=master

    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
  • How to use getMinimum(int field) method of Java.util.GregorianCalendar class | Java Date and Time


    Click here to watch on Youtube:
    https://www.youtube.com/watch?v=u0H2JWaSaNA&list=UUhwKlOVR041tngjerWxVccw

    GregorianCalendarDemo.java

    import java.util.GregorianCalendar;

    public class GregorianCalendarDemo
    {
        public static void main(String[] args)
        {
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar
                    .getInstance();

            /*
             * Parameters:
             *
             * field - the calendar field.
             *
             * Returns:
             *
             * the minimum value for the given calendar field.
             */

            int min = cal.getMinimum(GregorianCalendar.DAY_OF_MONTH);
            System.out.println("Minimum, DAY_OF_MONTH = " + min);

            min = cal.getMinimum(GregorianCalendar.MONTH);
            System.out.println("Minimum, MONTH = " + min);

            min = cal.getMinimum(GregorianCalendar.YEAR);
            System.out.println("Minimum, YEAR = " + min);

        }
    }

    Output

    Minimum, DAY_OF_MONTH = 1
    Minimum, MONTH = 0
    Minimum, YEAR = 1

    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev2/java_basics/GregorianCalendarDemo_getMinimum.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava_2018/GregorianCalendarDemo_getMinimum

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ca178d0280357a5fff33a91716acad6f25ed6390/BasicJava_2018/GregorianCalendarDemo_getMinimum/?at=master

    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
  • Monday, 29 October 2018

    How to use getMaximum(int field) method of Java.util.GregorianCalendar class | Java Date and Time


    Click here to watch on Youtube: 
    https://www.youtube.com/watch?v=Bu9U18-vUPM&list=UUhwKlOVR041tngjerWxVccw

    GregorianCalendarDemo.java

    import java.util.GregorianCalendar;

    public class GregorianCalendarDemo
    {
        public static void main(String[] args)
        {
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar
                    .getInstance();

            /*
             * Parameters:
             *
             * field - the calendar field.
             *
             * Returns:
             *
             * the maximum value for the given calendar field.
             */

            int max = cal.getMaximum(GregorianCalendar.DAY_OF_MONTH);
            System.out.println("Maximum, DAY_OF_MONTH = " + max);
           
            max = cal.getMaximum(GregorianCalendar.MONTH);
            System.out.println("Maximum, MONTH = " + max);

            max = cal.getMaximum(GregorianCalendar.YEAR);
            System.out.println("Maximum, YEAR = " + max);

        }
    }

    Output

    Maximum, DAY_OF_MONTH = 31
    Maximum, MONTH = 11
    Maximum, YEAR = 292278994

    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev2/java_basics/GregorianCalendarDemo_getMaximum.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava_2018/GregorianCalendarDemo_getMaximum

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ca178d0280357a5fff33a91716acad6f25ed6390/BasicJava_2018/GregorianCalendarDemo_getMaximum/?at=master

    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
  • How to use getGreatestMinimum(int field) and getLeastMaximum(int field) methods of GregorianCalendar


    Click here to watch on Youtube:
    https://www.youtube.com/watch?v=S4-l_9ANpBM&list=UUhwKlOVR041tngjerWxVccw

    GregorianCalendarDemo.java

    import java.util.GregorianCalendar;

    public class GregorianCalendarDemo
    {
        public static void main(String[] args)
        {
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar
                    .getInstance();

            /*
             * Parameters:
             *
             * field - the calendar field.
             *
             * Returns:
             *
             * the highest minimum value for the given calendar field.
             */

            int min = cal.getGreatestMinimum(GregorianCalendar.DAY_OF_MONTH);
            System.out.println("Greatest Minimum, DAY_OF_MONTH = " + min);

            min = cal.getGreatestMinimum(GregorianCalendar.MONTH);
            System.out.println("Greatest Minimum, MONTH = " + min);

            /*
             * Parameters:
             *
             * field - the calendar field
             *
             * Returns:
             *
             * the lowest maximum value for the given calendar field.
             */

            int max = cal.getLeastMaximum(GregorianCalendar.DAY_OF_MONTH);
            System.out.println("Least Maximum, DAY_OF_MONTH = " + max);

            max = cal.getLeastMaximum(GregorianCalendar.MONTH);
            System.out.println("Least Maximum, MONTH = " + max);

        }
    }

    Output

    Greatest Minimum, DAY_OF_MONTH = 1
    Greatest Minimum, MONTH = 0
    Least Maximum, DAY_OF_MONTH = 28
    Least Maximum, MONTH = 11

    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev2/java_basics/GregorianCalendarDemo_getGreatestMinimum.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava_2018/GregorianCalendarDemo_getGreatestMinimum

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ca178d0280357a5fff33a91716acad6f25ed6390/BasicJava_2018/GregorianCalendarDemo_getGreatestMinimum/?at=master

    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
  • How to use getActualMaximum(int field) and getActualMinimum(int field) methods of GregorianCalendar


    Click here to watch on Youtube: 
    https://www.youtube.com/watch?v=utAy96z0tZM&list=UUhwKlOVR041tngjerWxVccw

    GregorianCalendarDemo.java

    import java.util.GregorianCalendar;

    public class GregorianCalendarDemo
    {
        public static void main(String[] args)
        {
            GregorianCalendar gCal = (GregorianCalendar) GregorianCalendar
                    .getInstance();

            /*
             * Parameters:
             *
             * field - the calendar field
             *
             * Returns:
             *
             * the maximum of the given field for the time value of this
             * GregorianCalendar
             */

            int max = gCal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
            System.out.println("Actual Maximum, DAY_OF_MONTH = " + max);

            max = gCal.getActualMaximum(GregorianCalendar.MONTH);
            System.out.println("Actual Maximum, MONTH = " + max);

            /*
             * Parameters:
             *
             * field - the calendar field
             *
             * Returns:
             *
             * the minimum of the given field for the time value of this
             * GregorianCalendar
             */

            int min = gCal.getActualMinimum(GregorianCalendar.DAY_OF_MONTH);
            System.out.println("Actual Minimum, DAY_OF_MONTH = " + min);

            min = gCal.getActualMinimum(GregorianCalendar.MONTH);
            System.out.println("Actual Minimum, MONTH = " + min);

        }
    }

    Output

    Actual Maximum, DAY_OF_MONTH = 30
    Actual Maximum, MONTH = 11
    Actual Minimum, DAY_OF_MONTH = 1
    Actual Minimum, MONTH = 0

    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev2/java_basics/GregorianCalendarDemo_getActualMax_Min.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava_2018/GregorianCalendarDemo_getActualMax_Min

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/ca178d0280357a5fff33a91716acad6f25ed6390/BasicJava_2018/GregorianCalendarDemo_getActualMax_Min/?at=master

    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
  • Tutorials