62,620
社区成员
发帖
与我相关
我的任务
分享
这怎么解决???
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Properties;
public class Volume {
// 测试用例
public static void main(String[] args) {
// 1. 读取配置文件
Properties prop = new Properties();
try {
prop.load(Volume.class.getClassLoader().getResourceAsStream("Test.properties"));
// prop.load(new FileInputStream("src/test/resources/Test.properties"));
} catch (IOException e) {
e.printStackTrace();
return;
}
// 2. 读取配置信息
final String KEY_IMPL_CLASS = "Geometry.implementation";
String implClassName = prop.getProperty(KEY_IMPL_CLASS);
Geometry implOjbect = null;
try {
// 3. 找到配置的实现类
Class<? extends Geometry> implClass = (Class<? extends Geometry>) Class.forName(implClassName);
implOjbect = implClass.newInstance();
for (Field field : implClass.getDeclaredFields()) {
// 4. 分析实现类的属性结构,查找属性的配置信息。
String keyFieldName = implClassName + "." + field.getName();
String strValueField = prop.getProperty(keyFieldName);
// 5. 设置属性值
setFieldValue(implOjbect, field, strValueField);
}
} catch (Exception e) {
e.printStackTrace();
}
double height = 5;
Pillar pillar = new Pillar(implOjbect, height);
System.out.println("体积是:"+pillar.getVolume());
}
private static void setFieldValue(Geometry implOjbect, Field field, String strValueField) throws IllegalAccessException {
// 将配置文件中的配置信息设置到对象当中
// 由于本例中,所有的属性类型都是double类型,就没有对属性类型进行区分转换,统一将string转成double
field.setAccessible(true);
field.set(implOjbect, Double.parseDouble(strValueField));
}
}
/*接口Geomotry*/
interface Geometry{
double getArea();
}
/*定义Pillar类*/
class Pillar{
private final Geometry bottom;
private final double height;
public Pillar(Geometry bottom,double height) {
this.bottom=bottom;
this.height=height;
}
public double getVolume() {
return bottom.getArea()*height;
}
}
/*定义Circle类实现接口*/
class Circle implements Geometry{
private double radius;
public double getArea() {
return radius * radius * Math.PI;
}
public void setRadius(double r) {
this.radius=r;
}
}
/*定义Rectangle类*/
class Rectangle implements Geometry{
private double length;
private double width;
public double getArea() {
return length * width;
}
public void setLength(double a) {
this.length = a;
}
public void setWidth(double b) {
this.width=b;
}
}
Test.properties
## 配置文件的使用:
## Geometry.implementation 指定实现类的名字
## 实现类的名字作为前缀,加上点号,加上属性名 整体作为key,设置属性值
## 一个配置文件只允许出现一个 Geometry.implementation ,即只能有一个实现类的配置会生效。
Geometry.implementation=Circle
Circle.radius=2
#Geometry.implementation=Rectangle
Rectangle.length=3
Rectangle.width=4
public interface Geometry {
public double getArea();
}
public class Circle implements Geometry {
private double r;
final double π=3.1415926;
public double getArea() {
return π*r*r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
public class Rectangle implements Geometry {
private double height;
private double weight;
public double getArea() {
return height*weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
public class BeanTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Geometry g1=ctx.getBean("circle", Geometry.class);
System.out.println("圆面积为:"+g1.getArea());
Geometry g2=ctx.getBean("rectangle", Geometry.class);
System.out.println("长方形面积为:"+g2.getArea());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素,使用spring-beans.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="circle" class="domain.Circle">
<property name="r" value="3.0" />
</bean>
<bean id="rectangle" class="domain.Rectangle">
<property name="height" value="4.0" />
<property name="weight" value="5.0" />
</bean>
</beans>