spring项目的常用示范

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 项目一些类的常用示范

项目pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>logindemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>logindemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.9</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>layui</artifactId>
            <version>2.7.6</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.21</version>
        </dependency>

        <!--        <dependency>-->
        <!--            <groupId>com.mysql</groupId>-->
        <!--            <artifactId>mysql-connector-j</artifactId>-->
        <!--            <scope>runtime</scope>-->
        <!--        </dependency>-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.github.taoyua</groupId>
            <artifactId>logindemo</artifactId>
            <version>0.0.1-RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
        </plugins>
    </build>

</project>

项目config配置:

SecrityConfig

import com.example.logindemo.service.LoginDetailsService;
import com.example.logindemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    MySecritySuccessHandler mySecritySuccessHandler;

    @Autowired
    MySecrityFailureHandler mySecrityFailureHandler;

    @Autowired
    LoginDetailsService userDetailsService;

    @Autowired
    MySecrityLogoutHandler mySecrityLogoutHandler;

    @Autowired
    UserService userService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .headers().frameOptions().disable()
                .and().formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/form")
                .successHandler(mySecritySuccessHandler)
                .failureHandler(mySecrityFailureHandler)
                .and().authorizeRequests().antMatchers( "**/static/**","**/templates/**","/login")
                .permitAll()
                .anyRequest().authenticated();

        /**
         * 退出处理,logoutSuccessurl退出成功跳转登录
         */
        http.logout().logoutUrl("/signout").deleteCookies("JSESSIONID")//清除cookies
                .logoutSuccessHandler(mySecrityLogoutHandler);
//                .logoutSuccessUrl("/login").invalidateHttpSession(true);

    }

    /**
     * 忽略静态资源访问控制
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        //解决静态资源被拦截的问题
        web.ignoring().antMatchers("/css/**","/api/**","/images/**","/page/**",
                "/lib/**","/layui/**","/js/**","/*.js","/*.css","/*.png");
    }


    /**
     * 登录逻辑实现,获取数据库账号密码
     */

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(new BCryptPasswordEncoder());
    }

    /**
     * 内存处理用户名及密码
     */
//    @Autowired
//    public void configureGlobal(AuthenticationManagerBuilder auth)throws Exception{
//        auth.inMemoryAuthentication()
//                .passwordEncoder(new BCryptPasswordEncoder())
//                .withUser("admin")
//                .password(new BCryptPasswordEncoder().encode("123456"))
//                .roles("USER");
//    }

//   密码加密
    //    public static void main(String[] args) {
//        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
//        boolean matches = bCryptPasswordEncoder.matches( "123456","$2a$10$.y6yRGGU6uXyrq0Ftmdb9.WSZTZPZJmnoisz0IxaiRqvjPKUcywjq");
//        System.out.println(matches);
//        System.out.println(new BCryptPasswordEncoder().encode("123456"));

}

MySecritySuccessHandler:

import com.alibaba.fastjson2.JSON;
import com.example.logindemo.entity.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MySecritySuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        httpServletResponse.setContentType("application/json;charset=utf-8");
        Result result = new Result();
        result.setRespcode("0000");
        result.setRespmsg("登录成功");
        String res = JSON.toJSONString(result);
        httpServletResponse.getWriter().print(res);
    }
}

MySecrityFailureHandler:

import com.alibaba.fastjson2.JSON;
import com.example.logindemo.entity.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MySecrityFailureHandler implements AuthenticationFailureHandler {


    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {

        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        httpServletResponse.setContentType("application/json;charset=utf-8");
        Result result = new Result();
        result.setRespcode("1111");
        result.setRespmsg("登录失败!");
        String res = JSON.toJSONString(result);
        httpServletResponse.getWriter().print(res);
    }
}

MySecrityLogoutHandler:

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MySecrityLogoutHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login");//重定向到login

    }
}

控制类controller如下:

LoginController:

import com.example.logindemo.service.LoginDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
    @Autowired
    LoginDetailsService loginDetailsService;


    @RequestMapping("/login")
    public String login1(){
        return "login";

    }

//    @PostMapping("/login")
//    public Result login1(String username, String password, HttpServletRequest request){
//        return loginDetailsService.loadUserByUsername(username);
//
//    }

    @RequestMapping("/signout")
    public String signout(){
        return "login";

    }

    @RequestMapping("/index")
    public String index(){
        return "index";

    }

    @RequestMapping("/main")
    public String main(){
        return "main";

    }

}

UserController:

