spring注入,
空指针异常,看了好多帖子忍找不到办法,求大神指点
以下是我自己做的例子:使用全注解的形式
DAO层:
package Impl.DAOImpl;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import DAO.RegionDAO;
import Entity.Region;
@Repository(value="regionDAOimpl")
public class RegionDAOImpl implements RegionDAO {
@Resource
private RegionDAO regionmapper;
public RegionDAO getRegionmapper() {
return regionmapper;
}
/*public void setRegionmapper(RegionDAO regionmapper) {
this.regionmapper = regionmapper;
}*/
public Region selectRegion(int code) {
return regionmapper.selectRegion(code);
}
public int updateRegion(int code) {
return regionmapper.updateRegion(code);
}
public void deleteRegion(int code) {
regionmapper.deleteRegion(code);
}
public int insertRegion(Region region) {
return regionmapper.insertRegion(region);
}
public void test2() {
System.out.println("test2");
}
}
Service层:
package Impl.ServiceImpl;
import java.net.URI;
import javax.annotation.Resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import DAO.RegionDAO;
import Entity.Region;
import SERVICE.RegionService;
@Service
@Path("/region")
public class RegionServiceImpl implements RegionService {
@Context UriInfo uriInfo;
@Resource(name="regionDAOimpl")//不需要set方法了
private RegionDAO regionDAOimpl;
/*public void setRegionDAOimpl(RegionDAO regionDAOimpl) {
this.regionDAOimpl = regionDAOimpl;
}*/
public RegionDAO getRegionDAOimpl() {
return this.regionDAOimpl;
}
@Path("/{id}")
@GET
@Produces(MediaType.APPLICATION_XML)
public Region findRegion(@PathParam("id") String id) {
System.out.println("10");
int code = Integer.valueOf(id);
System.out.println(regionDAOimpl == null);
return this.regionDAOimpl.selectRegion(code);
}
@PUT
@Produces("text/xml")
@Consumes(MediaType.APPLICATION_XML)
@Transactional
public int update(JAXBElement<Region> jaxbRegion) {
Region region = jaxbRegion.getValue();
int id = region.getCode();
return this.regionDAOimpl.updateRegion(id);
}
@Path("/{id}")//子资源方法
@Transactional
@DELETE
@Consumes("text/xml")
public void delete(@PathParam("id")int id) {
this.regionDAOimpl.deleteRegion(id);
}
@Transactional
@POST