java代码常用知识点
1.Assert
java断言assert是jdk1.4引入的。assert这个关键字我们称之为“断言”。当这个关键字后边的条件为假的时候,程序自动崩溃并抛出AssertionError的异常。当这个关键字后面的条件为真的时候,程序继续执行下一句语句。
Assert.hasText(role, "A granted authority textual representation is required");
2.equalsIgnoreCase()和equals()的区别
String a="ABC";
a.equals("abc")为false,
a.equalsIgnoreCase("abc")为true;
equalsIgnoreCase与equals区别是前者不区分大小写,而后者区分
3.instanceof
instanceof 是 Java 的一个二元操作符,类似于 ==,>,< 等操作符。
instanceof 是 Java 的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回 boolean 的数据类型。
以下实例创建了 displayObjectClass() 方法来演示 Java instanceof 关键字用法:
4.@ConditionalOnProperty的作用和用法
在spring boot中有时候需要控制配置类是否生效,可以使用@ConditionalOnProperty注解来控制@Configuration是否生效.
配置类代码:
@Configuration
@ConditionalOnProperty(prefix = "filter",name = "loginFilter",havingValue = "true")
public class FilterConfig {
//prefix为配置文件中的前缀,
//name为配置的名字
//havingValue是与配置的值对比值,当两个值相同返回true,配置类生效.
@Bean
public FilterRegistrationBean getFilterRegistration() {
FilterRegistrationBean filterRegistration = new FilterRegistrationBean(new LoginFilter());
filterRegistration.addUrlPatterns("/*");
return filterRegistration;
}
}
配置文件中的代码
filter.loginFilter=true
当配置文件中值为true时:输出了"过滤器"三个字,说明loginFilter生效了,说明配置类生效了.
当配置文件中值为false时:没有输出了"过滤器"三个字,说明loginFilter没有生效,说明配置类没有生效.
总结:
通过@ConditionalOnProperty控制配置类是否生效,可以将配置与代码进行分离,实现了更好的控制配置.
@ConditionalOnProperty实现是通过havingValue与配置文件中的值对比,返回为true则配置类生效,反之失效.
5.建立META-INF/spring.factories文件的意义何在
平常我们如何将Bean注入到容器当中

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration { @Autowired
HelloProperties helloProperties; @Bean
public HelloService helloService() {
HelloService service = new HelloService();
service.setHelloProperties( helloProperties );
return service;
}
}

一般就建立配置文件使用@Configuration,里面通过@Bean进行加载bean
或者使用@Compont注解在类上进行类的注入
注意:
在我们主程序入口的时候:
@SpringBootApplication这个注解里面的东西

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

里面注解@EnableAutoConfiguration
@ComponentScan注解指扫描@SpringBootApplication注解的入口程序类所在的basepackage下的
所有带有@Component注解的bean,从而注入到容器当中。
但是
如果是加入maven坐标依赖的jar包,就是项目根目录以外的Bean是怎么添加的??
这个时候注解@EnableAutoConfiguration的作用就来了
导入了AutoConfigurationImportSelector这个类:
这个类里面有一个方法

/**
* Return the auto-configuration class names that should be considered. By default
* this method will load candidates using {@link SpringFactoriesLoader} with
* {@link #getSpringFactoriesLoaderFactoryClass()}.
* @param metadata the source metadata
* @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
* attributes}
* @return a list of candidate configurations
*/
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}

@EnableAutoConfiguration注解来注册项目包外的bean。而spring.factories文件,则是用来记录项目包外需要注册的bean类名

