Spring's common annotations_hehe.employment.over.34.1

34.1 Commonly used IOCs are classified by function

34.1.1 Used to create objects

  • Used to create objects:<bean> the function is the same as the function realized bywriting atagin the XML configuration file
  • Component:
    • Function: used to store the current class object in the spring container
    • Attributes:
      • value : used to specifybean id. When we do not write, its default value is the current class name, and the first letter is changed to lowercase.
  • Controller : Generally used in the presentation layer
  • Service : generally used in the business layer
  • Repository : generally used in the persistence layer
  • notes:
    • The functions and attributes of the above three annotations are exactly the same as those of Component;
    • The three of them are that the spring framework provides us with clear annotations for the use of the three layers, which makes our three-layer objects more clear;
    • Details: If there is one and only one attribute to be assigned in the annotation, and the name is value, the value can be omitted in the assignment.
  • Example:
    • bean.xml configuration file
<?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">

    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中-->
    <context:component-scan base-package="com.itheima"></context:component-scan>
</beans>
package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import org.springframework.stereotype.Component;

/**
 * 账户的业务层实现类
 */
//@Controller(value="accountService")
//@Service(value="accountService")
//@Repository(value="accountService")
@Component(value="accountService")
public class AccountServiceImpl implements IAccountService {
    
    

    private IAccountDao accountDao ;

    public AccountServiceImpl(){
    
    
        System.out.println("对象创建了");
    }

    public void  saveAccount(){
    
    
        accountDao.saveAccount();
    }
}

34.1.2 Used to inject data

  • Used to inject data: their role is the same as writing a tag in the bean tag in the xml configuration file
  • Autowired:
    • effect: Automatically inject by type. As long as there is only one bean object type in the container that matches the variable type to be injected, the injection can be successful
    • If there is no bean type in the ioc container that matches the type of the variable to be injected, an error is reported.
    • If there are multiple types of matches in the Ioc container: use the object variable name to be injected as the bean id, look it up in the spring container, and the injection can be successful if it is found. Report
      an error ifnot found.
    • Appearance position: It can be on the variable or method
    • Details: When using annotation injection, the set method is not necessary.
  • Qualifier:
    • Function : Inject according to the name on the basis of the injection in the class. It cannot be used alone when injecting class members. But it can be used when injecting method parameters.
    • Attributes:
      • value : Used to specify the id of the injected bean.
    • Example:
@Service("accountService")
//@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
    
    

    @Autowired
    @Qualifier("accountDao1")
    private IAccountDao accountDao = null;
}
  • Resource
    • Function : Inject directly according to the id of the bean. It can be used independently
    • Attributes:
      • name : Used to specify the id of the bean.
    • Example:
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    
    
    @Resource(name = "accountDao2")
    private IAccountDao accountDao = null;
}
  • notes:

    • The three injections of Autowired, Qualifier and Resource can only be injectedData of other bean types, And basic types and String types cannot be implemented using the above annotations.
    • The injection of collection types can only be achieved through XML.
      Example:
  • Value

    • Role : used for injectionBasic type and String typeThe data
    • Attributes:
      • value : Used to specify the value of the data. It can use SpEL in spring (that is, spring el expression)
        • How to write SpEL: ${表达式}

34.1.3 Used to change the scope of action

  • Used to change the scope of scope: their role is the same as that achieved by using the scope attribute in the bean tag
  • Scope
    • Role : used to specify the scope of the bean
    • Attributes:
      • value : The value of the specified range. Common value: singleton prototype

34.1.4 Related to life cycle

  • Related to the life cycle: their role is the same as using init-method and destroy-methode in the bean tag.
  • PreDestroy
    • Role : used to specify the destruction method
  • PostConstruct
    • Role : used to specify the initialization method
/**
 * 账户的业务层实现类
 */
@Service(value="accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
    
    

//  @Autowired
//  @Qualifier("accountDao1")
    @Resource(name = "accountDao2")
    private IAccountDao accountDao = null;

    @PostConstruct
    public void  init(){
    
    
        System.out.println("初始化方法执行了");
    }

    @PreDestroy
    public void  destroy(){
    
    
        System.out.println("销毁方法执行了");
    }

    public void  saveAccount(){
    
    
        accountDao.saveAccount();
    }
}

Guess you like

Origin blog.csdn.net/qq_44686266/article/details/114576674