知道如何实现将maven项目修改成弹簧启动项目

  介绍

这篇文章将为大家详细讲解有关的想法如何实现将maven项目修改成弹簧启动项目,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。


在pom。xml文件中,要首先添加父母父级依赖

& lt; !——这个父母是springboot的父级依赖,   它提供相关的起动器的maven管理以及版本号管理,还有相关maven插件的公共配置——比;   & lt; parent>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-parent   & lt; version> 2.2.4.RELEASE   & lt; relativePath/比;& lt; !——从库中查找父——比;   & lt;/parent>


在依赖关系中,添加spring-boot-starter核心依赖,并添加核心测试依赖

& lt; dependencies>   & lt; !——这是springboot的核心起动器,它将完成起步依赖,自动配置,日志,YAML配置等功能,比;   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter   & lt;/dependency>   & lt; !——测试依赖——比;   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-test   & lt; scope> test   & lt; exclusions>   & lt; exclusion>   & lt; groupId> org.junit.vintage   & lt; artifactId> junit-vintage-engine   & lt;/exclusion>   & lt;/exclusions>   & lt;/dependency>   & lt;/dependencies>


属性属性配置主要是存放依赖的版本号,可以自定义,相对于定义了一个变量

& lt; properties>   & lt; !——指定jdk版本——比;   & lt; java.version> 1.8 & lt;/java.version>   & lt; !——德鲁伊连接池版本——比;   & lt; druid.version> 1.1.17   & lt;/properties>      & lt; dependencies>   & lt; !——阿里巴巴开发的德鲁伊连接池——比;   & lt; dependency>   & lt; groupId> com.alibaba   & lt; artifactId> druid-spring-boot-starter   & lt; !——对中应属性的& lt; druid.version>——比;   & lt; version> $ {druid.version} & lt;/version>   & lt;/dependency>   & lt;/dependencies>

& lt; !——春天引导打包插件,主要将弹簧引导应用打包成jar文件或者战争文件——比;   & lt; build>   & lt; plugins>   & lt; plugin>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-maven-plugin   & lt;/plugin>   & lt;/plugins>   & lt;/build>


春季启动项年代目一般都有一个应用程序。java的入口类,里面有一个主要的方法,这是标准java应用程序的入口方法。

@SpringBootApplication   公共类TestApplication () {   公共静态void main (String [] args) {   SpringApplication.run (TestApplication.class, args);   }   }

在主方法中执行的Spring应用程序的运行方法,返回一个上下文的容器实例

公共静态void main (String [] args) {//SpringApplication的运行方法返回一个上下文的容器实例
  ApplicationContext上下文=SpringApplication.run (TestApplication.class, args);//从容器获取bean对象
  UserService服务=context.getBean (UserServiceImpl.class);
  service.say ();
  }

@SpringBootApplication注解是弹簧引导的核心注解,它是一个组合注解,主要包含以下注解:

1, @SpringBootConfiguration:这是春天引导项目的配置注解,这也是一个组合注解

@Target ({ElementType.TYPE})   @Retention (RetentionPolicy.RUNTIME)   @Documented   @ configuration   公共@ interface SpringBootConfiguration {   @AliasFor (   注释=Configuration.class   )   布尔proxyBeanMethods()默认正确;   }

2, @EnableAutoConfiguration:启用自动配置,该注解会使弹簧引导根据项目中依赖的jar包自动配置项目的配置项

3, @ComponentScan:默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。

如果弹簧启动项目中整合了SpringMVC,那么就需要添加一个注解@MapperScan,这个注解的作用是告诉容器dao包中的接口的路径,使得可以在运行时动态代理生成实现类并放入到容器中管理。

@SpringBootApplication   @MapperScan ({“edu.nf.ch01.user.dao",“edu.nf.ch01.product.dao"})   公共类TestApplication () {   公共静态void main (String [] args) {   SpringApplication.run (TestApplication.class, args);   }   }

知道如何实现将maven项目修改成弹簧启动项目