spring - the annotation form of spring

Regarding dependency injection:

    1: Injection of map collection: Map maintains LinkedHashMap inside Spring to make the data orderly.

        Entity class:

                                                   

        Configuration:

                                            

    2: Injection of the list collection

                                                                      

    3: Injection through the constructor:

        configuration file

            index is the parameter attribute position, starting from 0

            name is the parameter name in the parameter method

            value directly assigned

            ref reference type

                                

        Precautions:

                                

        When assigning a value to an attribute through a construction method, make sure that the construction parameters in the xml file are consistent with the construction method parameters in the concrete class.

    Notice:

        Under the condition that the source code is not imported, generating the corresponding method may cause the formal parameters to not correspond

                            

        If the name attribute is used in the xml configuration file to specify the parameter name for assignment, there may be problems. It is recommended that you use the index to assign the value.


    4. Parent property  

        When the Spring container creates an object, it is necessary to specify the child-parent relationship. If it is not specified, then when the Spring container creates an object, it will simply create an object and will not maintain the child-parent relationship.

    Use parent=" parent's ID" to specify.

    5. Simplify injection operation autowire--- automatic assembly reference type process

<bean id="person" class="domain.Person" autowire="byName"></bean>

        The auto wire attribute is added to the bean to inject the value of the reference type by yourself, so there is no need to inject manually.

        principle:

            autowire="byName", will first find all the set methods in the current bean , setCat()------> convert and remove the lowercase first letter of the set ------->cat attribute ------ > After getting the cat genus, match it with the bean tag in the configuration file. If the id in the bean matches it, it injects correctly.

             autowire="byType", it will first look for all set methods in the current bean , setCat()------> to convert and remove the lowercase first letter of the set ------->cat attribute ------ > Find the type of cat ( domain .Cat ) ----> match the class type in the bean , if the match is successful, it can be injected correctly.

                                            Note : If you use byType; type injection needs to ensure that the class type is unique

        Configure global:

            default-autowire="default" then won't have any effect . Ifbeanisautowire="default"  then the configuration should be consistent with the global one.

            Summary: Using Auto wire simplifies configuration files. Change the introduced injection method into automatic injection. From now on programmers do not need to pay attention to the injection of object references.



Property annotations:

    Add property annotations to the spring configuration file :

            <!--Start attribute annotation-->
           <context:annotation-config/>

    1-@Autowired注入方式:

        其实就是autowire属性的增强版,它有两种匹配规则。

        1.根据属性的名称与bean中的ID匹配,如果根据bean中的ID值匹配不成功,则根据属性的类型class进行匹配。如果Class类型能够正确匹配。则能注入。如果Class类型没有匹配成功,则报错。

        2.@Autowired + @Qualifier(value="catC"),两个注解同时使用的作用,就是指定具体的bean中的ID与之匹配。并且匹配方式变得唯一,必须按照ID进行匹配。如果找不到匹配的ID值那么必然报错

    2-Spring@Resource介绍: 

        @Resource注解就是@Autowired@Qualifier(value="catC")的增强版,能够实现

            1.按照属性的名称进行匹配bean的id

            2.按照属性的class类型进行匹配bean中的class

            3.按照指定的ID进行匹配

        说明:通常情况下使用@Resource居多,因为功能比较强大。实现的思路和原有的@Autowired不一致。


类的注解:   

    1-开启包扫描,在spring的配置文件中使用

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

        作用:可以创建对象,并且还能为对象赋值(为引用类型赋值)

    2-添加@Component注解在类上

  注入的原理:首先开启包扫描,那么Spring容器在启动的时候就会到相应的包中去查找所有的添加了 @Component注解的类。并且为这些类创建对象,保存到Spring容器内部。如果类中包含了属性注解,那么在创建对象时完成属性的注入。

    3-关于注解ID的生成策略

    @Component
    public class Person{}   person

        在默认的条件下,bean中的id生成的原则是将类名首字母小写person.但是还需查询第二个字母:如果第二个字母是大写的那么首字母大写。

        Person  id------>person

        PErson  id------>PErson

        如果为生成的对象指定具体的ID,这时 Spring中默认的ID生成策略将不生效。


同类型注解

    @PostConstruct   初始化方法

    @PreDestroy        销毁方法

    @Scope(value="")         单例多例

    @Lazy(true)           懒加载

     根据不同的分层情况,设定不同的注解

            Dao       @Repository

            Service   @Service

            Web(servlet)       @Controller

    作用:提高代码的可读性





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325519628&siteId=291194637