Spring
supports 4 sterio type annotations like Component, Controller, Repository,
Service. Tshese are used for automatic bean detection using classpath scan in
Spring framework.
Annotation
|
Description
|
Component
|
Indicates
that an annotated class is a "component".
|
Controller
|
Indicates
that an annotated class is a "Controller" (e.g. a web controller).
|
Repository
|
Indicates
that an annotated class is a "Repository" (or "DAO").
|
Service
|
Indicates
that an annotated class is a "Service" (e.g. a business service
facade).
|
Technically
there is not much difference between these annotations, Conceptually these
annotations makes beans clear separation. @Component is a generic stereotype
for any Spring-managed component. @Repository, @Service, and @Controller are
specializations of @Component for more specific use cases, for example, in the
persistence, service, and presentation layers, respectively.
Below
example uses Component annotation.
Step 1
: Create new
maven project “spring_tuorial”. Project structure looks like below.
Step 2
: Update
“pom.xml” file for maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring_tutorial</groupId> <artifactId>spring_tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>spring_tutorial</name> <description>spring_tutorial</description> <properties> <org.springframework-version>4.1.5.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> </dependencies> </project>
Step
3: Create new
package “com.springtutorial.model” under “src/main/java”.
Step
4: Create
“Address” class under the package “com.springtutorial.model”.
package com.springtutorial.model; import org.springframework.stereotype.Component; @Component public class Address { private String street; private String city; private String state; private String country; private String pin; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getPin() { return pin; } public void setPin(String pin) { this.pin = pin; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Address [street=").append(street).append(", city=").append(city) .append(", state=").append(state).append(", country=").append(country).append(", pin=") .append(pin).append("]"); return builder.toString(); } }
Step
5: Create
“Employee” class under the package “com.springtutorial.model”.
package com.springtutorial.model; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Employee { private String firstName; private String lastName; private int id; @Autowired private Address address; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", id=" + id + ", address=" + address + "]"; } }
Step
6: Create
“spring.xml” file in “src/main/resources”
spring.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.springtutorial" /> </beans>
"spring.xml"
is used to assign unique IDs to different beans, to control the creation of
objects with different values. You have to make sure that “spring.xml” file is
available in CLASSPATH and use the same name in main application while creating
application context as shown in Main.java file.
<context:component-scan
base-package="com.springtutorial" />
This tag
will do an auto scanning. Assuming each class that has to become a bean is
annotated with a correct annotation like @Component (for simple bean) or
@Controller (for a servlet control) or @Repository (for DAO classes) and these
class are somewhere under the package com.springtutorial, spring will find all
of these and create a bean for each one. This is done in 2 scans of the classes
- first time it just searches for classes that need to become a bean and maps
the injections it needs to be doing, and on the second scan it injects the
beansa.
Step 7
: Create package
“com.springtutorial.main” under “src/main/java”.
Step 8
: Create Main
class under package “com.springtutorial.main”.
package com.springtutorial.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.springtutorial.model.Employee; public class Main { public static void main(String args[]) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Employee emp = context.getBean(Employee.class); emp.setId(553); emp.setFirstName("Hari Krishna"); emp.setLastName("Gurram"); emp.getAddress().setCity("Ongole"); emp.getAddress().setCountry("India"); emp.getAddress().setPin("523169"); emp.getAddress().setState("AP"); emp.getAddress().setStreet("abcd"); System.out.println(emp); ((ClassPathXmlApplicationContext) context).close(); } }
Step
9: Run Main.java,you
will get error like below.
Apr 03, 2015 10:26:25 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@249e4f: startup date [Fri Apr 03 22:26:25 IST 2015]; root of context hierarchy Apr 03, 2015 10:26:25 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring.xml] Employee [firstName=Hari Krishna, lastName=Gurram, id=553, address=Address [street=abcd, city=Ongole, state=AP, country=India, pin=523169]] Apr 03, 2015 10:26:25 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@249e4f: startup date [Fri Apr 03 22:26:25 IST 2015]; root of context hierarchy
No comments:
Post a Comment