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/


No comments: