Common Aware Interfaces used in Spring

If You are developing application using Spring then sometime it's important that your beans aware about Spring IOC container resources. Here, Spring Aware comes into picture.

To use Aware Interfaces, You bean class should implement aware interfaces. and That's way they will be aware about container resources. I like to use these interfaces :)

Common Aware Interfaces used in Spring 3.2:
  1. BeanNameAware
  2. ApplicationContextAware
  3. BeanFactoryAware
  4. MessageSourceAware
  5. ApplicationEventPublisherAware
  6. ResourceLoaderAware

Spring @PostConstruct and @PreDestroy annotation Example

If You want to perform specific action or handling upon bean Initialization and Bean destruction then Implementing InitializingBean and Disposable Bean or init-method and destroy-method is for You !!

But there is another annotation approach available. You need to annotate methods with @PostConstruct and @PreDestroy annotation in you bean class and define in Spring Bean configuration file.

You can either add CommonAnnotationBeanPostProcessor (org.springframework.context.annotation.CommonAnnotationBeanPostProcessor) class in Spring bean xml or use context:annotation-config in spring bean configuration file

Init-method and Destroy-method Spring Bean example

If You want to perform specific action or handling upon bean Initialization and Bean destruction then Implementing InitializingBean and Disposable Bean is for You !!

But there is another approach also available if you don't want to implement those interfaces..You can add init-method and destroy-method into your spring bean configuration file at bean level.

Difference between context:component-scan and context:annotation-config in Spring

<context:component-scan/> scan packages and classes within given base packages then find and register beans into ApplicationContext.It does all things that <context:annotation-config/> is supposed to do.
So if you have used annotation for example, @Autowired in your code and <context:component-scan/> in xml then You do not require to use <context:annotation-config/>

What is ApplicationContext and BeanFactory in Spring?

ApplicationContext(org.springframework.context.ApplicationContext) is interface provided by Spring  and it's used for getting Configuration information about your application.Spring has many classes which implements this interface.

What are common Implementation of ApplicationContext ? - They are
  1. ClassPathXmlApplicationContext
  2. FileSystemXmlApplicationContext
  3. XmlWebApplicationContext

Apache Tika - Extract MetaData and Stractured Text Content Extraction

Apache Tika is used for detecting and extracting metadata and structured text content from different documents using existing parser libraries.

Apache Tika provides interface called Parser (org.apache.tika.parser) which provides api called parse whose job is to parses a document stream into a sequence of xhtml sax events.

Tika supports different formats like text,audio,image,video,word document,open document,pdf,xml,html etc. Please refer to Apache Tika Supported Documents Format for more details. You may want have a look at Apache Tika 1.4 API documentation

    Spring InitializingBean and DisposableBean Example

    In Spring, If You want to perform specific actions on Bean Initialization or Destruction then Spring abstract Interface InitializingBean and DisposableBean are for You !

    InitializingBean is abstract interface which has abstract method called afterPropertiesSet. If You have any bean which implements this interface then after all bean properties are set, this afterPropertiesSet method will be called.

    DisposableBean is abstract Interface which has abstract method called destroy. If You have any bean which implements this interface then after bean is released by Spring container, this method will be called.

    Java Program to Find Fibonacci Series of Given Number

    Fibonacci? Consider requirement that I want to display Fibonacci series of number 10.If You remember maths in school, we used to find Fibonacci number using below :

    Fibonacci is sum of previous two Fibonacci numbers fn= fn-1+ fn-2

    Fibonacci of 10 will be 0 1 1 2 3 5 8 13 21 34  ex. 0 1 (0+1) (1st+2nd)...

    How to Calculate Factorial of Number using Java with Recursion

    Factorial? let me show you how we calculate factorial of given number. Consider requirement that I want to find factorial of number 5.

    If you remembered Maths, in School we used to find factorial using below match function !
    n! = n * (n-1)!

    Ex. 5! = 5*(5-1)! = 5*4!
          4! = 4 *(4-1)! = 4*3!
          3! = 3*(3-1)! = 3*2!
          2!= 2*(2-1)! = 2*1!
          1! = 1*(1-0)! =  1

    How to Track Session Attribute - HttpSessionAttributeListener Example

    If You want to track session Attribute like whether there is new session attribute added,removed or replaced during application lifecycle then HttpSessionAttributeListener is for You !

    HttpSessionAttributeListener is abstract interface which comes as part of servlet-api.jar which has abstract method such as attributeAdded,attributeRemoved,attributeReplaced.

    How it works : 
    • When new session attribute is added using set then attributeAdded will be called.
    HttpSession session = request.getSession();
    session.setAttribute("sessionName","LoginIn");
    
    • If you try to set attribute value to some different value which is already present then attributeReplaced will be called. ex. session.setAttribute("sessionName","LoggedIn");

    How to Calculate Total Active Session - HttpSessionListener Example

    If you have web application and you want to count total number of active session then HttpSessionListener is for you !

    HttpSessionListener is abstract interface provided as part of servlet-api.jar which has abstract method such as sessionCreated and sessionDestroyed which get executed every time when we create new session using httpSession or invalidate session.

    By default one session is created. so when you use HttpSession session = request.getSession(); in HttpServlet then it will give you that session. You can invalidate session using session.invalidate();

    Listener - ServletContextListener Example

    If You want to do something before application starts, then ServletContextListener is for you.

    ServletContextListener is abstract interface provided as part of servler-api.jar which provides contextDestroyed and contextInitialized abstract method. One can create class which implements ServletContextListener and write implementation logic for example, logic that does something before web application starts.