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:
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>
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 } 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(); } } 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 } Java-based configuration is compatible with all other Spring features, such as property injection (@Value), aspect-oriented programming (@Aspect), transaction management (@Transactional), and more.
Spring Boot simplifies this process even further:
@SpringBootApplication annotation which implicitly defines a base ��search package�� for certain items.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.
Spring application without XML configuration example:
@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(); } } Configuring Spring without XML files:
@Configuration and @ComponentScan annotations.@Configuration @ComponentScan("com.example") public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } JavaConfig in Spring framework:
@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } Spring application with Java-based configuration:
@Configuration and @Bean.@Configuration @ComponentScan("com.example") public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } Zero XML Spring application setup:
@Configuration @ComponentScan("com.example") public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } Using @Configuration in Spring instead of XML:
@Configuration annotation to declare a class as a configuration class, providing bean definitions.@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } XML-free Spring application development:
@Configuration @ComponentScan("com.example") public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } Spring Boot without XML configuration:
@SpringBootApplication public class MySpringBootApplication { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class, args); } } udev marshalling reportlab openssh extends v-navigation-drawer azure-cli delivery-pipeline image-rotation jdatechooser