Author’s other platforms:

| CSDN: jiangxia _CSDN blog, blog – the written interview questions, Java, blogger front field

| the nuggets: juejin. Cn/user / 651387…

| zhihu: www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

This article is about 1189 words long and it is recommended to read for 8 minutes

Spring is an essential framework for Web development, and the way to configure beans based on traditional XML files is too tedious and reduces the efficiency of development. The advent of annotation development since Spring2.5 has greatly simplified the tedious configuration of daily development. Next, we’ll examine the use of various annotations in Spring through examples.

Without annotation development, component registration usually starts by creating a new entity class:

package com.xinyi.bean;
public class Person {
  private String name;
  private Integer age;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Integer getAge() {
    return age;
  }
  public void setAge(Integer age) {
    this.age = age;
  }
  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";
  }
  public Person(String name, Integer age) {
    super();
    this.name = name;
    this.age = age;
  }
  public Person() {
  }
}

Copy the code

Then create a new configuration file beans.xml under the Resource folder:

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> < bean id =" Person "class =" com. Xinyi. Beans. Person "> < property </property> <property name="age" value="18"></property> </bean> </beans>Copy the code

Then create a new test class:

package com.xinyi; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xinyi.bean.Person; public class MainTest { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Person personbean = (Person) applicationContext.getBean("Person"); System.out.println(personbean); }}Copy the code

The following output is displayed:

Person [name= new, age=18]Copy the code

Next, we will introduce two annotations @Configuration and @Bean.

@Configuration

From Spring3.0, @configuration is used to define Configuration classes that can replace XML Configuration files, annotated classes that contain one or more methods annotated by @bean, These methods will be AnnotationConfigApplicationContext or AnnotationConfigWebApplicationContext scanned, and used to construct the bean definition, initialize the Spring container.

The Configuration class of the @Configuration annotation has the following requirements:

@Configuration cannot be an anonymous class;

@configuration cannot be final;

  1. A nested Configuration must be a static class.

@Bean

@Bean is a method-level annotation that is used primarily in the @Configuration annotation class, but also in the @Component annotation class. @bean injects a Bean into the container of the type of the return value, with the default method name as the ID.

Registering component procedures in the Spring container through annotation development begins by declaring an entity class, as above. Then declare a configuration class:

package com.xinyi.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.xinyi.bean.Person; @configuration public class MyConfig {// @bean injects a Bean into the container of type return value type, Id Default method name as id // Injected person value //person222 is the name of the custom bean @bean ("person222") public person Person111 () {return new The Person (" xinyi, and 19); }}Copy the code

Test class tests:

package com.xinyi; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.xinyi.bean.Person; import com.xinyi.config.MyConfig; Public class MainTest {public static void main(String[] args) { Here is the return to the location of the configuration class ApplicationContext ApplicationContext = new AnnotationConfigApplicationContext (MyConfig. Class); Person person = applicationContext.getBean(Person.class); System.out.println(person); / / return a String bean type [] persontype = applicationContext. GetBeanNamesForType (Person. Class); for(String nameString :persontype) { System.out.println(nameString); }}}Copy the code

The following output is displayed:

Person [name=xinyi, age=19] person222Copy the code

That’s how registering components with @Configuration and @Bean annotations reduces the complexity of Configuration files and provides more flexibility than traditional Configuration file-based development.

Finally, welcome to pay attention to the public number: 1024 notes, free access to massive learning resources (including video, source code, documents)!

Related recommendations:

  • Spring annotation (3) : @scope sets the scope of the component

  • Spring is worth your collection!!

  • Spring annotation (7) : Assign attributes to beans using @value

  • SpringBoot develops Restful interfaces to implement CRUD functions

  • Introduction to distributed cache middleware Redis