Spring Configuration

Spring Framework (24 Part Series)

1 Spring Configuration
2 External Properties in Spring
20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types

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

Spring Framework (24 Part Series)

1 Spring Configuration
2 External Properties in Spring
20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types

原文链接:Spring Configuration

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容