springmvc service自动注入 dao自动注入 null
前提新建一个项目,其结构dao、service、controller,controller自动注入service,service自动注入dao,但是dao我为了测试,没有使用mybatis,当时的想法将service和dao都交给spring管理
代码如下:
一、Dao
①、applicationContext-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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="itemsMapperCustomDao" class="com.xxx.springmvc.dao.impl.ItemsMapperCustomDaoImpl" /> </beans>
②、Java代码
public class ItemsMapperCustomDaoImpl implements ItemsMapperCustomDao { @Override public List<ItemsCustom> findItemsList() throws Exception { List<ItemsCustom> itemsList=new ArrayList<ItemsCustom>(); ItemsCustom items_1 = new ItemsCustom(); items_1.setName("笔记本"); items_1.setPrice(6000f); items_1.setDetail("笔记本电脑!"); ItemsCustom items_2 = new ItemsCustom(); items_2.setName("手机"); items_2.setPrice(5000f); items_2.setDetail("手机!"); itemsList.add(items_1); itemsList.add(items_2); return itemsList; } }
二、Service
①、applicationContext-service.xml
<bean id="itemsMapperCustomService" class="com.xxx.springmvc.service.impl.ItemsMapperCustomServiceImpl"> </bean>
②、Java代码(通过自动注入[@Autowired]获取Dao对象,调用其对应方法)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class ItemsMapperCustomServiceImpl implements ItemsMapperCustomService { @Autowired private ItemsMapperCustomDao itemsMapperCustomDao; // 商品查询列表 public List<ItemsCustom> findItemsList() throws Exception { return itemsMapperCustomDao.findItemsList(); } }
三、Controller
①、springmvc.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<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" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 --> <context:component-scan base-package="com.xxx.springmvc.controller"/> <!-- 使用 mvc:annotation-driven代替注解映射器和注解适配器配置 mvc:annotation-driven默认加载很多的参数绑定方法, 比如json转换解析器就默认加载了, --> <mvc:annotation-driven/> <!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
②、Java代码(通过自动注入[@Autowired]获取Service对象,调用其对应方法)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Controller public class ItemsController { @Autowired private ItemsMapperCustomService itemsMapperCustomService; // 商品查询 @RequestMapping("/queryItems") public ModelAndView queryItems(HttpServletRequest request) throws Exception { // 调用service查找 数据库,查询商品列表 List<ItemsCustom> itemsList = itemsMapperCustomService.findItemsList(); // 返回ModelAndView ModelAndView modelAndView = new ModelAndView(); // 相当 于request的setAttribut,在jsp页面中通过itemsList取数据 modelAndView.addObject("itemsList", itemsList); // 指定视图 // 下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为 // modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); // 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀 modelAndView.setViewName("items/itemsList"); return modelAndView; } }
四、开启spring(配置监听器)和springmvc(配置前端控制器),配置web.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>03springmvc</display-name> <!-- 1、加载spring容器 1.1、整合applicationContext 1.2、配置监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext-*.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 2、springmvc前端控制器,rest配置 2.1、配置DispatcherServlet前端控制器 2.1、配置servlet映射(访问方式) --> <servlet> <servlet-name>springmvc_rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc_rest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- post乱码过虑器 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
五、问题来了
debug发现service层在自动注入dao层的对象为null,什么原因不知道
解决方法:
1、修改applicationContext-dao.xml
<context:component-scan base-package="com.xxx.springmvc.dao.impl"/>
2、在dao层类上添加一个@Repository注解
@Repository public class ItemsMapperCustomDaoImpl implements ItemsMapperCustomDao {
@Controller
用来表示一个web控制层bean,如SpringMvc中的控制器。
@Service
用来表示一个业务层bean。
@Repository
用来表示一个持久层bean,即数据访问层DAO组件。
@Component
用来表示一个平常的普通组件,当一个类不合适用以上的注解定义时用这个组件修饰。