在项目中,src/main/java目录下,用来存放java源代码,resources目录下的static用来存放css、js、图片等静态文件,templates用于存放html等模板文件。
项目中,核心文件只有3个,分别是HelloApplication.java、application.properties和pom.xml,下面分别介绍它们。
1. HelloApplication.java
SpringBoot程序启动入口一个是SpringApplication.run,一个是@SpringBootApplication注解。按下键盘ctrl键,鼠标单击@SpringBootApplication注解,进到SpringBootApplication源文件,可以看到如下代码:
package org.springframework.boot.autoconfigure;
import java.lang.annotation.Documented;
......
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "nameGenerator")
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
@AliasFor(annotation = Configuration.class)
boolean proxyBeanMethods() default true;
}
@SpringBootApplication组合注解,核心内容由下面三部分组成:
@SpringBootConfiguration:可以用java代码的形式实现spring中xml配置文件配置的效果,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。
@EnableAutoConfiguration:可以根据classpath中的jar依赖,自动注册bean,一般用于类或接口上,它尝试根据您添加的jar依赖项自动配置Spring应用程序;主要从配置文件META-INF/spring.factories加载所有可能用到的自动配置类;
@ComponentScan:根据定义的扫描路径,把符合扫描规则的类装配到spring的bean容器中。扫描路径下带有@Controller,@Service,@Repository,@Component注解加入spring容器,通过excludeFilters过滤出不用加入spring容器的类。
用@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan替换@SpringBootApplication,也是可以正常启动Springboot框架的,只是代码多了一些。
package com.home.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
2. application.properties
Springboot框架用“约定优于配置”的方式,节省了很大代码,但同时保留自定义要求的入口,这个入口就是application.properties文件,在这里,你可以配置一些“个性化”需求。比如你跟我一样,嫌弃每次访问网址,总是要输入:8080端口,那你可以按如下方式进行配置:
server.port=80
重新运行Springboot,这时候访问用:http://127.0.0.1/ ,就可以了。如果你喜欢用IP访问,那就用:http://localhost进行访问。
数据库连接参数,开发中经常要用到,配置参数如下:
# 数据库访问配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/niu
spring.datasource.username=root
spring.datasource.password=root
更多配置信息,等用到的时候,再进行解析。这个配置文件,也可以用application.yml的方式,yml最大的好处在于其拥有天然的树状结构,配置形式如下:
server:
port: 8801
properties用.分割,yml用:分割,两种文件格式都有人在用,你可以跟我一样,先用properties文件,到需要用yml时,用工具进行转换就可以了。
3. pom.xml
POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示。它用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。
<?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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.home</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello</name>
<description>hello</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
一个最简单的pom.xml的定义必须包含modelVersion、groupId、artifactId和version这四个元素,当然这其中的元素也是可以从它的父项目中继承的。
modelVersion:指定了当前POM模型的版本,对于Maven 2或3来说,它只能是4.0.0;
groupId:组织标识,定义了项目属于哪个组,如:com.home
artifactId:项目名称也可以说你所模块名称,如:com.home.dao
version:项目当前的版本号。
使用groupId、artifactId和version组成groupdId:artifactId:version的形式,可以用于确定一个项目的唯一性。
至于文件中的spring-boot-starter-xxx,在第1节的时候就说过了,这里就不再重复赘述了。不了解的,回去第1节,悄悄瞄一眼。
好了,这节的内容就说到这里,一个干了10多年技术总监的老家伙,正在今日头条:老陈说编程,分享Java和App的干货,关注他,你就赚大发了。