maven版本控制工具:管理JAR
一、系统要求
- JDK:maven3.3以上需要jdk1.7或以上
- Eclipse
- Maven程序包
二、Maven 下载与安装
- Maven 下载地址:http://maven.apache.org/download.cgi
- 将文件解压到D:\Maven\apache-maven-3.6.3目录下
- 新建环境变量MAVEN_HOME,赋值D:\Maven\apache-maven-3.6.3
- 编辑环境变量Path,加上%MAVEN_HOME%\bin;
- 通过win+R打开cmd,输入mvn -v查看是否安装成功
三、配置Maven本地仓库
- 并在该目录下新建maven-repository,作maven的本地库。
- 打开D:\Maven\apache-maven-3.6.3\conf\settings.xml文件,,修改默认仓库的路径:
<localRepository>/path/to/local/repo</localRepository>
改为
<localRepository>D:\Maven\apache-maven-3.6.3\maven-repository</localRepository>
- localRepository节点用于配置本地仓库,用以本地缓存,maven项目中运行mvn install,项目将会自动打包到本地仓库中。
- 运行mvn help:system命令
***mvn help:system作用:***首次执行 mvn help:system 命令,Maven相关工具自动到Maven中央仓库下载缺省的或者更新的各种配置文件和类库(jar包)到Maven本地仓库中。
运行成功后,D:\Maven\apache-maven-3.6.3\maven-repository会有一些文件,这些是 mvn help:system 命令成功后打印出的所有Java系统属性和环境变量信息。
四、在Eclipse配置Maven环境
- Eclipse,打开Window->Preferences->Maven->Installations,右侧点击Add,设置maven的安装目录,然后Finish。
- 打开Window->Preferences->Maven->User Settings,配置如下完成后Apply:
五、新建maven工程
- 新建Maven工程
- maven工程下包的作用
- 编辑pom.xml增加依赖的jar包
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.MavenHelloWorld</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 引入父级依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<!-- 加入web依赖 应用spring springmvc -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--加入jdk1.8插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.新建类controller
@EnableAutoConfiguration //启用自动配置
@Controller//配置controller注解
@ConfigurationProperties(prefix="book")//
public class UserController {
// @Value("${book.name}")
private String name;
// @Value("${book.author}")
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@RequestMapping("hello")
@ResponseBody//配置界面响应
public String helloTest() {
System.out.println(name+""+author);
return "hello";
}
public static void main(String[] args) {
SpringApplication.run(UserController.class, args);//springboot运行入口
}
}
六、springboot基础知识
- 配置文件,用来改变tomcat的默认端口号
- 支持两种格式
- 第一种: .yml(key:value)
- 第二种:.properties(key=value)
- 获取properties的value方法
第一种方法
//不需要生成getter setter方法
@Value("${book.name}")
private String name;
@Value("${book.author}")
private String author;
第二种方法
@ConfigurationProperties(prefix="book")//封装类,前提是properties前缀名称是一致的 ,需要生成getter setter方法
private String name;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
- 类型安全的配置
- 当自定义属性很多时,value 注解就很麻烦
1):在定义属性时,注意用相同的前缀
2):使用@ConfigurationProperties(prefix=“book”) prefix 填入定义属性时的前缀
3):使用@VALUE时不需要生成setter和getter,但类型安全匹配则需要生成 start poom
为工程提供自动配置的Bean,做相关的开发时,只要加入相关的stater poom即可
spring-boot-starter Spring Boot 核心的starter,包含自动配置、日志、yaml配置文件的支持等spring-boot-starter-actuator 准生产特性,用来监控和管理应用
spring-boot-starter-amqp 对”高级消息队列协议”的支持,通过spring-rabbit实现
spring-boot-starter-aop 对面向切面编程的支持,包括spring-aop和AspectJ
spring-boot-starter-batch 对Spring Batch的支持,包括HSQLDB数据库
spring-boot-starter-cloud-connectors 对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接
spring-boot-starter-data-elasticsearch 对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire 对GemFire分布式数据存储的支持,包括spring-data-gemfire
spring-boot-starter-data-jpa 对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb 对MongoDB NOSQL数据库的支持,包括spring-data-mongodb
spring-boot-starter-data-rest 对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现
spring-boot-starter-data-solr 对Apache Solr搜索平台的支持,包括spring-data-solr
spring-boot-starter-freemarker 对FreeMarker模板引擎的支持
spring-boot-starter-groovy-templates 对Groovy模板引擎的支持
spring-boot-starter-hateoas 对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现
spring-boot-starter-hornetq 对”Java消息服务API”的支持,通过HornetQ实现
spring-boot-starter-integration 对普通spring-integration模块的支持
spring-boot-starter-jdbc 对JDBC数据库的支持
spring-boot-starter-jersey 对Jersey RESTful Web服务框架的支持
spring-boot-starter-jta-atomikos 对JTA分布式事务的支持,通过Atomikos实现
spring-boot-starter-jta-bitronix 对JTA分布式事务的支持,通过Bitronix实现
spring-boot-starter-mail 对javax.mail的支持
spring-boot-starter-mobile 对spring-mobile的支持
spring-boot-starter-mustache 对Mustache模板引擎的支持
spring-boot-starter-redis 对REDIS键值数据存储的支持,包括spring-redis
spring-boot-starter-security 对spring-security的支持
spring-boot-starter-social-facebook 对spring-social-facebook的支持
spring-boot-starter-social-linkedin 对spring-social-linkedin的支持
spring-boot-starter-social-twitter 对spring-social-twitter的支持
spring-boot-starter-test 对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块
spring-boot-starter-thymeleaf 对Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity 对Velocity模板引擎的支持
spring-boot-starter-web 对全栈web开发的支持,包括Tomcat和spring-webmvc
spring-boot-starter-websocket 对WebSocket开发的支持
spring-boot-starter-ws 对Spring Web服务的支持
spring-boot-starter-remote-shell 添加远程ssh shell支持
spring-boot-starter-jetty 导入Jetty HTTP引擎(作为Tomcat的替代)
spring-boot-starter-log4j 对Log4J日志系统的支持
spring-boot-starter-logging 导入Spring Boot的默认日志系统(Logback)
spring-boot-starter-tomcat 导入Spring Boot的默认HTTP引擎(Tomcat)
spring-boot-starter-undertow 导入Undertow HTTP引擎(作为Tomcat的替代)
- @EnableAutoConfiguration自动配置原理
默认情况下,执行spring.factories 找到相关的类路径,然后实例化,类中有的属性如没有且全局的application.properties中没有覆盖,就使用默认的属性值