Friday 12 May 2017

Spring: Stereotypes

Stereo types are used for or automatic bean detection using classpath scan. Spring provide four stereo type annotations.
a.   Repository
b.   Component
c.   Service
d.   Controller

@Component is a generic stereotype for any Spring-managed component, where as @Repository, @Service, @Controller are specializations of @Component annotations, serves different purpose.

@Repository is used in Persistance layer
@Service is used in Service layer
@Controller is used in Presentation layer.

Automatically detect stereo typed classes
Define the class using one of the stereo type and specify the packages that these stereotyped classes present tp spring. Spring scans all the packages and autowire all the stereotyped beans. By using ComponentScan annotation, you can specify the packages to be scanned.

Example
@ComponentScan(basePackages = "org.example")

Following is the complete wotking application.

TimeCalculator.java
package com.sample.stereotypesDemo;

import org.springframework.stereotype.Component;

@Component
public class TimeCalculator {
 
 public long secondsFromHours(int hours){
  return hours * 3600;
 }
 
 public long minutesFromHours(int hours){
  return hours * 60;
 }
}

ApplicationConfig.java
package com.sample.configurations;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.sample.stereotypesDemo")
public class ApplicationConfig {
 
}

HelloWorld.java
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.sample.configurations.ApplicationConfig;
import com.sample.stereotypesDemo.TimeCalculator;

public class HelloWorld {

 public static void main(String args[]) {
  ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);

  TimeCalculator calc = context.getBean(TimeCalculator.class);
  
  System.out.println("Minutes per hour : " + calc.minutesFromHours(1));
  ((AnnotationConfigApplicationContext) context).close();
 }
}
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.sample.configurations.ApplicationConfig;
import com.sample.stereotypesDemo.TimeCalculator;

public class HelloWorld {

 public static void main(String args[]) {
  ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);

  TimeCalculator calc = context.getBean(TimeCalculator.class);
  
  System.out.println("Minutes per hour : " + calc.minutesFromHours(1));
  ((AnnotationConfigApplicationContext) context).close();
 }
}

Output
Minutes per hour : 60

If you are using configuration file, then you can specify the packages to scan using ‘context:component-scan’ element.

Example
<context:component-scan base-package="com.sample.stereotypesDemo" />

myConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

 <context:annotation-config />

 <context:component-scan base-package="com.sample.stereotypesDemo" />
</beans>


HelloWorld.java
package com.sample.test;

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

import com.sample.stereotypesDemo.TimeCalculator;

public class HelloWorld {

 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  TimeCalculator calc = context.getBean(TimeCalculator.class);
  
  System.out.println("Minutes per hour : " + calc.minutesFromHours(1));
  ((ClassPathXmlApplicationContext) context).close();
 }
}

Output
Minutes per hour : 60

How to specify multiple packages to scan?
you can specify a comma/semicolon/space-separated list that includes the parent package of each class.


Previous                                                 Next                                                 Home

No comments:

Post a Comment