在实现了controller之后,现在需要想的是怎么把在容器中加载的Service注入到controller中,这样就可以实现一系列的操作了。
首先要在在spring-mybatis.xml配置Service实现类的实体bean(注:这里的bean要用service的实现类)
再这么写就可以找到了(注:这里用的是service接口)
如果你这样配置完成后仍然不好使,那很可能就是你的MVC配置文件出了问题,注意,我之前加载controller的时候用的是扫描器扫进去的,这样他就会连同注释一起识别。但是如果你的controller用的是bean标签加载的话,那么这样配置是不好用的,但是还是有解决方法,只不过很麻烦,就不推荐了。
在MVC配置文件还可以进行一个优化,那就是在我们加载适配器和映射器的时候用的是注释方法加载,其实完全可以用一句话代替,这句话不仅包含适配器和映射器的加载,还包含一些其他东西。
<mvc:annotation-driven />
最后发一下代码:
spring-mybatis.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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/myblogdb" />
<property name="user" value="root" />
<property name="password" value="a635226968" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:MyBatisConfig.xml"></property>
<property name="mapperLocations">
<array value-type="java.lang.String">
<value>classpath:com/web/blog/map/*.xml</value>
</array>
</property>
</bean>
<!-- <bean id="sqlSession" factory-bean="sqlSessionFactory" factory-method="openSession"></bean> -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com/web/blog/map"></property>
<property name="SqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--Service-->
<bean id="messageService" class="com.web.blog.service.realize.MessageServiceReal"></bean>
</beans>
applicationContext-mvc.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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 将所有的controller都扫描进去 前提是用的注释配置-->
<context:component-scan base-package="com.web.blog.controller"></context:component-scan>
<mvc:annotation-driven />
<!-- <bean class="com.web.blog.controller.TestController"></bean> -->
<!-- 映射器
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> -->
<!-- 适配器
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />-->
<!-- 视图解析器 jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
<!-- 拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.web.student.w.TestHandlerInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>-->
</beans>
MyBatisConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- lazyloading
<settings>
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="true" />
<setting name="cacheEnabled" value="false" />
</settings>
-->
<!-- 缩写 -->
<typeAliases>
<package name="com/web/blog/po" />
</typeAliases>
</configuration>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>dreamblog</display-name>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc_servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc_servlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>