public class Apple
{
private boolean isJuicy;
private boolean isRipe;
public Apple(boolean isJuicy)
{
isJuicy = isJuicy;
}
public void setRipe(boolean ripe)
{
this.isRipe = ripe;
}
public boolean isRipe()
{
return this.isRipe;
}
}
在Spring,我有两个服务类AppleService
& FruitService
。
AppleService
public Apple createApple(boolean isJuicy)
{
Apple apple = null;
apple = new Apple(isJuicy);
fruitService.saveApple(apple);
log("ripe:" + apple.isRipe()); // false
return apple;
}
FruitService
public void saveApple(Apple apple)
{
if(apple.isJuicy())
apple.setRipe(true);
persist(apple);
log("ripe:" + apple.isRipe()); // true
}
为什么Apple报告在FruitService
但在AppleService
中没有成熟?
我认为通过引用FruitService
传递的事实保证了AppleService中apple
对象中应该存在相同的对象和对象属性吗?
答案 0 :(得分:1)
我认为问题是这样的:当isJuicy作为true传入时,你没有在构造函数中将isRipe设置为true。在java中,unset布尔值的默认值为false。 这将解决它:
private boolean isRipe;
public Apple(boolean isJuicy) {
this.isJuicy = isJuicy; // Note: added "this.", otherwise your assignment just sets the parameter to itself
isRipe = isJuicy; // Set isRipe too
}
答案 1 :(得分:0)