我使用springmvc 4.3.2-RELEASE来构建一个简单的结构。我将配置写入了几个spring - * .xml,当我调试我的应用程序时,它显示我的Controller中的serviceimpl有值,而serviceimpl中的daoimpl没有。
@Controller
@RequestMapping("/role")
public class RoleController {
@Autowired
IRoleInfoService roleInfoService;
@RequestMapping("index")
public String Index(){
return "/role/index";
}
@RequestMapping("getall")
public @ResponseBody List<RoleInfo> getAll() throws SQLException {
return roleInfoService.getAll();
}
}
public class RoleInfoServiceImpl implements IRoleInfoService {
@Autowired
RoleInfoDaoImpl roleInfoDao ;
@Override
public List<RoleInfo> getAll() throws SQLException {
return roleInfoDao.getAll();
}
}
public class RoleInfoDaoImpl implements IRoleInfoDao {
@Override
public List<RoleInfo> getAll() throws SQLException {
return null;
}
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>>
</listener>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="keyy.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp" />
<property name="prefix" value="/WEB-INF/jsp/"/>
</bean>
<bean class="keyy.service.serviceimpl.RoleInfoServiceImpl"></bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<bean class="keyy.dao.datasourcefactory.datasourcefactoryimpl.DataSourceFactory">
</bean>
抱歉,我忘记将我的RoleInfoDaoImpl放在配置文件中,但它确实是exst,这是我的spring-dao.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: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
">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="roleInfoDao" class="keyy.dao.daoimpl.RoleInfoDaoImpl"></bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<bean class="keyy.dao.datasourcefactory.datasourcefactoryimpl.DataSourceFactory">
</bean>
</beans>
我在我的RoleInfoDalImpl类中剪切了dataSourceFactory实例以简化这里的问题,两种情况下仍存在问题。
我的项目的层次结构:(我的声誉限制图像数量)
-|springmvc
-|resources
-|spring
* spring-dao.xml
* spring-service.xml
* springmvc.xml
* jdbc.properties
-|src
-|keyy
-|controller
* RoleController
* HomeController
-|dao
* IRoleInfoDao
-|daoimpl
* RoleInfoDaoImpl
-|entity
* RoleInfo
-|service
* IRoleInfoService
-|serviceimpl
* RoleInfoServceImpl
RoleInfoServiceImpl has value RoleInfoDaoImpl is null
好吧我仍然无法识别导致我的问题的原因,我尝试使用JavaConfig重建我的SpringDemo并且它工作正常-----我没有像使用xml那样使用@ComponentScan或@Service @Repository。谁能解释一下?Pleasae
答案 0 :(得分:1)
<强>服务强>
@Service
public class RoleInfoServiceImpl implements IRoleInfoService {
<强>道强>
@Repository
public class RoleInfoDaoImpl implements IRoleInfoDao {
注意:如果您依赖注释,最好只使用注释。现在您的服务正在使用xml config。
答案 1 :(得分:0)
将#printOnly
{
display: inline;
}
#hiderow
{
.no-print, .no-print*
{
display: none ! important;
}
添加到keyy.service
或将基础包更改为<context:component-scan base-package="keyy.controller"/>
,因为它会递归。这样做将允许Spring获取带注释的类。
答案 2 :(得分:0)
我认为您应该在 spring-service.xml
中添加<context:annotation-config>
请参阅此链接以了解whats-the-difference-between-mvcannotation-driven-and-contextannotation