You are on page 1of 5

- Spring

Spring
FrameWork Spring

J2EE .
Spring dependency injection .

XML) (beans


XML

)(
XML
Inversion Of Control IOC .

spring (AOP(Aspect Oriented Programming


. source code .

.

Spring

Spring
. XML .

Spring
FrameWok Spring container
. Spring :

http://www.springsource.org/download

IntelliJ V12.0 IDE community .


community IDE jetbrains.com .

Loose Coupling Spring






.
.
:

interface abstract class

) (concrete interface

) (instance bean

bean ).

driver class .
Spring Intellij
lib.zip . zip

commons-logging.jar
spring.jar
lib .

lib .

File project structure libraries


.
interface

;package spring

{ public interface Shape


;)(public double getArea
}
interface spring.

: Shape interface
Rectangle

package spring;
public class Rectangle implements Shape {
double width; double height;
public Rectangle(){}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}

(bean(bean Definition file


Proprty
Rectangle
setter getter .
bean . setWidth
.

public double getHeight() {


return height;
}
public void setHeight(double height) {
this.height = height;
}
@java.lang.Override
public double getArea() {
return width * height;
}
}
Circle
package spring;
public class Circle implements Shape {
private double radius = 1.0;
public Circle() {}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;

bean Definition file


Circle


.

}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI* (Math.pow(radius, 2
));
}
}
(bean(bean definition file
<?xml version="1.0" encoding="UTF-8"?>
<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/springbeans.xsd">
)( new Rectangle
<bean id="shape1" class="spring.Rectangle">
<property name="width" value="10"/>
<property name="height" value="20"/>
</bean>
<bean id="shape2" class="spring.Circle">
<constructor-arg value="10"/>
</bean>
</beans>

Rectangle
( setWidth(10
( setHeight(20

) new Circle(10
. Circle

Driver

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

src

.
public class ShapeTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
Shape shape1 = (Shape) context.getBean("shape1");
printInfo(shape1);
Shape shape2 = (Shape) context.getBean("shape2");
printInfo(shape2);


. xml

}
private static void printInfo(Shape shape) {
System.out.printf("Area of %s is %.2f%n",
shape.getClass().getSimpleName(),
shape.getArea());
}
}

Area of Rectangle is 200.00
Area of Circle is 314.16

You might also like