怎么做彩票網(wǎng)站seol英文啥意思
基本概念
春天
Spring 是用于開發(fā) Java 應(yīng)用程序的開源框架,為解決企業(yè)應(yīng)用開發(fā)的復(fù)雜性而創(chuàng)建。
- Spring 的基本設(shè)計(jì)思想是利用 IOC(依賴注入)和 AOP (面向切面)解耦應(yīng)用組件,降低應(yīng)用程序各組件之間的耦合度。
- 在這兩者的基礎(chǔ)上,Spring 逐漸衍生出了其他的高級(jí)功能:如 Security,JPA 等。
Spring MVC
Spring MVC 是 Spring 的子功能模塊,專用于 Web 開發(fā)。
Spring MVC 基于 Servlet 實(shí)現(xiàn),將 Web 應(yīng)用中的數(shù)據(jù)業(yè)務(wù)、顯示邏輯和控制邏輯進(jìn)行分層設(shè)計(jì)。開發(fā)者可以直接調(diào)用 Spring MVC 框架中 Spring 解耦的組件,快速構(gòu)建 Web 應(yīng)用。
Spring 引導(dǎo)
Spring Boot 是用于簡化創(chuàng)建 Spring 項(xiàng)目配置流程,快速構(gòu)建 Spring 應(yīng)用程序的輔助工具。Spring Boot 本身并不提供 Spring 框架的核心特性以及擴(kuò)展功能。但 在創(chuàng)建 Spring 項(xiàng)目時(shí),Spring Boot 可以:
- 自動(dòng)添加 Maven 依賴,不需要在 pom.xml 中手動(dòng)添加配置依賴。
- 不需要配置 XML 文件,將全部配置濃縮在一個(gè) appliaction.yml 配置文件中。
- 自動(dòng)創(chuàng)建啟動(dòng)類,代表著本工程項(xiàng)目和服務(wù)器的啟動(dòng)加載。
- 內(nèi)嵌 Tomcat 、Jetty 等容器,無需手動(dòng)部署 war 文件。
Spring Boot 配置
依賴
在Spring Boot中,引入的所有包都是 starter 形式:
spring-boot-starter-web-services,針對(duì) SOAP Web Services spring-boot-starter-web,針對(duì) Web 應(yīng)用與網(wǎng)絡(luò)接口 spring-boot-starter-jdbc,針對(duì) JDBC spring-boot-starter-data-jpa,基于 Hibernate 的持久層框架 spring-boot-starter-cache,針對(duì)緩存支持
默認(rèn)映射路徑
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
優(yōu)先級(jí)順序:META-INF/resources > 資源 > static > public
全局配置
位于 resources 文件夾下,支持以下兩種格式。由 Spring Boot 自動(dòng)加載。
- application.properties
- application.yml
#端口號(hào)
server.port=8080
#訪問前綴
server.servlet.context-path=/demo
#端口號(hào)
server.port=8080
#訪問前綴
server.servlet.context-path=/demo#數(shù)據(jù)庫驅(qū)動(dòng)
jdbc.driver=com.mysql.jc.jdbc.Driver
#數(shù)據(jù)庫鏈接
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
#數(shù)據(jù)庫用戶名
jdbc.username=root
#數(shù)據(jù)庫密碼
jdbc.password=wdh19970506#Mybatis
#配置文件路徑
mybatis_config_file=mybatis-config.xml
#SQL語句配置路徑
mapper_path=/mapper/**.xml
#實(shí)體類所在包
type_alias_package=com.example.demo.entity
- JDBC 連接 Mysql5 驅(qū)動(dòng): com.mysql.jdbc.Driver
- JDBC 連接 Mysql6 驅(qū)動(dòng): com.mysql.cj.jdbc.Driver , URL 必須要指定時(shí)區(qū) serverTimezone !
多重配置
在 Spring Boot 中,我們往往需要配置多個(gè)不同的配置文件去適應(yīng)不同的環(huán)境:
application-dev.properties
開發(fā)環(huán)境application-test.properties
測試環(huán)境application-prod.properties
生產(chǎn)環(huán)境
只需要在程序默認(rèn)配置文件?中設(shè)置環(huán)境,就可以使用指定的配置。application.properties
spring.profiles.active=dev
啟動(dòng)類
@SpringBootApplication
類:作為程序入口,在創(chuàng)建 Spring Boot 項(xiàng)目時(shí)自動(dòng)創(chuàng)建。
等同于 + + ,會(huì)自動(dòng)完成配置并掃描路徑下所有包。@Configuration
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
Spring 需要定義調(diào)度程序 servlet ,映射和其他支持配置。我們可以使用 web.xml 文件或 Initializer 類來完成此操作:
public class MyWebAppInitializer implements WebApplicationInitializer {@Overridepublic void onStartup(ServletContext container) {AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();context.setConfigLocation("com.pingfangushi");container.addListener(new ContextLoaderListener(context));ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context));dispatcher.setLoadOnStartup(1);dispatcher.addMapping("/");}
}
還需要將?注釋添加到?類,并定義一個(gè)視圖解析器來解析從控制器返回的視圖:@EnableWebMvc
@Configuration
@EnableWebMvc
@Configuration
public class ClientWebConfig implements WebMvcConfigurer { @Beanpublic ViewResolver viewResolver() {InternalResourceViewResolver bean= new InternalResourceViewResolver();bean.setViewClass(JstlView.class);bean.setPrefix("/WEB-INF/view/");bean.setSuffix(".jsp");return bean;}
}