Wednesday, 13 December 2017

Spring

1.Starting class implements WebApplicationInitializer interface
(org.springframework.web.WebApplicationInitializer)==>and this class will be scanned by Spring on application startup and bootstrapped.
 then overriding "onStartup" method from WebApplicationInitializer interface
 inside this method
        AnnotationConfigWebApplicationContext appContext = new         AnnotationConfigWebApplicationContext();
    appContext.register(ApplicationContextConfig.class);




ServletRegistration.Dynamic dispatcher = container.addServlet(
                "SpringDispatcher", new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

==>here,Dynamic servlet registration way of registering servlets. In Servlets 3 you can avoid creating web.xml and configure application in pure Java
It gives you some advantages like compile time check if everything is fine
in web.xml shows the above things like this
    <servlet>
     <servlet-name>appServlet</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
      </init-param>
     <load-on-startup>1</load-on-startup>
        </servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
        </servlet-mapping>

2. class which extend WebMvcConfigurerAdapter is used for bean configuration in web.xml or servlet-context.xml that shown in web.xml contextConfigLocation in init-param in xml

    lot @Beans here used in core java code that replaces what we used in web.xml

a) @Bean(name = "viewResolver")   <===>
      <beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

-->public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
        }

b)addInterceptors

3)Interceptor
    class which implements "HandlerInterceptorAdapter" is act as inteceptor class
it overriding  "preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)" method

4)@Configuration & @Bean Annotations
Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions.

   -->package com.tutorialspoint;
     import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig {
     @Bean
     public HelloWorld helloWorld(){
      return new HelloWorld();
     }
   }

  equal to

<beans>
   <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" />
</beans>

5)@ComponentScan("com.leejo")
Spring needs to know the packages to scan for annotated components. We can use @ComponentScan annotation to specify the base packages to scan. Any class which is annotated with @Component will be scanned for registered.

 6)@EnableWebMvc
     is used to enable Spring MVC
@EnableWebMvc is equivalent to <mvc:annotation-driven /> in XML
It enables support for @Controller-annotated classes that use @RequestMapping to map incoming requests to a certain method.

7)@PropertySource("classpath:some.properties")
<context:property-placeholder location="some.properties"/>
  equall to
    @Configuration
@PropertySource("classpath:some.properties")
public class ApplicationConfiguration

8)If you are using Java based configuration in your application you can enable annotation-driven injection by using AnnotationConfigApplicationContext to load your spring configuration as below:
@Configuration
@ComponentScan("com.baeldung.autowire.sample")
public class AppConfig {}

As an alternative, in Spring XML, it can be enabled by declaring it in Spring XML files like so:
<context:annotation-config/>

@Autowired and Optional Dependencies--->
          public class FooService {

              @Autowired(required = false)
                private FooDAO dataAccessor;
   
                  }

9)@Scope("session")
  The spring framework supports following five scopes. Out of which three scopes are supported only in web ApplicationContext.
    a.Singleton--Return a single bean instance per Spring IoC container
    b.Prototype--Return a new bean instance each time when requested
c.Request-- Return a single bean instance per HTTP request. *
d.Session--Return a single bean instance per HTTP session. *
e.Global Session--Return a single bean instance per global HTTP session. *

site:- https://www.tutorialspoint.com/spring/spring_bean_scopes.htm

10)Controller using @Controller
   The @Controller annotation is used to mark any java class as a controller class.
   The @RequestMapping annotation is used to indicate the type of HTTP request.
 
11)@Repository
  @Repository is an annotation that marks the specific class as a Data Access Object, thus clarifying it's role. Other markers of the same category are @Service and @Controller
   @Autowired is an annotation with a completely different meaning: it basically tells the DI container to inject a dependency.
 
12)@Service
  annotate classes at service layer level.
  annotation is used in your service layer and annotates classes that perform service tasks, often you don't use it but in many case you use this annotation to represent a best practice. For example, you could directly call a DAO class to persist an object to your database but this is horrible. It is pretty good to call a service class that calls a DAO. This is a good thing to perform the separation of concerns pattern.

13)Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection.

@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); registry.addResourceHandler("/uploads/**").addResourceLocations("/uploads/"); registry.addResourceHandler("/albums/**").addResourceLocations("/albums/"); }

==>pending
CascadeType

fetch = FetchType.LAZY,fetch = FetchType.EAGER 

Caching in Hibernate

Sieilaization
synchronisation
Cloneable ,how to create copy of object

No comments:

Post a Comment