DEV Community

eidher
eidher

Posted on • Edited on

5 2

Spring Configuration

Spring Configuration annotation indicates that the class has @Bean definition methods.
Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by the Spring context.

@Configuration
public class ApplicationConfig {
  @Bean
  public DataSource dataSource() {
    return new DataSource();
  }
}
Enter fullscreen mode Exit fullscreen mode

Spring will create as many beans as we define in the @Configuration classes. @Configuration classes are beans too.

public class MySpringApp {
  public static void main(String[] args) {
    // Create the application from the configuration
    ApplicationContext ctx = SpringApplication.run(ApplicationConfig.class);

    // Look up the application service interface
    // Way 1: Casting
    DataSource ds1 = (DataSource) ctx.getBean("dataSource");
    // Way 2: Use typed method
    DataSource ds2 = ctx.getBean("dataSource", DataSource.class);
    // Way 3: Without been id if type is unique
    DataSource ds3 = ctx.getBean(DataSource.class);

    // Use the application
    ds1.close();
    ds2.close();
    ds3.close();
  }
}
Enter fullscreen mode Exit fullscreen mode

Creating an Application Context from Multiple Files

@Configuration
@Import({ApplicationConfig.class, WebConfig.class})
public class InfraetructureConfig {
  @Bean
  public DataSource dataSource() {
    return new DataSource();
  }
}
Enter fullscreen mode Exit fullscreen mode

Beware of duplicate beans. In the previous code, if ApplicationConfig.class and WebConfig.class have the same bean id you get the bean from WebConfig because it is the last bean. To avoid that you can use the @Order annotation.

@Configuration
@Order(1)
public class ApplicationConfig {
  @Bean
  public String example(){
    return new String("example");
  }
}
Enter fullscreen mode Exit fullscreen mode

Bean Scope

The default scope is singleton

DataSource ds1 = (DataSource) ctx.getBean("dataSource");
DataSource ds2 = (DataSource) ctx.getBean("dataSource");
assert ds1 == ds2; // True - Same object
Enter fullscreen mode Exit fullscreen mode

Prototype Scope: a new instance is created every time the bean is referenced.

@Bean
@Scope("prototype")
public DataSource dataSource() {
  return new DataSource();
}

DataSource ds1 = (DataSource) ctx.getBean("dataSource");
DataSource ds2 = (DataSource) ctx.getBean("dataSource");
assert ds1 != ds2; // True - Diferent objects
Enter fullscreen mode Exit fullscreen mode

Other scopes:

  • session
  • request
  • application
  • global
  • thread
  • websocket
  • refresh
  • Custom scopes

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay