The introduction

In the last article, we introduced how to define Spring extensions in XML form. Many people have asked whether XML is still used nowadays. It’s out of date. What’s the point of mastering it? The Spring website puts this approach at the bottom of the list, so how important is it? Now that we’re all joking, let’s move on to the annotation-based Spring extension today.

JavaConfig configuration extension

Since Spring3.0, Spring has provided JavaConfig as a way to replace the previous XML approach, which can be replaced by annotations in XML configuration. This is implemented mainly through @Configuration, @bean, @import, and @dependson annotations. This approach is also used by SpringBoot.

@Configuration

@Configuration can only be tagged on a class to indicate that the class is a JavaConfig class so that it can be scanned by the Spring IOC container to create beans to add to the container. The @Configuration class is like an OLD XML file. Here’s an example from the official website:

@Configuration
public class AppConfig {
    @Bean
    public MyService myService(a) {
        return new MyServiceImpl();
    }
Copy the code

The JavaConfig definition is equivalent to the original XML configuration:

<beans>
    <bean id="myService" class="cn.javajr.services.MyServiceImpl"/>
</beans>
Copy the code

@Bean

The @bean can only be tagged on a method, indicating that the method returns a Spring Bean that can be hosted by the IOC container, equivalent to an element previously written in an XML file.

  • Name: Specify one or more bean names. When name is not set, the Spring container will default to the @bean method name as the bean name. When name is set, the method name is not used anymore. Equivalent to the name attribute in the XML configuration.
  • InitMethod: Specifies the method to be called by the container after the bean is initialized. Equivalent to the init-method attribute in the XML configuration.
  • DestroyMethod: Specifies the method to call before the container destroys the bean. Equivalent to destroy-method in XML configuration.
  • Autowire: specifies the dependency injection policy used in autowire. The value can be described in Enum class autowire constants: autowire.BY_NAME, autowire.

@Import

Tags in XML configuration, based on JavaConfig provides @import to combine modular configuration classes as follows:

@Configuration()  
@Import({ApplicationContextConfig.class})  
public class ApplicationContextConfig {
Copy the code

JavaConfig annotations replace XML annotations with JavaConfig annotations, which are very simple to use, especially if you already know XML configuration files.

The Dubbo JavaConfig

In the previous article, we introduced dubbo’s custom extensions through XML. Today, we’ll take a look at how Dubbo replaces XML extensions with JavaConfig. How does dubbo’s service provider implement this through annotations

@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.annotation.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static class ProviderConfiguration {
}
Copy the code

@Configuration, which we’ve already covered above, @enableDubbo @enableDubbo @enableDubBoConfig @DubBoComponentScan is implemented with the following annotation:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Import(DubboConfigConfigurationRegistrar.class)
public @interface EnableDubboConfig {
Copy the code

This annotation using the @ Import (DubboConfigConfigurationRegistrar. Class) so the Spring in the treatment of the @ EnableDubboConfig annotation When to instantiate DubboConfigConfigurationRegistrar and call it registerBeanDefinitions method, this method is mainly for propties file parsing and according to the different configuration Item generates a Bean object of the corresponding type.

conclusion

  • Xml-based and Java-based configuration extensions allow users to use our developed components through Spring, providing ease of use.
  • Although the JavaConfig approach is the norm these days, some people prefer XML

XML allows configuration to be centralized. All components are not scattered, thus giving you a good overview of Beans, such as myBais configuration files, SpingMvc configuration files, all together. If you need to split files, Spring can do that for you. Then (Spring) recombines it through internal tags or aggregates it through external context files.

  • XML and JavaConfig can be mixed, of course, but depending on your programming habits, neither method is absolutely good.
  • After reading these two articles about the different ways Spring extensions can be implemented on our own.

The end of the

  • As a result of their talent and learning, it is inevitable that there will be mistakes, if you found the wrong place, but also hope that the message to me pointed out, I will correct it.
  • If you think the article is good, your forwarding, sharing, appreciation, like, message is the biggest encouragement to me.
  • Thank you for reading. Welcome and thank you for your attention.

Shoulders of giants picking apples: javajr.cn