Common Spring annotations

1. What is annotation driven

When annotation is started, the form of annotation is used to replace xml configuration, which completely eliminates the complicated spring configuration file from the project and simplifies writing

2. Disadvantages of annotation driven

  • In order to achieve the purpose of annotation driven, the original simple writing may be made more complex

  • It is very convenient to configure third-party development resources in XML, but annotation driven can not be edited in third-party development resources, so it will increase the development workload

 

3. Start annotation function

  • Start annotation scanning and load the annotation items configured in the class

    <context:component-scan base-package="packageName"/>

  • explain:

    • When scanning the package, all files in the configured package and its sub packages will be scanned

    • The scanning process is carried out in the form of folder recursive iteration

    • The scanning process reads only valid java files

    • Only spring recognizable annotations are read during scanning

    • After scanning, the recognizable valid annotations will be converted into resources corresponding to spring and added to IoC container

  • be careful:

    • Whether in annotation format or XML configuration format, resources are finally loaded into IoC container. The difference is only that the data is read in different ways

    • In terms of loading efficiency, annotations are better than XML configuration files

 

4. Definition of bean

  • Name: @ Component @Controller @Service @Repository

  • Type: class annotation

  • Location: above class definition

  • Function: set this class as spring managed bean

  • example:

    @Component
    public class ClassName{}

  • explain:

    • @Controller, @ Service, @ Repository are derived annotations of @ Component, and their functions are the same as those of @ Component

  • Related properties

    • value (default): defines the access id of the bean

Example:

  1. Project structure

  2. applicationContext.xml
    <?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">
    
        <!--Start the annotation driver and specify the corresponding scanning path, that is, the package where the resource is located-->
        <context:component-scan base-package="com.itheima" />
    
       <!--Originally, these need to be configured as follows. After using annotations, they do not need to be configured-->
       <!-- <bean id="userService" class="com.itheima.service.impl.UserServiceImpl" />
        <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
        <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>-->
    </beans>
  3. Add annotations to the UserServiceImpl implementation class (the other two configuration methods are similar to them, so they are omitted)
    package com.itheima.service.impl;
    
    import com.itheima.service.UserService;
    import org.springframework.stereotype.Component;
    
    @Component("userService")
    public class UserServiceImpl implements UserService {
        public void save() {
            System.out.println("user service running....");
        }
        public void init() {
            System.out.println("user service init....");
        }
        public void destroy() {
            System.out.println("user service destroy....");
        }
    }
    

5. Scope of bean

  • Name: @ Scope

  • Type: class annotation

  • Location: above class definition

  • Function: set this class as the scope attribute corresponding to the bean

  • example:

    @Scope
    public class ClassName{}

  • Related properties

    • value (default): defines the scope of the bean. The default is singleton

 

6. The lifecycle of a bean

  • Name: @ PostConstruct, @ PreDestroy

  • Type: Method annotation

  • Location: above method definition

  • Function: set this class as the corresponding life cycle method of bean

  • example:

    @PostConstruct
    public void init() { System.out.println("init..."); }

Add annotation in serviceimpl example

package com.itheima.service.impl;

import com.itheima.service.UserService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component("userService")
@Scope("singleton")

public class UserServiceImpl implements UserService {
    public void save() {
        System.out.println("user service running....");
    }

    @PostConstruct
    public void init() {
        System.out.println("user service init....");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("user service destroy....");
    }
}

7. Load third-party resources

  • Name: @ Bean

  • Type: Method annotation

  • Location: above method definition

  • Function: set the return value of this method as the bean managed by spring

  • example:

    @Bean("dataSource")
    public DruidDataSource createDataSource() {    return ......;    }

  • explain:

    • Because third-party beans cannot be modified on their source code, use @ Bean to solve the problem of introducing third-party beans

    • This annotation is used to create bean s instead of static factories and instance factories in XML configuration. It does not distinguish whether the method is static or non static

    • @The class of the Bean must be scanned and loaded by spring, otherwise the annotation cannot take effect

  • Related properties

    • value (default): defines the access id of the bean

Keywords: Java Spring Back-end

Added by Homer30 on Sun, 20 Feb 2022 13:00:08 +0200