Java - SpringMvc-Spring项目整合

    一、  SpringMVC与Spring整合

                                                                                                        (Spring与mybatis整合上文已介绍)

   1、整合思路

          整合过程,就是累加:代码+依赖+配置。然后将service注入给controller即可

    此时项目中有两个工厂 :

         DispatcherServlet 启动的springMVC工厂——负责生产C及springMVC自己的系统组件

         ContextLoaderListener 启动的spring工厂——负责生产其他所有组件

 *** SpringMVC的工厂会被设置为spring工厂的子工厂,可以随意获取spring工厂中的组件

 

 2、整合技巧

        两个工厂不能有彼此侵入,即,生产的组件不能有重合。

      1、在SpringMvc配置文件中加入:

<!-- 告知SpringMVC  哪些包中 存在 被注解的类
	use-default-filters=true 凡是被 @Controller @Service  @Repository注解的类,都会被扫描
	use-default-filters=false 默认不扫描包内的任何类, 只扫描include-filter中指定的类
	只扫描被@Controller注解的类
-->
<context:component-scan base-package="com.vince" use-default-filters="false">
 	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

    2、在Spring配置文件中加入:

 

<!-- 告知Spring
     唯独不扫描@Controller注解的类 -->
<context:component-scan base-package="com.vince" use-default-filters="true">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

 

web.xml文件中配置:

<servlet>
        <servlet-name>mvc01</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 局部参数:声明配置文件位置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc.xml</param-value>
        </init-param>

    </servlet>

    <servlet-mapping>
        <servlet-name>mvc01</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--解决post乱码问题-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>






<!-- 监听项目的启动,负责启动spring工厂,并把工厂对象存储于ServletContext作用域中
       细节:需要知道 spring的配置文件的位置。会从 一个名为"contextConfigLocation"的context-param中获取
     -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 定义了spring配置文件位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

项目演示:

UserDao:

public interface UserDao {

    User queryuserByUsername(@Param("username") String username);

}

UserDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空间,用于隔离sql -->
<!-- 还有一个很重要的作用,使用动态代理开发DAO,1. namespace必须和Mapper接口类路径一致 -->
<mapper namespace="com.vince.dao.UserDao">

  <select id="queryuserByUsername" parameterType="string" resultType="User">

      SELECT  * from  t_user where username=#{username}

  </select>


</mapper>

 

UserService:

public interface UserService {


    User findUserByUsername(@Param("username") String username);
}

实现类:UserServiceImpl:    在此将dao注入给Service

import com.vince.dao.UserDao;
import com.vince.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service

public class  UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User findUserByUsername(String username) {
        User user = userDao.queryuserByUsername(username);
        return user;
    }
}

 

controller:

@Controller
@RequestMapping("/user")
public class ShiroController {

    @RequestMapping("/login/page")
    public String loginpage(){
        System.out.println("goto login");
        return "login";
    }

通过路径: user/login/page   可直接访问 loginpage 方法。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值