Thursday, April 26, 2012

Simple Logging Facade for Java (SLF4J)

             The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.
Before you start using SLF4J, we highly recommend that you read the two-page SLF4J user manual.

Note that SLF4J-enabling your library implies the addition of only a single mandatory dependency, namely slf4j-api.jar. If no binding is found on the class path, then SLF4J will default to a no-operation implementation.

SLF4J and and binding against a backend logger
You can only bind against one backend logging framework. With no binding framework on the classpath, the silent logger (NOP) will be used by default.
The frameworks supported by SLF4J:
  • Logback-classic
  • Log4J
  • java.util.logging (JUL)
  • Simple
  • NOP
  • Jakarta Commons Logging (JCL)
The preferred backend logging framework with SLF4J is Logback.

//TestSlf4jClient.java
 


Check all the Project Explorer:
Note: slf4j-api-X.x.x.jar API only exposed to Application by abstracting the back-end logging framework.

 Logback
Logback is written by the same people who have written SLF4J. It natively implements the SLF4J API.
Their description of the framework:
The logback-classic module can be assimilated to a significantly improved version of log4j
A nice feature with Logback is that you can see which jar file that contains a class in the stacktrace. And if the jar file contains an Implemented-Version property in the /META-INF/MANIFEST.MF file, then you can see the version number to the right for the jar file name.

Check out all back-end logging framework outputs:
Logback-Output :
Log4J-Output:

Simple-Output: 
 NOP-Output:
  java.util.logging (JUL)-Output:

Sunday, April 15, 2012

Spring Java Configuration - in shot

JavaConfig simply provides another mechanism to configure the Spring IoC container, this time in pure Java rather than requiring XML to get the job done

But, you are still allowed to use the classic XML way to define beans and configuration, the JavaConfig is just another alternative solution.
See the different between classic XML definition and JavaConfig to define a bean in Spring container.

Spring XML file: 
 
 
Equivalent configuration in JavaConfig:


package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.HelloWorld;
import com.HelloWorldImpl;

@Configuration
public class AppConfig {

    @Bean(name="helloBean")
    public HelloWorld helloWorld() {
        return new HelloWorldImpl();
    }
}

now
Spring JavaConfig Sample Java Project-
 
1.  We need to write an Interface first-

package com;
public interface EmployeeAction {
    void printHelloWorld(String msg);
}


2.  Provide the Implementation –

package com; 
public class EmployeeActionImpl implements EmployeeAction {    
private EmployeeService employeeService;     
public EmployeeActionImpl(EmployeeService employeeService) {
        super();
        this.employeeService = employeeService;
    }

    @Override
    public void printHelloWorld(String msg) {
        employeeService.printHelloWorld(msg);
       
    }
 }



3.  Interface for Service
package com;

public interface EmployeeService {

    void printHelloWorld(String msg);
}

4.     Providing Implementation for the above Service

package com;

public class EmployeeServiceImpl implements EmployeeService {

    /* (non-Javadoc)
     * @see com.EmployeeService#printHelloWorld(java.lang.String)
     */
    @Override
    public void printHelloWorld(String msg) {
        System.out.println("Hello Thanooj, Welcome to " + msg);
    }
}




 
/**
 * Configuring JavaConfig class
 */
package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.EmployeeAction;
import com.EmployeeActionImpl;
import com.EmployeeService;
import com.EmployeeServiceImpl;
/**
 * @author thanooj
 *
 */
public @Configuration class AppConfig {
      
    public @Bean EmployeeAction employeeAction() {
        return new EmployeeActionImpl(employeeService());
    }
    public @Bean EmployeeService employeeService() {
        return new EmployeeServiceImpl();
    }
 }
 
Now, working with Client program-

package com.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.EmployeeAction;
import com.config.AppConfig;

public class Client {
   
    public static void main(String[] args) {

        /* ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); */

        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        EmployeeAction empObj = (EmployeeAction) context.getBean("employeeAction");
        empObj.printHelloWorld("Spring3 Java Config world.");

    }
}
                      output:
  ----------------------------------------------------------------------------------
 Now
Spring JavaConfig web based sample
 
 
package com;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EmployeeActionImpl implements EmployeeAction {

    private final Log LOGGER  = LogFactory.getLog(getClass());
    private EmployeeService employeeService;
   
    public EmployeeActionImpl(EmployeeService employeeService) {
        super();
        this.employeeService = employeeService;
    }

    @Override
    public String getWelcomeString() {
        LOGGER.info("inside EmployeeActionImpl getWelcomeString()");
        return employeeService.getWelcomeString();
    }
}

package com;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EmployeeServiceImpl implements EmployeeService {
   
    private final Log LOGGER  = LogFactory.getLog(getClass());
   
    @Override
    public String getWelcomeString() {
        LOGGER.info("inside EmployeeServiceImpl getWelcomeString()");
        return "Spring 3 MVC Hello World NEW";
    }
}

package com;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.EmployeeAction;

@Controller
public class HelloController {
   
    private final Log LOGGER  = LogFactory.getLog(getClass());
    @Autowired
    private EmployeeAction employeeAction;
   
    @RequestMapping("/welcome")
    public ModelAndView getWelcomeString() {
        LOGGER.info("inside HelloController getWelcomeString()");
        String msg = employeeAction.getWelcomeString();
        LOGGER.info("return value of getWelcomeString() : "+msg);
        return new ModelAndView("message", "msg", msg);
    }
}
-----------------------

 
package config;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.EmployeeAction;
import com.EmployeeActionImpl;
import com.EmployeeService;
import com.EmployeeServiceImpl;


public @Configuration class AppConfig {
   
    private final Log LOGGER  = LogFactory.getLog(getClass());
   
    public @Bean EmployeeAction employeeAction() {
        LOGGER.info("inside AppConfig EmployeeAction");
        return new EmployeeActionImpl(employeeService());
    }
    public @Bean EmployeeService employeeService() {
        LOGGER.info("inside AppConfig EmployeeService");
        return new EmployeeServiceImpl();
    }
   }
------------------ 
                                       mvc-dispatcher-servlet.xml
 
web.xml
message.jsp

output:
References:
http://www.mkyong.com/spring3/spring-3-javaconfig-example/
http://blog.springsource.org/2008/03/26/spring-java-configuration-whats-new-in-m3/