import com.alibaba.fastjson2.JSONObject;
import com.example.logindemo.entity.User;
import com.example.logindemo.service.MenuService;
import com.example.logindemo.service.RoleService;
import com.example.logindemo.service.UserService;
import com.example.logindemo.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    RoleService roleService;

    @Autowired
    UserService userService;

    @Autowired
    MenuService menuservice;

    @GetMapping("/menus")
    @ResponseBody
    public Map<String, Object> menus() {
        String loginUsername = CommonUtils.getLoginUser();
        User loginUser = userService.findUserByusername(loginUsername);
        JSONObject initjson=new JSONObject(menuservice.menu(loginUser.getId()));

        return initjson;
    }

}

实体类entity:

Menu:

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.util.List;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Menu {
    private Long id;

    private Long pid;

    private String title;

    private String icon;

    private String href;

//    private String target;

    private List<Menu> children;
}

Role:

import lombok.Data;

@Data
public class Role {
    private long id;
    private String name;

    //角色编码
    private String sn;

    //角色描述
    private String desc;

}

User:

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

@Data
public class User {
    private Long id;
    private String username;
    private String password;
    private String tel;
    private String sex;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private String createTime;


}

Result类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private String respcode;
    private String respmsg;

}

mapper类如下:

MenuMapper:

import com.example.logindemo.entity.Menu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface MenuMapper {

    @Select("SELECT sr.id,sr.pid pid,sr.title,sr.icon,sr.href\n" +
            "FROM treemenu sr where STATUS = 1  ORDER BY  sort")
    List<Menu> ListPermissionByuserid(long userid);


    @Select("SELECT sr.id,sr.pid pid,sr.title,sr.icon,sr.href,sr.target FROM treemenu sr")
    List<Menu> listMenuByRoleid(long id);

//    @Select("SELECT sr.id,sr.pid pid,sr.title,sr.icon,sr.href,sr.target\n" +
//            "FROM sys_resources sr\n" +
//            "join role_treemenu st on st.menu_id=sr.id\n" +
//            "and st.role_id=#{id}")
//    List<MenuVo> listMenuByRoleid(long id);
}

RoleMapper:

import com.example.logindemo.entity.Role;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface RoleMapper {

    @Select("SELECT r.*\n" +
            "from role r\n" +
            "JOIN t_user_role u on r.id=u.roleid\n" +
            "where u.userid=#{userid}")
    List<Role> listRoleByuserid(long userid);//根据userID,查询该用户的角色
}

UserMapper:

import com.example.logindemo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    @Select("select *from admin where username=#{username}")
    User findUserByusername(String username);
}

service中的Impl实现类:

LoginDetailsService

import com.example.logindemo.entity.Role;
import com.example.logindemo.entity.User;
import com.example.logindemo.service.RoleService;
import com.example.logindemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class LoginDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//获取数据库用户
        User user= userService.findUserByusername(username);
        if(user==null){
            throw new UsernameNotFoundException("用户不存在");
        }else{
//            log.info("查到用户");
            Set authorities=new HashSet<>();
//            List<Role> roles = roleService.listRoleByuserid(user.getId());
//            for(Role role:roles) {
//                authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName()));
//            }
            authorities.add(new SimpleGrantedAuthority("Role_管理员"));
            return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),
                    true,true,true,true,authorities);
        }

    }

}

MenuService:

import com.example.logindemo.entity.Menu;
import com.example.logindemo.entity.Role;
import com.example.logindemo.mapper.MenuMapper;
import com.example.logindemo.utils.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;


@Service
public class MenuService {
    @Resource
    MenuMapper menuMapper;

    @Resource
    RoleService roleService;

    public Map<String, Object> menu(Long userid) {
        /**
         * 根据用户id获取用户所有角色
         */
//
//        List<Role> roleidList = roleService.listRoleByuserid(userid);
//        /**
//         * 遍历所有角色,获取list菜单
//         */
//        Set<Menu> menuSet=new HashSet<>();
//        for(Role role:roleidList){
//            List<Menu> sysMenuList = menuMapper.listMenuByRoleid(role.getId());
//            for(Menu menu :sysMenuList){
//                menuSet.add(menu);
//            }
//
//        }
//        List<Menu> sysmenuList=new ArrayList<>(menuSet);
        //将菜单转为权限树
        List<Menu> menuInfo=menuMapper.ListPermissionByuserid(userid);
        List<Menu> menuTree = new ArrayList<>();
        menuTree= TreeUtil.toTree(menuInfo, 0L);
        Map<String, Object> map = new HashMap<>();
        map.put("data", menuTree);
        return map;
    }

}

