Spring学习之依赖注入

关于Spring的依赖注入,英文是 Dependency Injection,翻译过来就是依赖注入。
依赖注入,我理解为就是新建一个对象。

新建一个类,类中有一系列属性,每个属性必须有get和set方法
例如

  • 例子1:
public class Address {
    private String phone;
    private String address;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Address(String phone, String address) {
        this.phone = phone;
        this.address = address;
    }
}
public Address(){
    System.out.println("实例化了,被调用");
}
  • 例子二:
public class Student {
    private String name;
    private Address theAddress;
    private String Sno;
    private String[] books;
    private Map<String,String> map;
    private List<String> hobbys;
    private String wife;
    private Set<String> games;
    private Properties props;



    public Properties getProps() {
        return props;
    }

    public void setProps(Properties props) {
        this.props = props;
    }

    public String getName() {
        return name;
    }

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

    public Address getTheAddress() {
        return theAddress;
    }

    public void setTheAddress(Address theAddress) {
        this.theAddress = theAddress;
    }

    public String getSno() {
        return Sno;
    }

    public void setSno(String sno) {
        Sno = sno;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", theAddress=" + theAddress.toString() +
                ", Sno='" + Sno + '\'' +
                ", books=" + Arrays.toString(books) +
                ", map=" + map +
                ", hobbys=" + hobbys +
                ", wife='" + wife + '\'' +
                ", games=" + games +
                '}';
    }
}

注入方式

1、构造器注入

相当于构造一个实例化对象,在构造时传入参数初始化。
beans.xml

    <bean id="address2" name="adrss" class="grandzio.pojos.Address">
        <constructor-arg name="address" value="河南省" />
        <constructor-arg name="phone" value="13667455"/>
    </bean>

注意:使用构造方法注入时,bean中的属性设置必须和构造方法的参数的个数相同,否则报错

2、set方式注入

相当于使用默认构造方法实例化一个对象,之后用set方法设置属性值

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="student" class="grandzio.pojos.Student">
    <property name="name" value="王杰"/>

    <property name="props">
        <props>
            <prop key="administrator">阿杰</prop>
            <prop key="support">不知道</prop>
            <prop key="development">who</prop>
        </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="hobbys">
        <list>
            <value>"打球"</value>
            <value>"打篮球"</value>
        </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="map">
        <map>
            <entry key="an entry" value="just some string"/>
            <entry key ="a ref" value="myDataSource"/>
        </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
    <property name="games">
        <set>
            <value>"just some string"</value>
            <value>"just some string2"</value>
            <value>"玩着荣耀"</value>
        </set>
    </property>

<!--    设置为空-->
<property  name="wife">
    <null></null>
</property>
    <property name="theAddress" ref="address1"/>
</bean>

<bean id="address1" name="adrs" class="grandzio.pojos.Address">
    <property name="address" value="南京市"/>
    <property name="phone" value="12345678"/>
</bean>
</beans>

测试代码:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
    @Test
public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Address adddr = (Address) context.getBean("address2");
        System.out.println(adddr.toString());
    }
}

在这里插入图片描述注意:属性可以将一个bean设置为另一个bean的属性

3、

如果一个bean是另一个bean的依赖项,这通常意味着一个bean被设置为另一个bean的属性。通常,您可以使用元素在基于XML的配置元数据中。但是,有时bean之间的依赖关系不那么直接。例如,需要触发类中的静态初始化程序,例如数据库驱动程序注册。这个depends-on属性可以显式强制在使用此元素初始化bean之前初始化一个或多个bean。下面的示例使用depends-on属性表示对单个bean的依赖关系:

<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />
<!-- 若要表示对多个bean的依赖关系,请将bean名称列表作为depends-on属性(逗号、空格和分号是有效的分隔符):-->
<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
    <property name="manager" ref="manager" />
</bean>

<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />

bean的单例模式
当定义bean定义并将其作用域为单例时,SpringIoC容器将创建由该bean定义定义的对象的一个实例。此单个实例存储在此类单例bean的高速缓存中,而对该命名bean的所有后续请求和引用都返回缓存的对象。
scope属性设置为singleton

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

非单例原型范围都会导致创建一个新bean实例。换句话说,就是创建新的对象。就是原型模式

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值