标签:style color java 使用 os strong 工作 res
通过 super.setBaseDao方法设置父类私有变量
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{
@Resource(name="testDao")
public void initBaseDao(TestDao testDao) {
super.setBaseDao(testDao);
}
}
@PostConstruct是Java EE 5引入的注解,Spring允许开发者在受管Bean中使用它。当DI容器实例化当前受管Bean时,@PostConstruct注解的方法会被自动触发,从而完成一些初始化工作
父类同上
@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(TestDao testDao) {
super.setBaseDao(testDao);
}
}
父类被注入对象非私有,子类重写setter方法注入到父类变量
public class BaseServiceImpl {
protected 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{
@Resource(name="testDao")
public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
}
}
spring使用注解通过子类注入父类的私有变量,布布扣,bubuko.com
标签:style color java 使用 os strong 工作 res
原文地址:http://www.cnblogs.com/walson/p/3897661.html