框架结构如下:
@Controller
public class LoginAction{ @Autowired
private LoginService loginService;
@Service
public class LoginServiceImpl implements LoginService { @Autowired
private RoleDao roleDao;
@Repository
public class RoleDaoImpl implements RoleDao { @Autowired
private SessionFactory sessionFactory;
@Autowired
private BaseDao baseDao;
@Repository
public class BaseDaoImpl implements BaseDao{
@Autowired
private SessionFactory sessionFactory;
问题出现, 假如我不使用baseDao 直接在roleDao使用sessionFactory进行查询 没有任何问题
[color=#FF0000]各层都会被正常装载,但是当我添加baseDao功能,在roleDao中加载baseDao进行方法调用时,就
出现了 Could not instantiate bean class [com.mvc.base.dao.impl.BaseDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException 的异常 <!--对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.mvc.java">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="com.mvc.base">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> 包扫描问题我也考虑了 base package下只用dao层功能 ,我就疑问,框架各功能应该是正常的,因为有成功的例子,但是在装载其他包内的dao时却出现了无法注入的问题,实在无法找到问题,跪求大神指导springMVC注解注入annotation
@Controller
public class LoginAction{ @Autowired
private LoginService loginService;
@Service
public class LoginServiceImpl implements LoginService { @Autowired
private RoleDao roleDao;
@Repository
public class RoleDaoImpl implements RoleDao { @Autowired
private SessionFactory sessionFactory;
@Autowired
private BaseDao baseDao;
@Repository
public class BaseDaoImpl implements BaseDao{
@Autowired
private SessionFactory sessionFactory;
问题出现, 假如我不使用baseDao 直接在roleDao使用sessionFactory进行查询 没有任何问题
[color=#FF0000]各层都会被正常装载,但是当我添加baseDao功能,在roleDao中加载baseDao进行方法调用时,就
出现了 Could not instantiate bean class [com.mvc.base.dao.impl.BaseDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException 的异常 <!--对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.mvc.java">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="com.mvc.base">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> 包扫描问题我也考虑了 base package下只用dao层功能 ,我就疑问,框架各功能应该是正常的,因为有成功的例子,但是在装载其他包内的dao时却出现了无法注入的问题,实在无法找到问题,跪求大神指导springMVC注解注入annotation
我愿意设计是将所有可能用的查询方法增删改查以及更复杂的查询方法放在impl中 在其他模块的DaoImpl中引入
此BaseDaoImpl调用各个方法 你说的可能是有一些具体的查询问题 当然之前说的这些方法中已经包含了普通的hql查询功能以及sql的查询功能,可以满足任何查询需求,一个通吃,这样会提高开发效率。
如果在实际操作dao中实现此baseDao接口,那岂不是每个daoImpl中都会出现此baseDao中的所有方法,而且要重写这些方法,还是会增加大量的代码量,如果在一个baseDaoImpl中封装好所有方法,那么在所有daoImpl中只需引入就可以类似更方便的方言一样去进行数据库查询(hibernate4之后对回话事物有很好的管理机制,不需要spring来管理了),项目模块量越大,效率提高越明显。
@Repository
public class BaseDaoImpl implements BaseDao{
@Autowired
private SessionFactory sessionFactory;
Session session = sessionFactory.openSession();
原因是Session session = sessionFactory.openSession();
这句话出了空指针错误,我将他放到具体的方法里面就没有问题了,可惜我还是不明白错误在哪里,
原意是将一个impl的实例对应一个session 看来不行 还是得单个查询对应一个session 那位大神知道的告知下原因 感激不尽。