20200111 - spring annotation-based IOC and IOC case

** spring case-based annotation IOC and the IOC **
1 in the Spring of ioc Common notes

2 columns using xml text annotation mode and single-mode table crud
persistence Technical Options: d'butils

3 Based on the transformation of the way ioc annotation, annotations using pure way
spring to use some of the new notes

4 spring and integration of junit

Annotation-based configuration ioc
comment xml configuration and configuration functions implemented are the same, are reducing the coupling between programs, but not the same form.

 * <!--    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>-->
  • For creating an object
  •  他们的作用就是xml配置文件中编写一个bean标签实现的功能是一样的
    
  • Data for injection
  •  他们的作用就喝在xml配置文件中的bean标签中的property标签一致
    
  • For changing the scope of
  •  他们的作用就和在bean标签中使用scope属性实现的功能是一样的
    
  • And life-cycle-related
  •  他们的作用就和在bean标签中使用init-methods 和 destroy-methods的作用是一样的
    

component notes

For the current class object is stored in the spring container
attribute is used to specify the value of the bean id when we do not write, its default value is the current class name and the first letter lowercase

aop is a comment must use the jar package

@Component
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao ;

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

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

This class generates an object identifier stored in the spring container
next need to inform the spring in bean.xml scan package
configuration label is no longer the beans, and the name context namespace constraints

Step Loading: namespace name and context constraints
Step introduced: into the label arranged in the scanning package

    <context:component-scan base-package="com.itheima"></context:component-scan> 

Value corresponding to the specified attribute may be added later in the tag @componennt

@Component(value = "mmz")

Three notes
Controller Presentation Layer
Service business layer
Respository persistence layer
more than three notes their role and attributes of the component is exactly the same. They are three clear spring framework we use to provide three layers of annotation. Our three objects more clearly

        IAccountDao adao  = (IAccountDao) ac.getBean("accountDaoImpl");
        System.out.println(adao);

Use bean repository notes identified

@Repository
public class AccountDaoImpl implements IAccountDao {

    public  void saveAccount(){

        System.out.println("保存了账户");
    }
}

Notes inject data
Autowired role: automatic injection by type, as long as the container has a unique bean object type and variable type matching to be injected can be injected successfully
if ioc does not have any type of bean types and variables to be injected into the match error
if there are multiple ioc when the type of match:
appearance location: can be a member variable, it can be a method
details: when using annotation injection, set method is not a must

springIOC structure is a map
key value is of type String Object Type

Qualifier:
Role: In accordance with the type of injection on the basis of the injection by name. It is injected to the class members, can not be used alone, but a method can be time parameter injection.
Attribute: value specifies the id of the bean injection
can not be used alone

Resource Role: direct injection according to the bean id, it can be used independently
but his problem, not value, but the name of the

Three or more notes can only inject other bean types of data, the basic type, String type can not be achieved using the annotations
Further, the type of set is achieved by injecting only class xml

@Value for basic types of annotation and of type String
attribute value: a value for the specified data, it can use the EL el expressions spring
SPel written expression is $ {}

For changing the scope
Scope effect: for specifying the bean scope
attribute: value: value of the specified range. Common values, singleton prototype embodiment corresponding to the single cases of

And lifecycle
PreDestroy effect: for the destruction method
PostConstruct: for initialization method

Published 657 original articles · won praise 39 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/103939284