Java学习(第一阶段模块四)2020/5/17-2020/5/24

任务一:SpringBoot基础回顾

顺利完成模块三的学习,正式进入模块四。

1. 约定优于配置

约定优于配置(Conversion over configuration),又称按约定编程,是一种软件设计范式。
约定优于配置简单来理解,就是遵循约定。

2. SpringBoot解决Spring问题

  1. 起步依赖:将具备某种功能的坐标打包到一起,并提供一些默认的功能。
  2. 自动配置:会自动将一些配置类的bean注册进ioc容器中,可以使用@autowired以及@resource等注解来使用。
  3. springboot:简单,快速,方便的搭建项目。对主流的开发框架无配置集成,极大的提高了开发,部署效率。

3. 单元测试与热部署

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
@RunWith(SpringRunner.class) // 测试启动器,并加载SpringBoot测试注解
@SpringBootTest //标记为SpringBoot单元测试类,并加载项目的ApplicationContext上下文环境
class SpringbootDemoApplicationTests {
@Autowired
private DemoController demoController;
// 自动创建的单元测试方法实例
@Test
void contextLoads() {
String demo = demoController.demo();
System.out.println(demo);
}
}
<!-- 引入热部署依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

在这里插入图片描述
在这里插入图片描述
Ctrl+Shift+Alt+/打开maintenance,选中并打开Registry页面。

任务二:SpringBoot原理深入及源码剖析

1. 依赖管理

项目pom.xml文件有两个核心依赖,分别是spring-boot-starter-parent和spring-boot-starter-web。

<!-- Spring Boot父项目依赖管理 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

2. 自动配置

@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
@Target({ElementType.TYPE}) //注解的适用范围,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 {
...
}

@SpringBootConfiguration注解表示SpringBoot配置类。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration //配置IOC容器
public @interface SpringBootConfiguration {
}

@EnableAutoConfiguration注解表示开启自动配置功能,该注解是SpringBoot框架最重要的注解,也是实现自动化配置的注解。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage//自动配置包
@Import({AutoConfigurationImportSelector.class})//自动配置类扫描导入
public @interface EnableAutoConfiguration{
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() defalult{};
String[] excludeName() default {};
}

@AutoConfigurationPackage

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class}) // 导入Registrar中注册的组件
public @interface AutoConfigurationPackage {
}

@Import({AutoConfigurationImportSelector.class})
查找classpath上所有jar包中的META-INF/spring.factories进行加载,实现将配置类信息交给SpringFactory加载器进行一系列的容器创建过程。
@ComponentScan注解具体扫描的包的根路径由SpringBoot项目主程序自动启动类所在的包位置决定,在扫描过程中由前面介绍的@AutoConfigurationPackage注解进行解析,从而得到SpringBoot项目主程序启动类所在包的具体位置。

任务三:Thymeleaf

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以编写一个自定义的 Maven 插件来实现这个需求。以下是一个示例插件的代码: 首先,在您的项目中创建一个 Maven 插件模块。在该模块中,创建一个 Mojo 类来定义插件的行为。下面是一个示例的 Mojo 类代码: ```java import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; import org.codehaus.plexus.archiver.Archiver; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.zip.ZipArchiver; import org.codehaus.plexus.archiver.zip.ZipEntry; import java.io.File; import java.io.IOException; @Mojo( name = "create-zip", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE ) public class CreateZipMojo extends AbstractMojo { @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true) private File outputDirectory; @Parameter(defaultValue = "${project.build.finalName}", required = true, readonly = true) private String finalName; @Parameter(defaultValue = "${project.build.outputDirectory}", required = true, readonly = true) private File classesDirectory; @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.zip", required = true) private File zipFile; @Override public void execute() throws MojoExecutionException, MojoFailureException { try { Archiver archiver = new ZipArchiver(); archiver.addFile(classesDirectory, "classes/"); archiver.addDirectory(new File(classesDirectory, "../lib/"), "lib/"); archiver.addFile(new File(outputDirectory, finalName + ".jar"), "app.jar"); archiver.createArchive(zipFile); } catch (ArchiverException | IOException e) { throw new MojoExecutionException("Failed to create ZIP archive", e); } } } ``` 在插件的 pom.xml 文件中,指定插件的信息和依赖,如下所示: ```xml <project> <!-- 省略其他配置 --> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-plugin</artifactId> <version>1.0.0</version> <packaging>maven-plugin</packaging> <dependencies> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.6.0</version> <scope>provided</scope> </dependency> <!-- 其他依赖 --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-plugin-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>default-descriptor</id> <goals> <goal>descriptor</goal> </goals> </execution> <execution> <id>help-descriptor</id> <goals> <goal>helpmojo</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> ``` 完成以上步骤后,使用以下命令在您的项目中安装插件: ``` mvn install ``` 然后,在您的项目的 pom.xml 文件中添加以下配置来使用插件: ```xml <project> <!-- 省略其他配置 --> <build> <plugins> <plugin> <groupId>com.example</groupId> <artifactId>my-plugin</artifactId> <version>1.0.0</version> <executions> <execution> <goals> <goal>create-zip</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> ``` 现在,当运行 `mvn package` 命令时,插件将会在项目根目录下的 target 文件夹中生成一个名为 `<project.build.finalName>.zip` 的 ZIP 包。该 ZIP 包将包含编译后的类文件(位于 target/classes 目录下)、可执行的 JAR 包(位于 target 目录下)以及程序运行所需的第三方 JAR 包(位于 target/lib 目录下)。 请注意,这只是一个示例插件,您可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值