RoleService:

import com.example.logindemo.entity.Role;
import com.example.logindemo.mapper.RoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleService {
    @Resource
    private RoleMapper roleMapper;

    public List<Role> listRoleByuserid(long userid) {
        List<Role> roles = roleMapper.listRoleByuserid(userid);
        return roles;
    }
}

UserService:

import com.example.logindemo.entity.User;
import com.example.logindemo.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {

    @Resource
    UserMapper usermapping;

    public User findUserByusername(String username) {

        return usermapping.findUserByusername(username);
    }

}

utils工具类:

CommonUtils:

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class CommonUtils {
    public static String getLoginUser(){
        //获取登录认证对象
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //得到认证的主体(登录用户)

        if(authentication!=null) {
            return authentication.getName();
        }
        return null;
    }
}

TreeUtil:

import com.example.logindemo.entity.Menu;
import java.util.ArrayList;
import java.util.List;

public class TreeUtil {
    public static List<Menu> toTree(List<Menu> treeList, Long pid) {
        List<Menu> retList = new ArrayList<Menu>();
        for (Menu parent : treeList) {
            if (pid.equals(parent.getPid())) {
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }
    private static Menu findChildren(Menu parent, List<Menu> treeList) {
        for (Menu child : treeList) {
            if (parent.getId().equals(child.getPid())) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(findChildren(child, treeList));
            }
        }
        return parent;
    }

}

application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#云上
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://rm-bp1l57prt0jm1g81x.mysql.rds.aliyuncs.com:3306/manage_system?characterEncoding=utf8
spring.datasource.username=yt
spring.datasource.password=Yt@123456

spring.web.resources.static-locations=classpath:/static/,/templates/

spring.thymeleaf.cache=false

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
WXM
|
24天前
|
Java 应用服务中间件 Maven
|
21天前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
4天前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
14 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
5天前
|
前端开发 JavaScript Java
spring boot+vue前后端项目的分离(我的第一个前后端分离项目)
该博客文章介绍了作者构建的第一个前后端分离项目,使用Spring Boot和Vue技术栈,详细说明了前端Vue项目的搭建、后端Spring Boot项目的构建过程,包括依赖配置、数据库连接、服务层、数据访问层以及解决跨域问题的配置,并展示了项目的测试结果。
spring boot+vue前后端项目的分离(我的第一个前后端分离项目)
|
14天前
|
IDE Java Shell
如何快速搭建一个 Spring Boot 项目?
本指南介绍如何通过Spring Initializr创建一个基本的Spring Boot Web项目。首先访问`start.spring.io`,选择Maven项目、Java语言、Spring Boot版本3.1.0、Java 17,并勾选Spring Web依赖。点击“Generate”下载项目模板。解压后,IDE打开项目并修改`DemoApplication.java`,添加REST控制器以实现一个简单的“Hello World!”服务。通过`@RestController`和`@GetMapping`注解定义Web端点,使用`@RequestParam`获取URL参数。
|
18天前
|
IDE Java Shell
如何快速搭建一个 Spring Boot 项目?
Spring Boot 可以用最少的配置来快速创建一个独立的、生产级的 Spring 应用程序。 本文介绍如何快速搭建一个 Spring Boot「Hello World!」项目。
|
23天前
|
Java Spring
idea新建spring boot 项目右键无package及java类的选项
idea新建spring boot 项目右键无package及java类的选项
33 5
|
23天前
|
Java 数据库连接 Spring
搭建 spring boot + mybatis plus 项目框架并进行调试
搭建 spring boot + mybatis plus 项目框架并进行调试
49 4
|
4天前
|
前端开发 Java 测试技术
单元测试问题之在Spring MVC项目中添加JUnit的Maven依赖,如何操作
单元测试问题之在Spring MVC项目中添加JUnit的Maven依赖,如何操作
|
14天前
|
设计模式 算法 Java
Spring Boot 项目怎么使用策略模式?
策略模式是一种设计模式,它允许在运行时选择不同的算法或行为。此模式通过定义一系列算法并将它们封装在独立的类中实现,这些类可以互相替换。这样可以根据不同情况动态选择最适合的算法。 在Spring框架中,可以通过依赖注入来实现策略模式。首先定义一个抽象策略类(接口或抽象类),然后创建具体策略类实现不同的算法。具体策略类通过`@Service`注解并在名称中指定特定的策略(如加法、减法等)。在上下文类(如Service类)中,通过`@Resource`注入策略对象的Map集合,根据需要选择并执行相应的策略。