Using annotation development
After spring 4, if you want to use annotation form, you must introduce aop package
In the configuration file, a context constraint [context] must be introduced
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
1. Implementation of Bean
We used to use bean tags for bean injection, but in actual development, we usually use annotations!
1. Configure which packages to scan for annotations
<!-- Open annotation--> <context:annotation-config/> <!-- Open scan package--> <context:component-scan base-package="com.q"/>
2. Write classes under the specified package and add comments
@Component //Common class annotation public class person { public String name = "hello"; //Attribute injection 1 mode }
3. Testing
public class MyTeat { @Test public void test1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); person person = applicationContext.getBean("person", person.class); System.out.println(person); System.out.println(person.name); } }
4. Output
com.q.pojo.person@59505b48 hello
2. Attribute injection
Using annotation injection attributes
1. You can add @ value("value") to the direct name without providing a set method
@Component //Common class annotation // Equivalent to < bean id = "user" class = "currently annotated class" / > in the configuration file public class person { // public String name = "hello"; // Attribute injection 1 mode // Equivalent to < property name = "name" value = "hello2" / > in the configuration file @Value("hello2") public String name; //Attribute injection 2 mode
2. If a set method is provided, add @ value("value") to the set method;
public String name; @Value("Annotation injection information: name name") public void setName(String name) { this.name = name; }
3. Derived annotation
These annotations replace the configuration steps in the configuration file! More convenient and fast!
@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
- @Component: pojo layer
Writing these annotations is equivalent to giving this class to the Spring management assembly!
4. Auto assembly annotation
Automatic assembly in Bean has been written and can be reviewed!
5. Scope
@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("leslie") public String name; }
6. 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
<!-- Open annotation--> <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!
Configuration based on Java classes
JavaConfig was originally a sub project of Spring. It provides Bean definition information through Java classes. In the version of Spring 4, JavaConfig has officially become the core function of Spring 4.
Test:
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 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(){ ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Dog dog = (Dog) applicationContext.getBean("dog"); System.out.println(dog.name); }
4. Output results successfully!
How to import other configurations?
1. Let's write another configuration class!
@Configuration //Represents that this is a configuration class public class MyConfig2 { }
2. 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(); } }
This is the core of speingBoot's automatic assembly.
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!