版权声明: https://blog.csdn.net/qq_38386316/article/details/88743742
1.Component And Annotations
- Circle.java
package org.zcs.spring;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class Circle implements Shape {
private Point center;
public Point getCenter() {
return center;
}
@Resource
public void setCenter(Point center) {
this.center = center;
}
@PostConstruct
public void initlize() {
System.out.println("initlizing a circle ");
}
@PreDestroy
public void destroy() {
System.out.println("destroy a circle");
}
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Drawing Circle:");
System.out.println("Circle : point is : " + center.getX() +"," + center.getY());
}
}
- Triangle.java
package org.zcs.spring;
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("This is PointA: (" +pointA.getX() +"," + pointA.getY() +")");
System.out.println("This is PointB: (" +pointB.getX() +"," + pointB.getY() +")");
System.out.println("This is PointC: (" +pointC.getX() +"," + pointC.getY() +")");
}
}
- 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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<!--
<bean id = "triangle" class= "org.zcs.spring.Triangle" autowire= "byName" >
<property name="pointA" ref = "pointA"></property>
<property name="pointB" ref = "pointB"></property>
<property name="pointC" ref = "pointC"></property>
</bean> -->
<bean id= "pointA" class = "org.zcs.spring.Point">
<qualifier value = "circleRelated"></qualifier>
<property name="x" value = "${pointA.pointX}"></property>
<property name="y" value = "${pointA.pointY}"></property>
</bean>
<bean id= "pointB" class = "org.zcs.spring.Point">
<property name="x" value = "20"></property>
<property name="y" value = "0"></property>
</bean>
<bean id= "center" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "20"></property>
</bean>
<!-- <bean id = "circle" class= "org.zcs.spring.Circle" >
</bean> -->
<context:component-scan base-package="org.zcs.spring"></context:component-scan>
<bean class = "org.zcs.spring.MyBeanFactory" ></bean>
<bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value = "pointsconfig.properties"></property>
</bean>
<bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
</bean>
</beans>
学习记录
- 学习了一个神奇的注释:component,
<context:component-scan base-package="org.zcs.spring"></context:component-scan>
,直接就能显示运行circle.