为什么需要spring.factories文件,
因为我们整个项目里面的入口文件只会扫描整个项目里面下的@Compont @Configuration等注解
但是如果我们是引用了其他jar包,而其他jar包只有@Bean或者@Compont等注解,是不会扫描到的。
除非你引入的jar包没有Bean加载到容器当中
所以我们是通过写/META-INF/spring.factories文件去进行加载的。
java代码常用知识点的更多相关文章
- Java代码常用写法总结
1.字符串是否为空判断 以下是java 判断字符串是否为空的四种方法:方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equal ...
- Selenium+java自动化测试常用知识点
一.元素的定位 1.通过ID定位元素: findElement(By.id(element)); 2.通过元素的名称定位元素: findElement(By.name(element)); 3.通过元 ...
- java代码---------常用的方法indexOf()和substring()方法的小结、主要是下标都是从0开始,很重要,错了就那个差远了啊
package com.s.x; //indexOf()方法从字符起始处的第一个位子开始的位置 //substring public class Wang { public static void m ...
- Java开发常用知识点总结
docker exec -it imageId redis-cli docker container ls -a docker rm containerId 复制目录&文件 cp -r /ro ...
- JAVA常用知识点及面试题总结
1. String.StringBuffer.StringBuilder三者区别? (1)三者在执行速率上的比较: String<StringBuffer<StringBuilder 原因 ...
- 常用的Java代码汇总
1. 字符串有整型的相互转换 Java 1 2 <strong>Stringa=String.valueOf(2); //integer to numeric ...
- HTML常用知识点代码演示
1 HTML部分常用知识点 <!-- 版本声明 --> <!DOCTYPE html> <!-- 唯一根元素 --> <html> <!-- 对网 ...
- Java 常用知识点
Java 常用知识点 1.日期格式化 SimpleDateFormat Date date=new Date(System.currentTimeMillis()) ; SimpleDateForma ...
- 牛客网Java刷题知识点之关键字static、static成员变量、static成员方法、static代码块和static内部类
不多说,直接上干货! 牛客网Java刷题知识点之关键字static static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中st ...
随机推荐
- operator 之旅(一)
环境准备 依赖版本 MAC M1 kubernetes: 1.18.3 go: 1.17.6 kubebuilder:3.1.0 知识必备 Kubernetes的Group.Version.Resou ...
- [题解]Mail.Ru Cup 2018 Round 1 - D. Changing Array
[题目] D. Changing Array [描述] 给n个整数a[1],...,a[n],满足0<=a[i]<=2^k-1.Vanya可以对这n个数中任一多个数进行操作,即将x变为x' ...
- Gerrit的用法及与gitlab的区别
来到一个新的团队,开发的代码被同事覆盖了.找同事核实,同事却说根本没有看到我的代码.经过一番沟通了解,原来他们的代码没有直接在gitlab上操作,而是先提交到gerrit,然后在提交到git.但是代码 ...
- 安装CentOS7出现dracut:/#……time解决办法
当选择install CentOS7以后一会就会出现错误.报错信息:就是dracut:/# ... timeout一大堆.我本来以为是我的启动盘没做好,后来我又重做了好几次都是这问题. 解决 通过搜索 ...
- c# 编程学习(五)
使用复合赋值和循环语句 使用 while 语句,可在条件为 true 的前提下重复运行一个语句.while 语句的语法如下: while ( booleanExpression ) statemen ...
- VUE3 之 使用标签实现动画与过渡效果 - 这个系列的教程通俗易懂,适合新手
1. 概述 巴纳姆效应告诉我们: 人们更容易相信笼统的.常见的人格描述,并觉得特别适合自己,认为该描述真实地反映了自己的人格面貌. 这也是所有算命先生的小把戏,算命先生通常把话说的很笼统,很通用,基本 ...
- Python的内置数据结构
Python内置数据结构一共有6类: 数字 字符串 列表 元组 字典 文件 一.数字 数字类型就没什么好说的了,大家自行理解 二.字符串 1.字符串的特性(重要): 序列化特性:字符串具有一个很重要的 ...
- MailKit和MimeKit 收发邮件
新建项目,引用MailKit和MimeKit NuGet包 using CommonTool.MailKit; using System; using System.Collections.Gener ...
- 面向对象编程(C++篇1)——引言
目录 1. 概述 2. 详论 2.1. 类与对象 2.2. 数据类型 3. 目录 1. 概述 现代C++与最原始的版本已经差不多是两种不同的语言了.不断发展的C++标准给C++这门语言带来了更多的范式 ...
- linux添加串口权限
通过添加到用户组的方式实现1.由于tty属于"dialout"组别,比如你的用户名是blue, 先命令查看下用户隶属的组别 groups blue 2.如果没有隶属"di ...