Using annotation development
1. Description
After spring 4, if you want to use annotation form, you must introduce aop package
In the configuration file, a context constraint must also 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>
2. Bean implementation @ Component
We used to use bean tags for bean injection, but in actual development, we usually use annotations!
- Configure which packages to scan for annotations
<!--Specify annotation scan package--> <context:component-scan base-package="com.xxc.pojo"/>
- Write classes under the specified package and add comments
@Component("user") // When you do not write parentheses, the default is the class initial lowercase name // Equivalent to < bean id = "user" class = "currently annotated class" / > in the configuration file public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
- test
public class MyTest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = context.getBean("user", User.class); user.setName("Jiangnan"); System.out.println(user.getName()); } }
3. Attribute injection @ Value("value")
For the above test code, we will not execute user Setname ("Jiangnan"); Instead, use annotations. Modify our entity class.
User.java
@Component("user") // When you do not write parentheses, the default is the class initial lowercase name // Equivalent to < bean id = "user" class = "currently annotated class" / > in the configuration file public class User { private String name; public String getName() { return name; } @Value("Jiangnan") public void setName(String name) { this.name = name; } }
Note: the initials of notes are generally capitalized.
test
public class MyTest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = context.getBean("user", User.class); System.out.println(user.getName()); } }
Note: there is no setName();
Note: you can directly add @ value("value") to the attribute name without providing a set method
If a set method is provided, add @ value("value") to the set method;
4. Three derived annotations of @ component
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, which function is the same at present.
- @Controller: web layer
- @Service: service layer
- @Repository: dao layer
The above annotations, with the same functions, are equivalent to handing over this class to Spring management assembly!
We wrote the specified annotation scanning package earlier
<context:component-scan base-package="com.xxc.pojo"/>
If derived annotations are used, the class of these annotations is not completely under pojo. At this time, we can expand the scope of the specified annotation scanning package.
<context:component-scan base-package="com.xxc"/>
5. Automatic assembly notes
The automatic assembly of Bean has been talked about.
https://blog.csdn.net/weixin_45842494/article/details/122777536
6. 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.
@Component("user") @scope("prototype") public class User { @Value("Jiangnan") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
7. Summary
XML and annotation comparison
- The XML structure is clear and applicable to any scenario
- 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 explicitly 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!