Summary of spring IOC injection method_study notes

Learning resources come from the blog garden
springIoC container injection methods include set injection, constructor injection, and annotation injection.
The premise of DI requires the container to create the object. The way the spring container creates the object

One, set injection

1. Injection of objects injected by set method

 <!-- 基于set方法的注入 -->
   <bean name="stu" class="com.cnblogs.bean.Student">
       <property name="sNo" value="1001"></property>
       <property name="name" value="jack"></property>
       <property name="age" value="12"></property>
       <property name="sex" value="male"></property>
       <property name="grade" value="三年级"></property>
   </bean>
   <!-- 注入引用类型 -->
   <bean name="A" class="com.cnblogs.bean.A">
       <property name="desc" value="A组合了Student类对象"></property>
       <!-- ref属性表示调用这个setStudent方法的时候要用的参数是名字为stu的对象 -->
       <property name="stu" ref="stu"></property>
   </bean>
  1. In the label, name and id play the role of identifying the object, id will help us to check whether the name given to the object is standardized (the name cannot be repeated, cannot have spaces, and cannot start with a number). The name will not check these things.
  2. By default, springIoC manages objects in singleton mode, that is, objects that are taken out multiple times with the same name can be added with the attribute
    scope="prototype" to represent non-singleton, and scope="Singleton" to represent singleton mode.
  3. You can add an alias to an object and add one at the back. You can get the object through stu, or you can get the object through s1.
  4. When a member variable in a class is an object of another class, it can be introduced in the subtag through ref, and the value of ref is the name or id value of a tag.
  5. Multiple objects can be configured in the same class, but the id or name values ​​that identify multiple objects must be different.
  6. The bottom layer of the set method will use the set method of a bean class. If the member variable of the bean class does not have a set method but uses the set method for injection, an error will be reported.

2. Set injection of set injection

3. Automatic injection of set injection

  1. Automatic injection is generally aimed at combining objects of another type in one class.
  2. Automatic injection has byName injection and byType injection
  3. byName injection: The spring container will look for the name of the property in the current class, and then according to this name to find in the spring container whether there is an object with the same name as the property, if so, put this object as a parameter in setXxxx Inject in the method. (If you find multiple, no error will be reported)
  4. byType injection: The spring container will find a matching object in the container according to the parameter type of the set method, and inject it if it finds it, and forget it if it is not found. If more than one is found, an error will be reported.
  5. You can add default-autowire="byType" to the label, and the label below will be automatically injected according to the byType method.
  6. You can specify the injection method in the label with autowire=" ", which will block default-autowire=" ".

E.g:

<?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-4.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd"
    default-autowire="byName"
>

    <bean name="stu" class="com.cnblogs.bean.Student">
        <property name="sNo" value="1001"></property>
        <property name="name" value="jack"></property>
        <property name="age" value="12"></property>
        <property name="sex" value="male"></property>
        <property name="grade" value="三年级"></property>
    </bean>

    <!-- byName -->
    <bean name="A1" class="com.cnblogs.bean.A">
        <property name="desc" value="A组合了Student类对象,byName"></property>
    </bean>
    
    <!-- byType -->
    <bean name="A2" class="com.cnblogs.bean.A" autowire="byType">
        <property name="desc" value="A组合了Student类对象,byType"></property>
    </bean>
</beans>

Two, constructor injection

The constructor has two injection methods, one is injected according to the parameter type, and the other is injected according to the subscript.

1. Inject according to the parameter type

<!-- 根据参数类型 -->
   <bean name="stu1" class="com.cnblogs.bean.Student">
       <constructor-arg type="String" value="1001"></constructor-arg>
       <constructor-arg type="String" value="jack"></constructor-arg>
       <constructor-arg type="int" value="12"></constructor-arg>
       <constructor-arg type="String" value="male"></constructor-arg>
       <constructor-arg type="String" value="三年级"></constructor-arg>
   </bean>

2. According to the parameter subscript injection

 <!-- 根据下标 -->
   <bean name="stu2" class="com.cnblogs.bean.Student">
       <constructor-arg index="0" value="1002"></constructor-arg>
       <constructor-arg index="1" value="tina"></constructor-arg>
       <constructor-arg index="2" value="12"></constructor-arg>
       <constructor-arg index="3" value="female"></constructor-arg>
       <constructor-arg index="4" value="三年级"></constructor-arg>
   </bean>

Three, annotation injection

类中需要无参构造器!!!
First, you need to specify the package to be injected with annotations in the xml file, and the springIoC container will know when reading this file

<context:component-scan base-package="com.cnblogs.ioc.anatation" /> 

The role of several annotations:

@Autowired
    1) @Autowired使用后需要在xml文件加入以下配置才能生效: <context:annotation-config/>

    2) @Autowired注解可以写在成员变量、setter方法、构造器函数上面
    
    3) @Autowired默认按照byType匹配的方式进行注入,如果没有一个bean的类型是匹配的则会抛异常,如果有多个bean的类型都匹配成功了,
    那么再按byName方式进行选择
    
    4) @Autowired如果最终匹配不成功(注意一定是一个都没有找到的情况)则会抛出异常,但是如果设置为 @Autowired(required=false),
    则最终匹配不成功没有不会抛出异常。
    
    5) @Autowired可以结合@Qualifier("beanName")来使用,则可以达到byName的效果
      


@Resource
    1) @Resource使用后需要在xml文件加入以下配置才能生效:<context:annotation-config/>

    2) @Resource的作用和@Autowired差不多,只不过 @Resource是默认先用byName,如果找不到合适的就再用byType来注入

    3) @Resource有俩个属性,name和type,使用name属性则表示要byName匹配,使用type属性则表示要byType匹配
       

@PostConstruct和@PreDestroy
    1) 标注了@PostConstruct注解的方法将在类实例化后调用
    2) 标注了@PreDestroy注解的方法将在类销毁之前调用
  

@Component
    1) @Component注解可以直接定义bean,而无需在xml定义。但是若两种定义同时存在,xml中的定义会覆盖类中注解的Bean定义

    2) @Component注解直接写在类上面即可

    3) @Component有一个可选的参数,用于指定bean的名称
        @Component("boss")
        public class Boss{}

    4) @Component如果不指定参数,则bean的名称为当前类的类名小写
        //和上面例子的相关相同
        @Component
        public class Boss{}

    5) @Component使用之后需要在xml文件配置一个标签
        <context:component-scan/>

    6) <context:component-scan base-package="com.briup.ioc.annotation" /> 
        表示spring检查指定包下的java类,看它们是否使用了 @Component注解

    7) @Component定义的bean默认情况下都是单例模式的,如果要让这个bean变为非单例,可以再结合这个@Scope注解来达到目标@Scope("prototype")


    @Component是Spring中所有bean组件的通用形式, @Repository @Service @Controller 则是 @Component的细化,用来表示更具体的用例,
    分别对应了持久化层、服务层和表现层。但是至少到现在为止这个四种注解的实质区别很小(甚至几乎没有),都是把当前类注册为spring容器中
    的一个bean


注意:
    1.component-scan标签默认情况下自动扫描指定路径下的包(含所有子包)

    2.component-scan标签将带有@Component @Repository @Service @Controller注解的类自动注册到spring容器中

    3.component-scan标签对标记了@Required @Autowired @PostConstruct @PreDestroy @Resource @WebServiceRef @EJB  
    @PersistenceContext @PersistenceUnit等注解的类进行对应的操作,使注解生效
    
    4.component-scan标签包含了annotation-config标签的作用

Guess you like

Origin blog.csdn.net/qq_40084325/article/details/110946124