Spring Core - mrbmrb1122/SpringBootCloud GitHub Wiki

Spring Core

Why Spring:

  • IOC /DI:
  • Good Integration : Struts, Hibernate. Its good integration but Its dont force you to use.. Its a flexibility.
  • Flexibility: Integration with other frameworks

Spring's Singleton:

  • Java Singleton means: One instance per JVM
  • Spring Singleton means: One Instance per application container.

Spring AOP:

AOP:AOP can help you write cleaner applications.Most definitions of AOP say something about the modularization of crosscutting concerns.

Aspect: An aspect is the cross-cutting functionality you are implementing. It is the aspect, or area, of your application you are modularizing. The most common (albeit simple) example of an aspect is logging. Logging is something that is required throughout an application.

Joinpoint: A joinpoint is a point in the execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified.

Advice: Advice is the actual implementation of our aspect. It is advising your application of new behavior. In our logging example, the logging advice would contain the code that implements the actual logging, such as writing to a log file. Advice is inserted into our application at joinpoints.

Pointcut :A pointcut defines at what joinpoints advice should be applied. Advice can be applied at any joinpoint supported by the AOP framework. Of course, you don’t want to apply all of your aspects at all of the possible joinpoints. Pointcuts allow you to specify where you want your advice to be applied. Often you specify these pointcuts using explicit class and method names or through regular expressions that define matching class and method name patterns. Some AOP frameworks allow you to create dynamic pointcuts that determine whether to apply advice based on runtime decisions, such as the value of method parameters. Example:

@Aspect
public class CalculatorLoggingAspect {
...
@Pointcut("execution(* *.*(..))")
private void loggingOperation() {}  ////This is not the Actual method to execute. execution method is different with expressions *.*(..) with params.

@Before("loggingOperation()")
public void logBefore(JoinPoint joinPoint) {
...
}
@AfterReturning(pointcut = "loggingOperation()", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
...
}

Cross Cutting functionality: AOP

We can achive by using ASPECT-POintcut AOP.

If we want to implements Logging whenever methods call like Before method.. end of the method
Point-CUt: Defining which are the methods are intercepted..

This is PointCut
//*save*
//*.get*

Aspect: what the code/functionality which we are we are trying to implement
Logger.logMsg(Logger.INFO, methodName + methodParams);
Different Types of AOP advices:
Before Advice
After returning 
After throw
..
..

Caching implementation in Spring:

@Configuration
@EnableCaching
public class AppConfig{
} 
//Next step 
@Cacheable("players"){
public player findPlayer(int playerID){
}
@CachePut: Update existing cache.
@CacheEvict: Remove from Cache. EX: player is deleted.

//EhCache: anoother Caching framework

Spring Batch features:

  • Restartability : Easy to restart a batch program from where it failed
  • Different Readers and Writers : Provides great support to read from JMS, JDBC, Hibernate, iBatis etc. It can write to JMS, JDBC, Hibernate and more.
  • Chunk Processing : If we have 1 Million records to process, these can be processed in configurable chunks (1000 at a time or 10000 at a time).
  • Easy to implement proper transaction management even when using chunk processing.
  • Easy to implement parallel processing. With simple configuration, different steps can be run in parallel.
  • Spring also provides integration with well known cacheing frameworks like EHCache

Spring Annotations:

@Component/Service : Spring framework Wants to know which are the things/beans life cycle needs to be maintained by the IOC Container.
@AutoWired
@Configuration : Before it was XMls to launch application context. Now we can use @Configuration to lunch applicaiton context. 
@ComponentScan : Specifies where we can search for components/Services.
@RunWith
@ContextConfiguration : What's the context classs

1 ) @Component/Service :

// no need to specify here.
interface HiService{
public String SayHi();
}

@Component 
public class GoodMorningServiceImpl implements HiService{

  public String SayHi(){
  }
}
public class DependencyInjectionJavaContextexmp{

   @AutoWired
   Private Hiservice service;

}

2) Configuration: will have the all the configuration information.

@Configuration
public class JavaTestContext{
}

3) @ComponenrScan:

@Configuration
@ComponentScan(basPackages="Com.services")
public class JavaTestContext{
}

4) ContextConfiguration:

@Configuration
@ComponentScan(basPackages="Com.services")
public class JavaTestContext{

}

@RunWith(SpringJUnitClassRunner.class)
@ContextConfiguration(classes=JavaTestConext.class)
public class DependencyInjectionJavaContextexmp{
   @AutoWired
   Private Hiservice service;
}

@Inject:

Inject also serves same like as @Autowired. But @Inject is from JSR330. From Spring 3, it supports all JSR annotations. JSR330 Annotations: @Inject, @Named, @Qualifier, @Scope and @Singleton.

⚠️ **GitHub.com Fallback** ⚠️