SSM + Shiro 整合 (1)- 实现 Spring 的 HelloWorld

25 篇文章 0 订阅
23 篇文章 0 订阅

项目源码:https://github.com/weimingge14/Shiro-project
演示地址:http://liweiblog.duapp.com/Shiro-project/login

SSM + Shiro 整合 (1)- 实现 Spring 的 HelloWorld

本节从零开始,一步一步介绍如何搭建一个使用 Spring MVC 、Spring 、MyBatis 、Shiro 集成的一个 Web 项目。该项目主要演示了 Shiro 在 Web 应用中是如何使用的,同时也复习了我们使用 Spring MVC + Spring + MyBatis 如何快速搭建一个 Web 应用。

因为篇幅的原因,在这里就不过多地粘贴源代码。大家可以到我这个项目的 GitHub 仓库地址下载源代码,以及通过浏览 Shiro-project 这个项目的 commits 来观察环境是如何一步一步搭建起来的,以及是如何一点一点增加新的功能。

项目使用 Maven 构建工具。JDK 版本使用 Java8,使用 Jetty 插件

第 1 节 我们先引入 Spring4 和 JUnit 的相关依赖,编写 Spring 的 HelloWorld 。

步骤 1 :引入 Spring 的相关依赖

1、Spring 体系的相关依赖
(有些包其实我们这一节还用不上,先添加进来,但每个包,为什么添加这个包一定要明白。此处只列出 artifactId ,下同)
spring-core、spring-context、spring-beans、spring-jdbc、spring-tx、spring-test
其中 spring-jdbc 提供了数据访问的依赖,spring-tx 提供了事务的支持,spring-test 提供了在 Spring 环境中使用 JUnit 的支持;
2、JUnit 的依赖
junit
3、MyBatis 相关依赖
mybatis、mybatis-spring
4、MySQL 依赖
mysql-connector-java
5、数据库连接池
druid
6、日志框架
log4j、 slf4j-log4j12

引入了配置以后,还要注意:
1、Spring 系列依赖比较多,建议将 Spring 的版本声明通过引入变量的形式来定义,方便以后的修改和升级,例如

<properties>
    <spring.version>4.2.6.RELEASE</spring.version>
</properties>

在引入 spring-core 依赖的时候就可以使用变量 spring.version

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>

2、可以使用 Maven 的编译插件指定项目使用的 JDK 版本,这样 IDE 工具就不会使用默认的 1.5 的 JDK 了。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

步骤 2:实现 Spring 的 HelloWorld

1、在 resources 目录下新建 spring/spring-service.xml 文件作为 Spring 容器启动的配置文件。

<?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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.liwei.shiro.service"/>

</beans>

配置文件的意思是:在 com.liwei.shiro.service 这个包下扫描 @Service 、@Repository 、@Component 、@Controller 、@Configuration 注解,添加了这些注解的类成为被 Spring 管理的 Bean。

对于这个配置,我们还可以指定只扫描指定的例如 @Service 这样的注解。

其中,xml 文件 <beans> 节点中的片段应该到 Spring 的官方文章上去拷贝下来,或者使用 Spring 的开发工具 STS 来添加文档约束,不建议在网上随便找一篇文章粘贴进去。

2、编写一个普通的 bean

@Service
public class HelloWorld {

    public String hello(String str){
        return str;
    }
}

3、在 test 模块的 java 测试源代码目录下编写测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-service.xml"})
public class HelloWorldTest {

    @Autowired
    private HelloWorld helloWorld;

    @Test
    public void testHelloWorld(){
        String str = helloWorld.hello("world");
        /**
         * 第 1 个参数:测试不通过的时候显示的消息
         * 第 2 个参数:期望值
         * 第 3 个参数:实际值
         */
        Assert.assertEquals("测试不通过","world",str);
    }
}

说明:1、@RunWith(SpringJUnit4ClassRunner.class) 表明我们使用的 JUnit4 是在 Spring 的容器下运行的。
2、@ContextConfiguration(locations = {"classpath:spring/spring-service.xml"}) 表明了我们的 Spring IOC 容器是通过 locations 属性指定的 xml 文件启动的,locations 属性是一个数组,我们可以设置多个 xml 文件。
以后配置文件模块化以后,很可能是由多个 Spring 的配置文件来启动 Spring 容器的。
3、这里还要注意:指定 Spring 容器启动的配置文件的时候,要加上 classpath: 前缀。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值