zoukankan      html  css  js  c++  java
  • springboot pom文件依赖

    spring-boot-starter-parent

    通过spring initializr,我们可以快速构建一个springboot应用,如果你选择的是Maven来管理项目,在默认的pom文件中有这么一个部分:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>

    它表示当前pom文件从spring-boot-starter-parent继承下来,在spring-boot-starter-parent中提供了很多默认的配置,这些配置可以大大简化我们的开发。

    通过继承spring-boot-starter-parent,默认具备了如下功能:

    1.Java版本(Java8)
    2.源码的文件编码方式(UTF-83.依赖管理
    4.打包支持
    5.动态识别资源
    6.识别插件配置
    7.识别不同的配置,如:application-dev.properties 和 application-dev.yml

    继承spring-boot-starter-parent后,大大简化了我们的配置,它提供了丰富的常用的默认的依赖的版本定义,我们就不需要再次指定版本号:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    Maven只允许定义一个parent pom,这时的项目虽然没有继承自spring-boot-starter-parent但是也需要手动导入其他依赖。

    spring-boot-starter-web

    spring-boot-starter-web默认使用嵌套式的Tomcat作为Web容器对外提供HTTP服务,默认端口8080对外监听和提供服务。默认为我们提供一些SpringMVC必要的组件。

    spring-boot-starter-actuator

    在生产环境中,需要实时或定期监控服务的可用性。Spring Boot的actuator(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。

    启动项目后即可访问:

    http://localhost:8081/webproject/actuator/health
    http://localhost:8081/webproject/actuator/info

    spring-boot-starter-aop

     面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。

    利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

    /**
     * 日志切面
     */
    @Aspect
    @Component
    public class LogAspect {
        @Pointcut("execution(public * com.xncoding.aop.controller.*.*(..))")
        public void webLog(){}
    
        @Before("webLog()")
        public void deBefore(JoinPoint joinPoint) throws Throwable {
            // 接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            System.out.println("URL : " + request.getRequestURL().toString());
            System.out.println("HTTP_METHOD : " + request.getMethod());
            System.out.println("IP : " + request.getRemoteAddr());
            System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));
    
        }
    
        @AfterReturning(returning = "ret", pointcut = "webLog()")
        public void doAfterReturning(Object ret) throws Throwable {
            // 处理完请求,返回内容
            System.out.println("方法的返回值 : " + ret);
        }
    
        //后置异常通知
        @AfterThrowing("webLog()")
        public void throwss(JoinPoint jp){
            System.out.println("方法异常时执行.....");
        }
    
        //后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
        @After("webLog()")
        public void after(JoinPoint jp){
            System.out.println("方法最后执行.....");
        }
    
        //环绕通知,环绕增强,相当于MethodInterceptor
        @Around("webLog()")
        public Object arround(ProceedingJoinPoint pjp) {
            System.out.println("方法环绕start.....");
            try {
                Object o =  pjp.proceed();
                System.out.println("方法环绕proceed,结果是 :" + o);
                return o;
            } catch (Throwable e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    spring-boot-starter-test

    使用最新的springboot版本2.2.6, 使用的是junit5版本。

    <!--加入下面的引入,可以使用junit5的版本来测试; 如果想用junit4的测试, 把exclusioins去除-->

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
    //如果有多个启动类则需要指定classes属性,指明是哪个启动类。如果只有一个启动类可以不写classes属性。junit5
    @SpringBootTest(classes = SpringbootexceptionandjunitApplication.class)
    class SpringbootexceptionandjunitApplicationTests {
    
        @Autowired
        private UsersServiceImpl usersService;
    
        //方法名任意
        @Test
        void contextLoads() {
            //测试方法
            usersService.addUser();
        }
    
    }

    spring-boot-dependencies

    一般用来放在父项目中,来声明依赖,子项目引入相关依赖而不需要指定版本号,好处就是解决依赖冲突,统一管理依赖版本号

    利用pom的继承,一处声明,处处使用。在最顶级的spring-boot-dependencies中,使用dependencyManagement让所有子项目引用一个依赖而不用显式的列出版本号,将结构信息,部署信息,共同的依赖信息放置在统一的位置。dependencyManagement只声明依赖,并不真正引入,因此子项目需要通过dependencies引入相关依赖。

    例如:

    Maven 父子项目结构和 Java继承一样,都是单继承,一个子项目只能制定一个父 pom

    很多时候,我们需要打破这种 单继承, 例如使用 spring-boot 的时候, 官方推荐的方式是继承父pom是spring-boot-starter-parent

    <parent>
        <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
    </parent>

    但是如果项目中已经有了其他父pom, 又想用 spring-boot 怎么办呢?

          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>  <!-- import scope只能用在dependencyManagement里面,且仅用于type=pom的dependency。解决单继承问题-->  
          </dependency>

    spring-boot-configuration-processor

     springboot中获取配置文件的方式,通常大家最常用的是@Value("${mail.username}")的形式,也可以用spring-boot-configuration-processor来更优雅得读取配置文件。下面讲下具体用法。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>  <!-- 当optional为true是,这个依赖项不会传递到子模块 -->
    </dependency>

    定义好自己的配置文件:

    mail.username=wjh
    mail.password=wjh123

    新建配置文件读取类:

    @Data
    @Configuration
    @EnableConfigurationProperties(MailConfig.class)
    @ConfigurationProperties(prefix = MailConfig.PREFIX, ignoreInvalidFields = true)
    public class MailConfig {
    
        public static final String PREFIX = "mail"; // 这里对应配置文件中的mail前缀
    
        private String username;
    
        private String password;
    }

    先注入配置文件类:

    @Autowired
    private MailConfig mailConfig;

    取配置文件:

    String userName = mailConfig.getUsername();

     spring-boot-starter-security

    一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架

    pom依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    创建测试用Controller:

    @RestController
    public class TestController {
    
        @GetMapping("getData")
        public String getData() {
            return "date";
        }
    
    }

    测试:

    访问http://127.0.0.1:8080/getData,由于我们开启了SpringSecurity且当前是未登录状态,页面会被302重定向到http://127.0.0.1:8080/login,页面如下:

    为了解决复杂密码的问题,我们可以在application.yml中做如下设定:

    spring:
      security:
        user:
          name: user
          password: 123

    这样我们就可以通过用户名user密码123来访问http://127.0.0.1:8080/getData接口了。

    详细:https://www.imooc.com/article/287214

  • 相关阅读:
    Ubuntu 14.04上架IPSec+L2TP的方法
    Windows Server 2008 R2 FTP无法从外部访问的解决方法
    在Windows Server 2008 R2上打开ping的方法
    全站导航
    拉勾网招聘信息分析
    pandas之DataFrame
    pandas之Series
    matplolib学习
    numpy学习
    scrapy框架【爬虫的暂停和启动】
  • 原文地址:https://www.cnblogs.com/chong-zuo3322/p/13489553.html
Copyright © 2011-2022 走看看