您的位置:首页 > 编程语言 > Java开发

Spring使用@PostConstruct注解完成子类注入父类私有变量

2017-08-10 09:11 281 查看
原博在此:http://www.cnblogs.com/walson/p/3897661.html

讲了三个使用注解在子类中替换父类私有变量的方法,其中@PostConstruct方法有误。

注解使用方式应为如下:

父类:

public class BaseServiceImpl {
private BaseDao baseDao;

public BaseDao getBaseDao() {
return baseDao;
}

public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
}

public void test(){
System.out.println("TsetService...... ");
baseDao.test();
}
}


子类:

@Service("testServiceImpl")
public class TestServiceImpl extends BaseServiceImpl{

private TestDao testDao;

public TestDao getTestDao() {
return testDao;
}

@Resource(name="testDao")
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}

@PostConstruct
public void initBaseDao() {
super.setBaseDao(this.getTestDao());
}

}


注意:PostConstruct注解不可以有参数的,否则就会报错:java.lang.IllegalStateException: Lifecycle method annotation requires a no-arg method
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