SpringBoot從零單排 ------初級入門篇


  有人說SSM已死,未來是SpringBoot和SpringCloud的天下,這個觀點可能有點極端,但不可否認的是已經越來越多的公司開始使用SpringBoot。所以我將平時學習SpringBoot的內容做個記錄,查漏補缺吧

 

1、創建SpringBoot項目

可以通過官方提供的Spring initializer工具來初始化springboot,同時IntelliJ IDEA 也集成了這個工具。因此可以根據個人需求選擇不同的創建方式

1、官方工具Spring initializer
網址 :https://start.spring.io

下載下的壓縮包進行解壓導入到編輯器中即可。

 

2、Idea創建項目

New -> Project - > spring initializer -> 選擇SDK->填寫Group& Artifact->next->選擇所需jar的依賴(也可暫時勾選)->next->修改項目名->finish

創建成功之后的目錄

 

3、啟動項目

啟動springboot我們只需要執行上圖中的ManApplication中的main方法就可以了。

package com.objectman.springboot_study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootStudyApplication.class, args);
    }

}

 

這個啟動類可以分為兩部分
1、@SpringBootApplication
2、SpringApplication.run

SpringBootApplication

查看源代碼我們發現@SpringBootApplication是一個復合注解,主要包括了
@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

@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 {

}
@SpringBootConfiguration

其實源代碼中的注釋已經描述了這個注解的作用了

