spring dependency injection

spring dependency injection

Dependency injection concept

Dependency Injection: Dependency Injection. It is a concrete implementation framework core ioc spring of. Our program in the preparation, by inversion of control, to create objects to the spring, but the code does not depend on circumstances that can not happen. ioc decoupling only reduce their dependency, but not eliminated. For example: Our business layer will call the method persistence layer. This dependency that the business layer and persistence layer, after using the spring, let the spring to maintain. Simply put, it is to wait for the incoming object persistence layer framework to the business layer, instead of our own to get.

IOC role

Reduce the coupling (dependencies) between programs
dependencies management: after all spring to be maintained

In 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

Dependency maintenance: it is called dependency injection.
Dependency injection: capable of injecting data : There are three basic types of String and other bean types (in the configuration file or the configuration over the annotation bean) complex type / collection type injection ways : three first : using the constructor provided second: using the set method to provide a third: use annotations to provide (content tomorrow)







bean .xml

<?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">



Constructor injection

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 some of the constructor parameters type
index: used to specify injection parameter assignment data specifying the position of the index to the constructor. The position of the index is zero
name: specifies the name of a constructor parameter assignment specified common
=For designating to which three or more parameter assignment 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 core Ioc container had bean object

Advantage:
When obtaining bean object, inject data is a must, otherwise the object can not be created successfully.
Drawbacks:
changing the way the bean is instantiated object, so that when we create objects, if less than this data must also be provided.

bean .xml

<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
    <constructor-arg name="name" value="泰斯特"></constructor-arg>
    <constructor-arg name="age" value="18"></constructor-arg>
    <constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>

<!-- 配置一个日期对象 -->
<bean id="now" class="java.util.Date"></bean>

set injection method

Label according to: property
where they appear: internal bean tag
label attribute
name: set the name of a method for specifying the injection when invoked
value: String for providing basic types and data types
ref: with bean to specify other types of data. It refers to appear in the spring of core Ioc container had bean objects
advantage:
there is no clear limit, you can directly use the default constructor to create objects
drawbacks:
if a member must have a value, you get the object it is possible to set method is not implemented.
-bean .xml

    <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
        <property name="name" value="TEST" ></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="now"></property>
    </bean>

Complex type of injection

Collection type injection
for injection to set a tag structure List: list array set
for one set of injection Map structures Tags: map props
same configuration, the label may be interchanged

bean .xml

<bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
    <property name="myStrs">
        <set>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </set>
    </property>

    <property name="myList">
        <array>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </array>
    </property>

    <property name="mySet">
        <list>
            <value>AAA</value>
            <value>BBB</value>
            <value>CCC</value>
        </list>
    </property>

    <property name="myMap">
        <props>
            <prop key="testC">ccc</prop>
            <prop key="testD">ddd</prop>
        </props>
    </property>

    <property name="myProps">
        <map>
            <entry key="testA" value="aaa"></entry>
            <entry key="testB">
                <value>BBB</value>
            </entry>
        </map>
    </property>
</bean>

IAccountService.java

package com.itheima.service;

/**
 * 账户业务层的接口
 */
public interface IAccountService {

    /**
     * 模拟保存账户
     */
    void saveAccount();
}

accountService.java

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

import java.util.Date;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;

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

    public void  saveAccount(){
        System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);
    }


}

accountService2.java

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

import java.util.Date;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl2 implements IAccountService {

    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;

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

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

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

    public void  saveAccount(){
        System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);
    }


}

accountService3.java

package com.itheima.service.impl;

import com.itheima.service.IAccountService;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.Map;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl3 implements IAccountService {

    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String,String> myMap;
    private Properties myProps;

    public void setMyStrs(String[] myStrs) {
        this.myStrs = myStrs;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }

    public void  saveAccount(){
        System.out.println(Arrays.toString(myStrs));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myProps);
    }

}

Client.java simulate a presentation layer presentation layer, the business layer for calls

package com.itheima.ui;

import com.itheima.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id获取Bean对象
//        IAccountService as  = (IAccountService)ac.getBean("accountService");
//        as.saveAccount();

//        IAccountService as  = (IAccountService)ac.getBean("accountService2");
//        as.saveAccount();

        IAccountService as  = (IAccountService)ac.getBean("accountService3");
        as.saveAccount();

    }
}

Guess you like

Origin www.cnblogs.com/Lilwhat/p/12584155.html