spring annotation development

Environment construction

In addition to the xml in the previous document, a context:component constraint should be added

1. xml configuration

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--Specify annotation scan package-->
    <context:component-scan base-package="com"/>
    <!--Open annotation-->
    <context:annotation-config/>
</beans>

Entity class writing

Component

@Data;
@Component("user")
// Equivalent to < bean id = "user" class = "currently annotated class" / > in the configuration file
public class User {
   public String name ;
}

Using annotation injection attributes

  1. You can add @ value("value") to the direct name without providing a set method
@Component("user")
// Equivalent to < bean id = "user" class = "currently annotated class" / > in the configuration file
public class User {
   @Value("yyyyyfff")
   // Equivalent to < property name = "name" value = "yyyfff" / > in the configuration file
   public String name;
}
  1. If a set method is provided, add @ value("value") to the set method;
@Component("user")
public class User {

   public String name;

   @Value("yyyyff")
   public void setName(String name) {
       this.name = name;
  }
}

Derived annotation

@Three derived annotations of Component
For better layering, Spring can use the other three annotations with the same functions. At present, which function is used is the same.

  • @Controller: web layer
  • @Service: service layer
  • @Repository: dao layer
    Writing these annotations is equivalent to giving this class to the Spring management assembly!

Scope

  • Singleton: by default, Spring creates this object in singleton mode. Close the factory and all objects will be destroyed.
  • prototype: multi instance mode. Close the factory and all objects will not be destroyed. The internal garbage collection mechanism will recycle
@Controller("user")
@Scope("prototype")
public class User {
   @Value("XXXXXX")
   public String name;
}

Summary

XML and annotation comparison

  • XML can be applied to any scenario, with clear structure and convenient maintenance
  • Annotations are not self provided classes and cannot be used. Development is simple and convenient

xml and annotation integrated development: Recommended Best Practices

  • xml management Bean
  • Annotation complete attribute injection
  • In the process of using, you can not scan. The scanning is for the annotation on the class

context:annotation-config/
effect:
Make annotation driven registration to make annotations effective
It is used to activate the annotations on the bean s that have been registered in the spring container, that is, to register with spring
If you do not scan the package, you need to configure the bean manually
If it is driven without annotation, the injected value is null!

The JavaConfig class is configured to replace the XML file

1. Write an entity class, Dog

@Component  //Mark this class as a component of Spring and put it in the container!
public class Dog {
   public String name = "dog";
}

2. Create a new config configuration package and write a MyConfig configuration class

@Configuration  //Represents that this is a configuration class
//Equal to < beans > < / beans > in xml file
public class MyConfig {

   @Bean //Register a bean through the method. The return value here is the bean type, and the method name is the bean id!
   public Dog dog(){
       return new Dog();
  }
}

3. Testing

@Test
public void test2(){
  // ClassPathXmlApplicationContext() is not used here
   ApplicationContext applicationContext =
           new AnnotationConfigApplicationContext(MyConfig.class);
   Dog dog = (Dog) applicationContext.getBean("dog");
   System.out.println(dog.name);
}

How to import other configurations?

  1. Let's write another configuration class!
@Configuration  //Represents that this is a configuration class
public class MyConfig2 {
}
  1. In the previous configuration class, let's choose to import this configuration class
@Configuration
@Import(MyConfig2.class)  //Import and merge other configuration classes, similar to the inculde label in the configuration file
public class MyConfig {

   @Bean
   public Dog dog(){
       return new Dog();
  }
}

We will see a lot about the configuration of this Java class in SpringBoot and SpringCloud later. We need to know the role of these annotations!

Keywords: Java Spring SSM

Added by svivian on Thu, 10 Feb 2022 21:04:00 +0200