我在父内部注入子bean时遇到空指针异常。
我的applicationcontext.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: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">
<bean id="icmsUniversityMgmtPM" class="com.cmd.cms.cmsuniversitymgmt.services.CmsUniversityMgmtPM" init-method="initIt" destroy-method="cleanUp">
<property name="beanName" value="abc"></property>
</bean>
<bean id="cmsUniversityService" class="com.cmd.cms.cmsuniversitymgmt.services.impl.CmsUniversityMgmtImpl" init-method="initIt" destroy-method="cleanUp" autowire="byName">
<property name="cmsUniversityMgmtPM" ref="icmsUniversityMgmtPM" />
<property name="cmsMapper" ref="cmsMapper" />
</bean>
<bean name="cmsUniversityDAO" class="com.cmd.cms.cmsuniversitymgmt.dao.impl.CmsUniversityDAOImpl">
</bean>
<bean name="cmsCollegeDAO" class="com.cmd.cms.cmsuniversitymgmt.dao.impl.CmsCollegeDAOImpl">
</bean>
CmsUniversityMgmtImpl
public class CmsUniversityMgmtImpl implements ICmsUniversityMgmt {
Logger log = LoggerFactory.getLogger(getClass());
private IcmsUniversityMgmtPM cmsUniversityMgmtPM;
private DozerBeanMapper cmsMapper;
public IcmsUniversityMgmtPM getCmsUniversityMgmtPM() {
return cmsUniversityMgmtPM;
}
public void setCmsUniversityMgmtPM(IcmsUniversityMgmtPM cmsUniversityMgmtPM) {
this.cmsUniversityMgmtPM = cmsUniversityMgmtPM;
}
public DozerBeanMapper getCmsMapper() {
return cmsMapper;
}
public void setCmsMapper(DozerBeanMapper cmsMapper) {
this.cmsMapper = cmsMapper;
}
public void initIt() throws Exception {
System.out.println("Init method after properties are set : CmsUniversityMgmtImpl" );
}
public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up : CmsUniversityMgmtImpl");
}
@Override
public GetCollegeByIdResponse getCollegeById(GetCollegeByIdRequest parameters) throws CMSException {
// TODO Auto-generated method stub
return null;
}
@Override
public GetAllCollegesResponse getAllColleges(VoidType parameters) throws CMSException {
// TODO Auto-generated method stub
return null;
}
@Override
public SaveUniversityResponse saveUniversity(SaveUniversityRequest parameters) throws CMSException {
System.out.println("Comming inside ............" + "");
try {
System.out.println(cmsUniversityMgmtPM.toString());
if (getCmsUniversityMgmtPM() != null) {
System.out.println("not null");
} else {
System.out.println("it is null");
}
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
return null;
}
@Override
public GetAllUniversitiesResponse getAllUniversities(VoidType parameters) throws CMSException {
// TODO Auto-generated method stub
return null;
}
@Override
public DeleteUniversityByIdResponse deleteUniversityById(DeleteUniversityByIdRequest parameters)
throws CMSException {
// TODO Auto-generated method stub
return null;
}
@Override
public SaveCollegeResponse saveCollege(SaveCollegeRequest parameters) throws CMSException {
// TODO Auto-generated method stub
return null;
}
@Override
public GetUniversityByIdResponse getUniversityById(GetUniversityByIdRequest parameters) throws CMSException {
// TODO Auto-generated method stub
return null;
}
@Override
public DeleteCollegeByIdResponse deleteCollegeById(DeleteCollegeByIdRequest parameters) throws CMSException {
// TODO Auto-generated method stub
return null;
}
}
CmsUniversityMgmtPM
public class CmsUniversityMgmtPM implements IcmsUniversityMgmtPM {
public String beanName;
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public void initIt() throws Exception {
System.out.println("Init method after properties are set CmsUniversityMgmtPM : " + beanName);
}
public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up : CmsUniversityMgmtPM");
}
@Override
public List<University> getAllUniversities() {
// TODO Auto-generated method stub
return null;
}
@Override
public University getUniversityById(int universityId) {
// TODO Auto-generated method stub
return null;
}
@Override
public University getUniversityByName(int universityName) {
// TODO Auto-generated method stub
return null;
}
@Override
public int saveUniversity(University university) throws CMSException {
// TODO Auto-generated method stub
return 0;
}
@Override
public List<University> getAllCollegesByUniversityId() {
// TODO Auto-generated method stub
return null;
}
@Override
public University getCollegeByName(int collegeName) {
// TODO Auto-generated method stub
return null;
}
@Override
public int saveCollege(College college) {
// TODO Auto-generated method stub
return 0;
}
@Override
public College getCollegeById(int collegeId) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Department> getAllDepartmentByCollegeId() {
// TODO Auto-generated method stub
return null;
}
@Override
public Department getDepartmentByName(int departmentName) {
// TODO Auto-generated method stub
return null;
}
@Override
public int saveDepartment(Department department) {
// TODO Auto-generated method stub
return 0;
}
@Override
public College getDepartmentById(int collegeId) {
// TODO Auto-generated method stub
return null;
}
}