/**
 * Indicates that a class provides Spring Boot application
 * {@link Configuration @Configuration}. Can be used as an alternative to the Spring's
 * standard {@code @Configuration} annotation so that configuration can be found
 * automatically (for example in tests).
 * <p>
 * Application should only ever include <em>one</em> {@code @SpringBootConfiguration} and
 * most idiomatic Spring Boot applications will inherit it from
 * {@code @SpringBootApplication}.
 *
 * @author Phillip Webb
 * @since 1.4.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

  

Can be used as an alternative to the Spring’s standard {@code @Configuration} annotation so that configuration can be found 主要意思是可以替代Spring的@Configuration注解。作用是將當前類中用@Bean注解標注的方法實力注入到Spring容器中,實例名就是方法名。
寫個代碼,舉個栗子

定義一個配置類,

import com.objectman.springboot_study.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

@SpringBootConfiguration
public class Configuration_Test {

    public Configuration_Test() {
        System.out.println("=====>>>>> Configuration_Test 容器啟動初始化");
    }

    @Bean
    public User createUser() {
        User user = new User();
        user.setUserName("Object Man");
        user.setAge(18);
        return user;
    }
}

在main方法中可以直接獲取bean。

@SpringBootApplication
public class SpringbootStudyApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(SpringbootStudyApplication.class, args);
		User user = (User) context.getBean("createUser");
		System.out.println("用戶姓名為:" + user.getUserName() + ",今年" + user.getAge() + "歲了");
	}
}

啟動項目控制后台輸出如下

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2019-01-05 20:25:18.761  INFO 71172 --- [           main] c.o.s.SpringbootStudyApplication         : Starting SpringbootStudyApplication on MicroWin10-1123 with PID 71172 (C:\Users\Administrator\IdeaProjects\springboot_study\target\classes started by Administrator in C:\Users\Administrator\IdeaProjects\springboot_study)
2019-01-05 20:25:18.789  INFO 71172 --- [           main] c.o.s.SpringbootStudyApplication         : No active profile set, falling back to default profiles: default
2019-01-05 20:25:19.787  INFO 71172 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-01-05 20:25:19.809  INFO 71172 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-05 20:25:19.809  INFO 71172 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2019-01-05 20:25:19.816  INFO 71172 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [E:\JDK\jdk1.8.0_131\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;E:\JDK\jdk1.8.0_131\bin;E:\JDK\jdk1.8.0_131\jre\bin;C:\WINDOWS\System32\OpenSSH\;E:\apache-maven-3.5.4\bin;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;E:\program\MySql\mysql-8.0.13-winx64\bin;E:\program\node\;C:\Program Files\Git\cmd;E:\Python\Python_Controller\Scripts\;E:\Python\Python_Controller\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Roaming\npm;.]
2019-01-05 20:25:19.900  INFO 71172 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-05 20:25:19.900  INFO 71172 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1001 ms
=====>>>>> Configuration_Test 容器啟動初始化
2019-01-05 20:25:20.124  INFO 71172 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-05 20:25:20.300  INFO 71172 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-01-05 20:25:20.304  INFO 71172 --- [           main] c.o.s.SpringbootStudyApplication         : Started SpringbootStudyApplication in 1.945 seconds (JVM running for 2.714)
用戶姓名為:Object Man,今年18歲了

  

SpringBoot的核心理念約定優於配置,因此通過注解的形式取代了xml配置文件,減少了工作量,也使代碼變得簡潔。

@EnableAutoConfiguration

還是老規矩先看源碼

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
	Class<?>[] exclude() default {};
	String[] excludeName() default {};
}

 

發現有個Import導入了AutoConfigurationImportSelector類。那么這個類是干嘛的呢?查看源碼我們發現有個selectImports方法

 

	public String[] selectImports(AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return NO_IMPORTS;
		}
		//AutoConfigurationMetadataLoader是springboot autoconfigure加載AutoConfigurationMetadata的內部工具類
		AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
				.loadMetadata(this.beanClassLoader);
		AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
				autoConfigurationMetadata, annotationMetadata);
		return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
	}

loadMetadata的實現代碼和相關代碼如下:

//定義一個路徑
	protected static final String PATH = "META-INF/"
			+ "spring-autoconfigure-metadata.properties";

	public static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader) {
		return loadMetadata(classLoader, PATH);
	}
	
	static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader, String path) {
		try {
			//找到自動配置的屬性文件
			Enumeration<URL> urls = (classLoader != null) ? classLoader.getResources(path)
					: ClassLoader.getSystemResources(path);
			//創建一個properties對象,將所有配置文件加載到properties對象中
			Properties properties = new Properties();
			while (urls.hasMoreElements()) {
				properties.putAll(PropertiesLoaderUtils
						.loadProperties(new UrlResource(urls.nextElement())));
			}
			return loadMetadata(properties);
		}
		catch (IOException ex) {
			throw new IllegalArgumentException(
					"Unable to load @ConditionalOnClass location [" + path + "]", ex);
		}
	}

PATH路徑下被自動配置的類有

 

結論:EnableAutoConfiguration會將SpringBoot鎖需要的將配置加載到容器中。

@ComponentScan
可以把它理解為一個掃描器,一個項目中可能會有好多個控制器,我們就是通過ComponentScan去發現指定路徑下的@Controller(@RestController)、@Service、@Repository 、@Component並將他們裝入bean容器中。
他有如下幾個屬性

public enum FilterType {


   ANNOTATION, //按照注解過濾

   ASSIGNABLE_TYPE, //按照給定的類型過濾

   ASPECTJ, //使用ASPECTJ表達式

   REGEX, //通過正則

   CUSTOM //自定義規則

}

  

SpringApplication.run

該過程首先創建了一個SpringApplication對象實例,然后完某些實例的初始化。之后調用run方法。具體詳情可以參考源碼和下圖

 

HelloWorld

添加依賴

pom文件中有個parent標簽

     <parent>
		<!-- 一個非常牛x的依賴,使用之后后面常用的依賴包可以不用寫version了 -->
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
	</parent>

  

這是SpringBoot的一個父級依賴,使用之后相關依賴的時候可以不用填寫版本、默認和父級依賴的版本一樣。然后我們需要在dependencies標簽中添加web依賴模塊

<!-- WEB依賴包 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
</dependency>

編寫Controller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @RestController = @Controller + @ResponseBody
 *
 */
@RestController
public class HelloWorldController {
    /**
     * RequestMapping 將Http請求映射到方法上
     */
    @RequestMapping("/")
    public String HelloWorld() {
        return "Hello World";
    }
}

  然后啟動項目瀏覽器訪問:http://localhost:8080/ 就可以看到瀏覽器輸出"Hello World"

 


歡迎關注我的公眾號:程序員共成長 

 公眾號內回復【禮包】,獲取程序員專屬資料,包括但不限於Java、Python、Linux、數據庫、大數據、架構、測試、前端、ui以及各方向電子書


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM