Spring IOC-- dependency injection

The spring dependency injection

IOC action: to reduce the coupling (dependencies) between programs
dependencies management: to spring to maintain the (current class need to use objects of other classes, provided by the spring for us, we just need to explain in the configuration file)
dependence maintenance of relationships: Dependency injection
Dependency injection:
you can inject data:

  1. Basic types and String
  2. Other bean types (in the configuration file or the configuration notes had bean)
  3. Complex Type / collection type

Object bean injection method:

  1. Using Constructors
  2. Use the set function
  3. Use annotations

Using Constructors

To the constructor: |
Tags used: constructor- arg
location tag appear: internal bean tag
label attribute
type: type of data used to specify the data to be injected, the data type is one or a constructor these types of parameters
index: injecting data used to specify the index position of the specified parameter assigned to the constructor. Starting from the position of the index E
name: constructor specifies the name of the specified parameter assignment common
=========These three parameters for efficient assignment which is assigned to the constructor
value: String for providing basic types and data types
ref: other bean types for the specified data. It refers to appear in the spring of Ioc core container off

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="accountService" class="com.ay.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="飞扬"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>
    <bean id="now" class="java.util.Date" ></bean>
</beans>
package com.ay.service;

public interface AccountService {
    public  void saveAccount();
}

package com.ay.service.impl;

 import com.ay.service.AccountService;

 import java.util.Date;

public class AccountServiceImpl implements AccountService {
    private String name;
    private Integer age;
    private Date birthday;
    @Override
    public void saveAccount() {
        System.out.println("方法创建成功了");
    }

    public AccountServiceImpl(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "AccountServiceImpl{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}

package com.ay.ui;

import com.ay.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as = (AccountService)ac.getBean("accountService");
        as.saveAccount();
        System.out.println(as.toString());
    }
}

Here Insert Picture Description
Summary:
Advantage: Objects in obtaining bean, inject data is a must, otherwise the object can not be created successfully.
Drawbacks: changing the way bean instantiating the object, so that when we create objects, if less than this data must also be provided.

Use the set function

Property: according to the tag
location appears: internal bean tag
label attribute
name: specifies the name of a method to set the called fashionable
value: String for providing basic types and data types
ref: specifies other bean the type of data. It refers to appear in the spring of core Ioc container had bean object

package com.ay.service;

public interface AccountService {
    public  void saveAccount();
}

package com.ay.service.impl;

 import com.ay.service.AccountService;

 import java.util.Date;

public class AccountServiceImpl implements AccountService {
    private String name;
    private Integer age;
    private Date birthday;
    @Override
    public void saveAccount() {
        System.out.println("方法创建成功了");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "AccountServiceImpl{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}

package com.ay.ui;

import com.ay.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as = (AccountService)ac.getBean("accountService");
        as.saveAccount();
        System.out.println(as.toString());
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="accountService" class="com.ay.service.impl.AccountServiceImpl">
        <property name="name" value="飞扬"></property>
        <property name="age" value="20"></property>
        <property name="birthday" ref="now"></property>
     </bean>
    <bean id="now" class="java.util.Date" ></bean>
</beans>

Here Insert Picture Description
Summary:
Advantages: there is no clear limit when you create an object, you can directly use the default constructor
drawbacks: if a member must have a value, it is possible to get the object set method is not implemented.

Released eight original articles · won praise 27 · views 330

Guess you like

Origin blog.csdn.net/qq_44706044/article/details/103988392