Spring MVC

 

 

 

 

http://www.java4s.com/wp-content/uploads/2013/07/Spring-MVC-execution-flow.png

 

Spring MVC 3.2 Execution Flow

Step 1: First request will be received by DispatcherServlet
Step 2:         
DispatcherServlet will take the help of HandlerMapping and get to know theController class name associated with the given request
Step 3: So request transfer to the Controller, and then controller will process the request by executing appropriate methods and returns 
ModeAndView object (contains Model data and View name) back to the DispatcherServlet.

Step 4: Now DispatcherServlet send the model object to the ViewResolver to get the actual view page.

Step 5: Finally DispatcherServlet will pass the Model object to the View page to display the result

That’s it :-)

Just remember this diagram for the interview purpose, i will explain you the practical flow in the first example.

 

 

 

http://images.techhive.com/images/idge/imported/article/jvw/1999/12/model1_sml-100158071-orig.gif

 

 

http://images.techhive.com/images/idge/imported/article/jvw/1999/12/model2_sml-100158072-orig.gif

 

 

 

 

The key benefit of a Dependency Injector is that it removes the dependency.

Spring container resolves this requirements by automatically and automagically wiring everything together

 

Central to the Spring Framework is its Inversion of Control container, which provides a consistent means of configuring and managing Java objects using callbacks. The container is responsible for managing object lifecycles: creating objects, calling initialization methods, and configuring objects by wiring them together.

Objects created by the container are also called Managed Objects or Beans.

 

The BeanFactory (Beans and Core) is a sophisticated implementation of the factory pattern.

 

 


 

Spring Security

 

 

http://2.bp.blogspot.com/-10l38lt98mY/UC24yf8xbWI/AAAAAAAAAG0/dJMdbsEWcOE/s1600/Nimble.png

 

 

 


 

If multiple beans found for the same type, use @Qualifier

 

public interface Car {

    public void getCarName();

}

 

 

@Component("Ferari")

public class Ferari implements Car{

 

    public void getCarName() {

        System.out.println("This is Ferari");

    }

 

}

 

 

@Component("Mustang")

public class Mustang implements Car{

 

    public void getCarName() {

        System.out.println("This is Mustang");

    }

}

 

 

@Component

public class Bond {

 

    @Autowired

    @Qualifier("Mustang") // This is very important

    private Car car;

    

    public void showCar(){

        car.getCarName();

    }

}

 

 

public class AppMain {

 

    public static void main(String args[]) {

        AbstractApplicationContext context = new AnnotationConfigApplicationContext(

                AppConfig.class);

 

        Bond bond = (Bond) context.getBean("bond");

        bond.showCar();

    }

 

}

 

 

 

 

Ehcache

 

parameter values of the method will be used as a composite key into the cache, caching the return value of the method.

Configuration

@EnableCaching

@ComponentScan({ "com.mkyong.*" })

public class AppConfig {

 

        @Bean

        public CacheManager cacheManager() {

                 return new EhCacheCacheManager(ehCacheCacheManager().getObject());

        }

 

        @Bean

        public EhCacheManagerFactoryBean ehCacheCacheManager() {

                 EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();

                 cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));

                 cmfb.setShared(true);

                 return cmfb;

        }

}

 

@Cacheable(value="manual", key="#isbn.id")

public Manual findManual(ISBN isbn, boolean checkWarehouse)

@CacheEvict(value = "manuals", allEntries=true)

public void loadManuals(InputStream batch)


Servlet and Reactive Stacks in Spring Framework 5

 

 

 

 

 

@RestController
public class
CarController {

   
private final WebClient carsClient =
           
WebClient.create("http://localhost:8081");
   
private final WebClient bookClient =
           
WebClient.create("http://localhost:8082");

   
@PostMapping("/booking")
   
public Mono<ResponseEntity<Void>> book() {
       
return carsClient.get().uri("/cars")
                .
retrieve()
                .
bodyToFlux(Car.class)
                .
take(5)
                .
flatMap(this::requestCar)
                .
next();
    }

   
private Mono<ResponseEntity<Void>> requestCar(Car car) {
       
return bookClient.post()
                .
uri("/cars/{id}/booking", car.getId())
                .
exchange()
                .
flatMap(response -> response.toEntity(Void.class));
    }
}

 

 

 

 

 

// Pump out a new car every second

Flux<Car> body = Flux.interval(Duration.ofSeconds(1)).map(i -> new Car(...));

// Post the live streaming data to the server

WebClient.create().post()
        .
uri("http://localhost:8081/cars")
        .
contentType(MediaType.APPLICATION_STREAM_JSON)
        .
body(body, Car.class)
        .
retrieve()
        .
bodyToMono(Void.class)
        .
block();

// Server posts the streamed data as it comes (see discussion below)

@PostMapping(path="/cars", consumes = "application/stream+json")
public Mono<Void> loadCars(@RequestBody Flux<Car> cars) {
   
return repository.insert(cars).then();
}  

 

// alternatively

 

@GetMapping(path = "/cars", produces = "application/json")
public Flux<Car> getCars() {
   
return this.repository.findAll();
}

 

@GetMapping(path = "/cars", produces = "application/stream+json")
public Flux<Car> getCarStream() {
   
return this.repository.findCars();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Spring MVC : blocking way