Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring Application Without Any .xml Configuration

In modern Spring applications, especially with the advent of Spring Boot, it's become increasingly common to develop applications without any XML configuration. Instead, Java-based configuration and annotations are favored for their clarity, type-safety, and conciseness.

Let's walk through creating a basic Spring application without any XML configuration:

1. Setup Spring Dependencies:

First, you need to set up your build tool (e.g., Maven) with the necessary Spring dependencies:

<!-- Spring core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.x.x.RELEASE</version>
</dependency>

<!-- Spring context for JavaConfig -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.x.x.RELEASE</version>
</dependency>

2. Java Configuration:

Create a Java configuration class annotated with @Configuration. This class can use the @ComponentScan annotation to specify packages to scan for Spring beans and other annotations for additional configuration.

@Configuration
@ComponentScan(basePackages = "com.example.myapp")
public class AppConfig {
    // Bean definitions go here
}

3. Define Beans:

Within your configuration class, or within any class in the scanned package, define beans using the @Component, @Service, @Repository, or @Controller annotations.

For instance:

package com.example.myapp.service;

@Service
public class MyService {
    // Service code here
}

If you want to define beans in the @Configuration class itself, you can use the @Bean annotation:

@Configuration
public class AppConfig {
    
    @Bean
    public MyBean myBean() {
        return new MyBeanImpl();
    }
}

4. Instantiate Spring Application Context:

Create the application context in your main method or wherever you bootstrap your application:

public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    MyService myService = context.getBean(MyService.class);
    // Use the service or any other bean
}

5. Use Other Features:

Java-based configuration is compatible with all other Spring features, such as property injection (@Value), aspect-oriented programming (@Aspect), transaction management (@Transactional), and more.

With Spring Boot:

Spring Boot simplifies this process even further:

  1. Add Spring Boot Starter dependencies in your build tool.
  2. Use @SpringBootApplication annotation which implicitly defines a base ��search package�� for certain items.
  3. Bootstrap the application using SpringApplication.run() in the main method.
@SpringBootApplication
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

Spring Boot also provides a variety of "starters" which are pre-defined dependency descriptors that you can include in your application. For instance, the spring-boot-starter-web is used for web applications.

In conclusion, Java-based configuration has become the predominant way to configure Spring applications, offering a programmatic, type-safe alternative to XML. Combined with the conveniences of Spring Boot, setting up a Spring application has never been easier.

  1. Spring application without XML configuration example:

    • Modern Spring applications often prefer Java-based configuration over XML. This example demonstrates a simple Spring application using only Java configuration.
    @Configuration
    @ComponentScan("com.example")
    public class AppConfig {
    
        @Bean
        public MyBean myBean() {
            return new MyBean();
        }
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context = 
                new AnnotationConfigApplicationContext(AppConfig.class);
    
            MyBean myBean = context.getBean(MyBean.class);
            myBean.doSomething();
    
            context.close();
        }
    }
    
  2. Configuring Spring without XML files:

    • Configure Spring without XML by using @Configuration and @ComponentScan annotations.
    @Configuration
    @ComponentScan("com.example")
    public class AppConfig {
    
        @Bean
        public MyBean myBean() {
            return new MyBean();
        }
    }
    
  3. JavaConfig in Spring framework:

    • JavaConfig is an alternative to XML configuration, allowing you to define beans and their relationships using Java classes.
    @Configuration
    public class AppConfig {
    
        @Bean
        public MyBean myBean() {
            return new MyBean();
        }
    }
    
  4. Spring application with Java-based configuration:

    • Create a Spring application using Java-based configuration, leveraging @Configuration and @Bean.
    @Configuration
    @ComponentScan("com.example")
    public class AppConfig {
    
        @Bean
        public MyBean myBean() {
            return new MyBean();
        }
    }
    
  5. Zero XML Spring application setup:

    • Achieve a Spring application setup with zero XML files by relying entirely on JavaConfig.
    @Configuration
    @ComponentScan("com.example")
    public class AppConfig {
    
        @Bean
        public MyBean myBean() {
            return new MyBean();
        }
    }
    
  6. Using @Configuration in Spring instead of XML:

    • Use the @Configuration annotation to declare a class as a configuration class, providing bean definitions.
    @Configuration
    public class AppConfig {
    
        @Bean
        public MyBean myBean() {
            return new MyBean();
        }
    }
    
  7. XML-free Spring application development:

    • Embrace XML-free Spring application development by leveraging JavaConfig and annotations.
    @Configuration
    @ComponentScan("com.example")
    public class AppConfig {
    
        @Bean
        public MyBean myBean() {
            return new MyBean();
        }
    }
    
  8. Spring Boot without XML configuration:

    • Spring Boot simplifies configuration further, reducing or eliminating the need for XML files.
    @SpringBootApplication
    public class MySpringBootApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MySpringBootApplication.class, args);
        }
    }