Thursday, October 19, 2017

Spring annotations: What When and How?



@Configuration:
Used as a replacement for bean definition in xml. Usually @Bean is used to register beans.


@Configurable // To inject beans within object created using new ()
@Configurable(preConstruction = true)  -Inject dependencies before object creation 

<context:spring-configured/> To enable load time weaving 
<context:annotation-config> To activate annotations in beans already registered
<context:component-scan> To activate annotations in beans already registered +   also scans packages to find and register beans within the application context.




@ContextConfiguration:
-Used for Spring based test class
-By default it will look to load testclass-context.xml if not specified in class .
@ContextConfiguration("configFile.xml")
Or @ContextConfiguration({TestConfig.class,HelperClass.class})

@Import
-Aggregating @Configuration classes
-Can be used to import other @Configuration class

@ Retryable

@Service

public class TestService {

  @Retryable(value = {SomeException.class, MyException.class}, 
      maxAttempts = 3,backoff = @Backoff(2000))
  public void retryWhenException() {
try{
// perform operation that can fail
}catch(SomeException se){
// will be retried
throw se;
}catch(MyException me){
// will be retried
throw me;
}catch(Exception e){
// will not be retried
throw e;
}
  }

  @Recover
  public void recover(SomeException exception) {
    // recover from SomeException
  }
}

No comments:

Post a Comment