1. What is annotation
- Annotation is a code special tag, format: @ annotation name (attribute name = attribute value, attribute name = attribute value...)
- Use annotations, which act on classes, properties, and methods
- The purpose of using annotations is to simplify xml configuration
2. Spring provides annotations for creating objects in Bean management
- @Component
- @Service
- @Controller
- @Repository
The above four annotation functions are the same and can be used to create bean instances
The different names are just for the convenience of distinguishing each code layer in the code.
3. Object creation based on annotation
Step 1: introduce dependencies (Spring-aop-5.2.6.RELEASE.jar)
Step 2 start component scanning
- If multiple packages are scanned, they are separated by commas
- Scan package upper directory
<context:component-scan base-package="com.clearlast"></context:component-scan>
The third step is to create a class and add object annotations on the class
-
In the annotation, the value attribute value can be omitted,
The default value is the class name, which is lowercase
UserService – userService@Component(value = "userService") //Equivalent to < bean id = "userservice" class = ".." / > public class UserService { public void add() { System.out.println("add......."); } }
4. Enable component scanning details configuration
Use default filters = "false" means that you do not use the default filter now, but configure the filter yourself
Context: include filter, which sets what content to scan
Context: Exclude Filter: sets which contents are not scanned
-
If you want to configure the scanned content yourself, first set use default filters to false,
Generally, use default filters = "false" and context: include filter are used together. -
If you just want to set some contents not to be scanned, you can directly configure context: exclude filter, because they are scanned by default.
-
give an example:
3.1 set what content needs to be scanned
<context:component-scan base-package="com.clearlast" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
3.2 set which contents do not need to be scanned
<context:component-scan base-package="com.atguigu"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
5. Attribute injection based on annotation
-
@Autowired: automatically assemble according to the attribute type
-
@Resource: can be injected according to type or name
@Resource(name = "userDaoImpl1") -
@Value: inject common type attribute
@Value(value = "abc") -
@Qualifier: inject the @ qualifier annotation according to the name,
Use with @ Autowired above
@Qualifier(value = "userDaoImpl1")Example:
@Autowired //Injection according to type @Qualifier(value = "userDaoImpl1") //Injection by name private UserDao userDao;
6. Fully annotated development
-
@Configuration (represents that this class is a configuration class)
-
@ComponentScan (basePackages = {"..."}) (packages to scan)
-
Create a configuration class to replace the xml configuration file
@Configuration //As a configuration class, replace the xml configuration file @ComponentScan(basePackages = {"com.clearlast"}) public class SpringConfig { }
-
Writing test classes
@Test public class test(){ ApplicationContext context = new AnnotationConfigApplicationContext("SpringConfig.class"); context.getbean("xxx"); }