Mongoosej.Blog.Software.Programming.Java.Framework.Spring.SpringFramework.API


spring-core-xxx.jar

  1. <groupId>org.springframework</groupId>
  2. <artifactId>spring-core</artifactId>

SimpleCommandLineArgsParser

org.springframework.core.env.SimpleCommandLineArgsParser
该类的作用就是提供public CommandLineArgs parse(String… args)方法,将参数args转为CommandLineArgs对象。官方API解释如下。

Parses a String[] of command line arguments in order to populate a CommandLineArgs object. Working with option arguments Option arguments must adhere to the exact syntax:

  • —optName[=optValue]

That is, options must be prefixed with “—“, and may or may not specify a value.If a value is specified, the name and value must be separated without spacesby an equals sign (“=”). Valid examples of option arguments

  • —foo
  • —foo=bar
  • —foo=”bar then baz”
  • —foo=bar,baz,biz

Invalid examples of option arguments

  • -foo
  • —foo bar
  • —foo = bar
  • —foo=bar —foo=baz —foo=biz

Working with non-option arguments Any and all arguments specified at the command line without the “—“ optionprefix will be considered as “non-option arguments” and made available through the CommandLineArgs.getNonOptionArgs() method.

SpringFactoriesLoader

org.springframework.io.support.SpringFactoriesLoader
SpringBoot自动配置实现原理所涉及的一个核心类。

General purpose factory loading mechanism for internal use within the framework. Spring框架内部使用的通用工厂加载机制。 SpringFactoriesLoader loads and instantiates factories of a given type from “META-INF/spring.factories” files which may be present in multiple JAR files in the classpath. The spring.factoriesfile must be in Properties format, where the key is the fully qualified name of the interface or abstract class, and the value is a comma-separated list of implementation class names. For example: SpringFactoriesLoader从“META-INF/spring.factories”文件中加载并实例化给定类型的工厂,这些文件可能存在于类路径中的多个JAR文件中。Spring.factoriesfile必须采用Properties格式,其中键是接口或抽象类的完全限定名称,值是以逗号分隔的实现类名列表。例如 example.MyService=example.MyServiceImpl1,example.MyServiceImpl2 where example.MyService is the name of the interface, and MyServiceImpl1and MyServiceImpl2 are two implementations. example.MyService是接口全限定名,MyServiceImpl1和MyServiceImpl2是两个实现类。

o.s.context.annotation.AnnotationBeanNameGenerator

在用@Component、@Repository、@Service、@Controller等注解创建bean时,如果不指定bean名称,bean名称的默认规则由该类来实现。
总结起来大概是:

  • 如果发现类的前两个字符都是大写,则直接返回类名。例如RBACUser -> RBACUser。
  • 否则将类名的第一个字母转成小写,然后返回。例如SysConfig -> sysConfig。

    o.s.util.Assert

    Assertion utility class that assists in validating arguments. 用于辅助验证参数的断言实用类。 Useful for identifying programmer errors early and clearly at runtime. 有助于在运行时尽早清楚地识别程序员错误。 For example, if the contract of a public method states it does not allow null arguments, Assert can be used to validate that contract. Doing this clearly indicates a contract violation when it occurs and protects the class’s in variants. 例如,如果公共方法声明它不允许空参数,那么可以使用Assert来验证。这样做清楚地表明在发生时违反了契约,并在保护了类的属性。 Typically used to validate method arguments rather than configuration properties, to check for cases that are usually programmer errors rather than configuration errors. In contrast to configuration initialization code, there is usually no point in falling back to defaults in such methods. 通常用于验证方法参数而不是配置属性,检查通常是程序员错误而不是配置错误的情况。与配置初始化代码不同,在这种方法中,通常没有必要回到默认值。 This class is similar to JUnit’s assertion library. If an argument value is deemed invalid, an IllegalArgumentException is thrown (typically).For example:

    • Assert.notNull(clazz, “The class must not be null”);
    • Assert.isTrue(i > 0, “The value must be greater than zero”);

    Mainly for internal use within the framework; consider Apache’s Commons Lang for a more comprehensive suite of String utilities. 主要用于框架内的内部使用;考虑Apache的Commons Lang,以获得更全面的字符串实用程序集。

o.s.util.ResourceUtils

SpringBoot不要使用ResourceUtils读取资源文件
Springboot 生产环境下读取Resource下的文件

org.springframework:spring-beans

  1. <groupId>org.springframework</groupId>
  2. <artifactId>spring-beans</artifactId>

o.s.beans.factory.annotation.Autowired

new出来的对象无法通过@Autowired注入ioc容器中的对象
@Autowired用法详解
在启动spring IoC时,容器自动装载了一个AutowiredAnnotationBeanPostProcessor后置处理器,当容器扫描到@Autowied、@Resource或@Inject时,就会在IoC容器自动查找需要的bean,并装配给该对象的属性。在使用@Autowired时,首先在容器中查询对应类型的bean:

腾讯二面:@Bean与@Component用在同一个类上,会怎么样?