Spring Annotation

Introduction

The most common annotation in Spring would include @Component, @Repository, @Service and @Controller.

@Component is the generic term for Spring Bean. @Repository, @Service and @Controller are subclass of @Component. They have specific usage in Spring Framework. @Repository represent a Spring Bean in Persistent Layer. @Service represent a Spring Bean in Service Layer.@Controller represents a Spring Bean in Presentation Layer.

@Controller

@Controller specify a Class as a Spring Bean in Presentation Layer of Spring Web MVC Framework. It would work with other annotations such as @RequestMapping, @PathVariable, @RequestParam, @MatrixVariable, @RequestHeader, @RequestPart, @SessionAttribute, @RequestAttribute, @ModelAttribute, @ResponseBody.

By default, the scope of Spring Bean annotated with @Controller is Singleton

@RequestMapping and @ResponseBody

@RequestMapping could be place above a class or a method. It would specify a request method or URL that would be handled. @ResponseBody specify the return type would be written to HTTP response body.

@Controller
public class ExampleController {

  @RequestMapping("/")
  @ResponseBody
  public String handle(){
    return "Hello World";
  }
}

@Repository

@Repository specify a class as a Spring Bean of Data Access Object (DAO) in Spring Frameowork. It provide support for DataAccessException which is a generic exception for technology-specific exception such as SQLException, HibernateException and JDOExcpetion. It provide access to DataSource through @Autowired, @Inject, @Resource, @PersistenceContext. Also,  Spring Framework would handle the transaction management in its Data Access Object annotated with @Repository.

@Service

@Service is more specific than @Component. It indicate the class as a Business Service Facade. Currently, no specific support is added @Service to make it very different from @Component.

Spring JBCD integrated with H2

Prerequisite 

  • spring-JDBC-X.X.X.jar
  • spring-beans-x.x.x.jar (Spring Core Framework)
  • spring-context-x.x.x.jar (Spring Core Framework)
  • spring-expression-x.x.x.jar (Spring Core Framework)
  • spring-core-x.x.x.jar (Spring Core Framework)
  • h2-x.x.x.jar (H2 Database)

 

ItTechOfficeBeans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="springExampleBean1" class="com.ittechoffice.spring.SpringExampleBean1">
 <property name="name" value="It Tech Office Spring Example Bean 1" />
 </bean>

 <bean id="dataSource"
 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="org.h2.Driver" />
 <property name="url" value="'jdbc:h2:/ITTechOfficeH2Db" />
 <property name="username" value="sa" />
 <property name="password" value="" />
 </bean>
</beans>

SpringExampleBean.java

package com.ittechoffice.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;



public class SpringExampleBean1 {
 private String name;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }
 
 public static void main(String[] args){
 ApplicationContext context = new ClassPathXmlApplicationContext("ItTechOfficeBeans.xml");
 SpringExampleBean1 springBean1 = (SpringExampleBean1) context.getBean("springExampleBean1");
 System.out.println("Name of springExampleBean1 is \"" + springBean1.getName() + "\"");
 DriverManagerDataSource dataSource = (DriverManagerDataSource) context.getBean("dataSource");
 }
}

 

 

 

 

 

Spring Example – Hello World Spring

ItTechOfficeBeans.xml (Path: src)

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="springBean1" class="com.ittechoffice.spring.SpringBean1">
 <property name="name" value="ItTechOffice1" />
 </bean>

</beans>

SpringContainer.java (Path: src/com/ittechoffice/spring)

package com.ittechoffice.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContainer {

 public static void main(String[] args){
 ApplicationContext context = new ClassPathXmlApplicationContext("ItTechOfficeBeans.xml");
 SpringBean1 springBean1 = (SpringBean1) context.getBean("springBean1");
 System.out.println(springBean1.getName());
 }
}

SpringBean1 (Path: src/com/ittechoffice/spring)

package com.ittechoffice.spring;

public class SpringBean1 {
 private String name;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }
 
 
}

Output

INFO Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ea3c69a: startup date [Fri Apr 08 09:50:59 CST 2016]; root of context hierarchy
INFO Loading XML bean definitions from class path resource [ItTechOfficeBeans.xml]
ItTechOffice1