×
☰ See All Chapters

Spring @Service Annotation

@Service annotation can be called as specialized form of @Component annotation. By annotating a class with @Service annotation we indicate that a class is a "Business Service Facade" (in the Core J2EE patterns sense), or something similar. There is no any other specialty from @Component annotation except using it in the service layer.

Let’s create a simple spring application where we use @Service annotation.

Spring @Service Annotation Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_Service</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

PrintService.java

package com.java4coding.service;

 

public interface PrintService {

       

        public abstract void printUppercase(String s);

       

        public abstract void printLowercase(String s);

}

PrintServiceImpl.java

package com.java4coding.service.impl;

 

import org.springframework.stereotype.Service;

import com.java4coding.service.PrintService;

 

@Service

public class PrintServiceImpl implements PrintService {

 

        public void printUppercase(String s) {

                System.out.println(s.toUpperCase());

        }

 

        public void printLowercase(String s) {

                System.out.println(s.toLowerCase());

        }

}

SpringServiceAnnotationExample.java

package com.java4coding;

 

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

 

import com.java4coding.service.PrintService;

 

@Configuration

@ComponentScan(basePackages = { "com.java4coding.service" })

public class SpringServiceAnnotationExample {

        public static void main(String[] args) {

                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(

                                SpringServiceAnnotationExample.class);

                PrintService printService = (PrintService)ctx.getBean("printServiceImpl");

                printService.printLowercase("Hello");

                printService.printUppercase("Hello");

        }

}

 

Output:

spring-service-annotation-0
 

All Chapters
Author