From spring to springboot

1、 From spring to springboot

With the growing ecosystem of spring, spring has become more and more complex and bloated. For example, if you want to develop based on a set of SSM framework, you need to configure a lot of things and integrate a lot of things.

Therefore, spring boot came into being. So what makes him simple?

That is: Convention is greater than configuration

What is springboot?

Springboot makes it easy to create independently running, production level spring applications. You can run it directly. Most springboot applications require only a small amount of configuration to use.

Springboot features:

Quickly build Maven project

Can be in https://start.spring.io Quickly generate a springboot project.

2、 Core principles of springboot

Automatic configuration, configuration based, enablexx, condition

Spring boot starter: scaffold core, rapid integration of third-party class libraries

The getautoconfigurationentry method of autoconfigurationimportselector class will first judge whether automatic configuration is enabled. If enabled, it will scan all configurations in spring The name in the factories is a class with enableautoconfiguration.

The main codes are as follows:

 protected List<String> getCandidateConfigurations(AnnotationMetadata Metadata,AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(),this.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;
    }

    protected Class<?> getSpringFactoriesLoaderFactoryClass() {
        //去扫描name为EnableAutoConfiguration的类
        return EnableAutoConfiguration.class;
    }
public static List<String> loadFactoryNames(Class<?> factoryType,@Nullable ClassLoader classLoader) {
		ClassLoader classLoaderToUse = classLoader;
		if (classLoaderToUse == null) {
			classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
		}
		String factoryTypeName = factoryType.getName();
		//根据factoryTypeName去load类
		return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName,Collections.emptyList());
	}

	private static Map<String,List<String>> loadSpringFactories(ClassLoader classLoader) {
		Map<String,List<String>> result = cache.get(classLoader);
		if (result != null) {
			return result;
		}

		result = new HashMap<>();
		try {
		    //FACTORIES_RESOURCE_LOCATION静态常量,值为Meta-INF/spring.factories,读取spring.factories文件,并进行处理
			Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
			...

Annotations for conditional autoconfiguration

Custom starter

See my blog: https://www.cnblogs.com/javammc/p/13893698.html

Why should the contract be greater than the configuration

The advantage is that out of the box:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>