JavaWeb开发常用技术及其使用场景

SpringBoot
多Profile文件
我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml 
默认使用application.properties的配置;
@Value获取值和@ConfigurationProperties获取值比较
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和置文件进行映射,我们就直接使用@ConfigurationProperties;
@PropertySource:加载指定的配置文件
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;		想让Spring的配置文件生效,加载进来;
@ImportResource标注在一个配置类上SpringBoot推荐给容器中添加组件的方式;
推荐使用全注解的方式
1、配置类@Configuration------>Spring配置文件
2、使用@Bean给容器中添加组件
yml支持多文档块方式
server:
  port: 8081
spring:
  profiles:
    active: prod
‐‐‐
server:
  port: 8082
spring:
  profiles: dev
‐‐‐
server:
  port: 8080
spring:
  profiles: prod #指定属于哪个环境
激活指定profile
1、在配置文件中指定 spring.profiles.active=dev
2、命令行:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
可以直接在测试的时候,配置传入命令行参数
3、虚拟机参数:-Dspring.profiles.active=dev
通过spring.config.location来改变默认的配置文件位置
项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;
指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.properties
SpringBoot自动配置原理
整个自动装配的过程是:
Spring Boot通过@EnableAutoConfiguration注解开启自动配置,
加载spring.factories中注册的各种AutoConfiguration类,
当某个AutoConfiguration类满足其注解@Conditional指定的生效条件
(Starters提供的依赖、配置或Spring容器中是否存在某个Bean等)时,
实例化该AutoConfiguration类中定义的Bean(组件等),并注入Spring容器,
就可以完成依赖框架的自动配置
@EnableAutoConfiguration 作用
利用EnableAutoConfigurationImportSelector给容器中导入一些组件?
可以查看selectImports()方法的内容;
List configurations = getCandidateConfigurations(annotationMetadata, attributes)
获候选的配置
	SpringFactoriesLoader.loadFactoryNames()扫描所有jar包类路径下
	META‐INF/spring.factories
	把扫描到的这些文件的内容包装成properties对象从properties中获取到
	EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中
自动配置类必须在一定的条件下才能生效
我们可以通过启用 debug=true属性;来让控制台打印自动配置报告,
这样我们就可以很方便的知道哪些自动配置类生效;
1、Positive matches:(自动配置类启用的)
2、Negative matches:(没有启动,没有匹配成功的自动配置类)
市面上的日志框架
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j....
日志门面 (日志的抽象层)日志实现
JCL(Jakarta Commons Logging SLF4j(Simple LoggingFacade for Java) jboss-logginLog4j JUL(java.util.logging)Log4j2 Logback
SpringBoot:底层是Spring框架,Spring框架默认是用JCL;
SpringBoot选用 SLF4j和logback;
SpringBoot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,
引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉即可;
Web模块
模板引擎
JSP、Velocity、Freemarker、Thymeleaf;
SpringBoot推荐的Thymeleaf
Thymeleaf使用
只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;
导入thymeleaf的名称空间:<html lang="en" xmlns:th="http://www.thymeleaf.org">
Thymeleaf语法规则
1)、th:text;改变当前元素里面的文本内容;
th:任意html属性;来替换原生属性的值

Thymeleaf表达式
Simple expressions:(表达式语法)
	Variable Expressions: ${...}:获取变量值;OGNL;
		1)、获取对象的属性、调用方法
		2)、使用内置的基本对象:
			#ctx : the context object.
			#vars: the context variables.
			#locale : the context locale.
			#request : (only in Web Contexts) the HttpServletRequest object.
			#response : (only in Web Contexts) the HttpServletResponse object.
			#session : (only in Web Contexts) the HttpSession object.
			#servletContext : (only in Web Contexts) the ServletContext object.
			${session.foo}
		3)、内置的一些工具对象:
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the
same way as they would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for example, as a
result of an iteration).
	Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
		补充:配合 th:object="${session.user}:
	<div th:object="${session.user}">
	<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
	<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
	<p>Nationality: <span th:text="*{nationality}">Saturn</span>.				   </p>
	</div>
	
	Message Expressions: #{...}:获取国际化内容
	Link URL Expressions: @{...}:定义URL;
			@{/order/process(execId=${execId},execType='FAST')}
	Fragment Expressions: ~{...}:片段引用表达式
			<div th:insert="~{commons :: main}">...</div>
			
Literals(字面量)
	Text literals: 'one text' , 'Another one!' ,…
	Number literals: 0 , 34 , 3.0 , 12.3 ,…
	Boolean literals: true , false
	Null literal: null
	Literal tokens: one , sometext , main ,…
Text operations:(文本操作)
	String concatenation: +
	Literal substitutions: |The name is ${name}|
Arithmetic operations:(数学运算)
	Binary operators: + , ‐ , * , / , %
	Minus sign (unary operator): ‐
lean operations:(布尔运算)
	Binary operators: and , or
	Boolean negation (unary operator): ! , not
Comparisons and equality:(比较运算)
	Comparators: > , < , >= , <= ( gt , lt , ge , le )
	Equality operators: == , != ( eq , ne )
Conditional operators:条件运算(三元运算符)
	If‐then: (if) ? (then)
	If‐then‐else: (if) ? (then) : (else)
	Default: (value) ?: (defaultvalue)
Special tokens:
	No‐Operation: _
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值