I'm using springmvc 4.3.2-RELEASE to build a simple structure. I wrote my configurations into a couple of spring-*.xml ,when I debug my application ,it shows the serviceimpl in my Controller has value while the daoimpl in serviceimpl doesnt.
RoleController
@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();
}
}
RoleInfoServiceImpl
public class RoleInfoServiceImpl implements IRoleInfoService {
@Autowired
RoleInfoDaoImpl roleInfoDao ;
@Override
public List<RoleInfo> getAll() throws SQLException {
return roleInfoDao.getAll();
}
}
RoleInfoDaoImpl
public class RoleInfoDaoImpl implements IRoleInfoDao {
@Override
public List<RoleInfo> getAll() throws SQLException {
return null;
}
}
web.xml
<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>
springmvc.xml
<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>
spring-service.xml
<bean class="keyy.service.serviceimpl.RoleInfoServiceImpl"></bean>
spring-dao.xml
<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>
Sorry I forgot to put my RoleInfoDaoImpl in the configuration file, but it indeed exitst, here is the full copy of my 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>
I cut the dataSourceFactory instance in my RoleInfoDalImpl Class to simplify the question here, the problems still there in both case.
The hierarchy of my project:(my reputation limits the number of images)
-|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
Well I still cannot recognize what causes my question, I tried rebuild my SpringDemo using JavaConfig and it works fine ----- which I didn't use @ComponentScan or @Service @Repository like using xml. Can anyone explain?Pleasae