Professional Documents
Culture Documents
Farsi
Farsi
Spring
FrameWork Spring
J2EE .
Spring dependency injection .
XML) (beans
XML
)(
XML
Inversion Of Control IOC .
Spring
Spring
. XML .
Spring
FrameWok Spring container
. Spring :
http://www.springsource.org/download
.
.
:
) (concrete interface
) (instance bean
bean ).
driver class .
Spring Intellij
lib.zip . zip
commons-logging.jar
spring.jar
lib .
lib .
;package 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;
}
}
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