@component is used to define a bean in XML with class name,
which is equivalent to below bean
<bean
id="circle" class="org.yash.watertechsol.Circle">
</bean>
By adding @Component annotation in class you can avoid bean
definition in XML. You need to add below property in bean xml to make
@Component annotation work.
<context:component-scan
base-package="org.yash.watertechsol"></context:component-scan>
The disadvantage of @component is you do not have multiple
instance of that class. Suppose you want to have different instances of Circle
like Point object then it is not possible by using @Component class you need to
use XML configuration.
The concept of stereo type annotation is like this, when you
write enterprise application you would have some standard spring beans that
performs some stand rules, you would have data object, you would have service
object, view, controller, so all these are stereo typical roles perform in many
enterprise application. So what spring has done, it has some annotations, which
defines one of the particular beans to perform one of the roles.
@Service : To define service class
@Repository : To define data object
@Controller : To define
controller(view)
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">
<!-- <bean
id="triangle" class="org.yash.watertechsol.Triangle">
<property
name="pointA" ref="pointA"></property>
<property
name="pointB" ref="pointB"></property>
<property
name="pointC" ref="pointC"></property>
</bean> -->
<!--
<bean id="circle"
class="org.yash.watertechsol.Circle">
</bean>
-->
<bean id
="pointA"
class="org.yash.watertechsol.Point">
<property name="x"
value="0" />
<property name="y"
value="0" />
</bean>
<bean id
="pointB"
class="org.yash.watertechsol.Point">
<property name="x"
value="-20" />
<property name="y"
value="0" />
</bean>
<bean id
="pointC"
class="org.yash.watertechsol.Point">
<property name="x"
value="0" />
<property name="y"
value="20" />
</bean>
<context:annotation-config />
<context:component-scan base-package="org.yash.watertechsol"></context:component-scan>
</beans>
Circle.java
package org.yash.watertechsol;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
@Component
public class Circle implements Shape {
private
Point center;
@Override
public
void draw() {
System.out.println("Drawing
Circle");
System.out.println("("+center.getX()+",
"+center.getY()+")");
}
public
Point getCenter() {
return
center;
}
@Resource(name="pointC")
public
void setCenter(Point center) {
this.center
= center;
}
@PostConstruct
public
void initCircle(){
System.out.println("Init
of Circle");
}
@PreDestroy
public
void destroyCircle(){
System.out.println("Destroy
of Circle");
}
}
Shape.java
package org.yash.watertechsol;
public interface Shape {
public void draw();
}
Triangle.java
package org.yash.watertechsol;
public class Triangle implements Shape{
private Point pointA;
private Point pointB;
private Point pointC;
public Point
getPointA() {
return pointA;
}
public void setPointA(Point
pointA) {
this.pointA = pointA;
}
public Point
getPointB() {
return pointB;
}
public void setPointB(Point
pointB) {
this.pointB = pointB;
}
public Point
getPointC() {
return pointC;
}
public void setPointC(Point
pointC) {
this.pointC = pointC;
}
public void draw(){
System.out.println("Drawing
Triangle");
System.out.println("Point A
= (" + getPointA().getX()+", "+getPointA().getY()+")");
System.out.println("Point B
= (" + getPointB().getX()+", "+getPointB().getY()+")");
System.out.println("Point C
= (" + getPointC().getX()+", "+getPointC().getY()+")");
}
}
Point.java
package org.yash.watertechsol;
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
DrawingApp.java
package org.yash.watertechsol;
import
org.springframework.context.support.AbstractApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public
static void main(String[] args){
AbstractApplicationContext
context = new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();
Shape
shape = (Shape)context.getBean("circle");
shape.draw();
}
}
No comments:
Post a Comment