Spring学习笔记1——依赖注入的方式与命名空间

8 篇文章 0 订阅

依赖注入的两种方式:构造器注入&getset注入

演示代码结构:

 HelloBean 无构造函数,有getset方法。

package com.glodon.springdemo1;

public class HelloBean {
    private HelloBeanFeature1 helloBeanFeature1;

    public void setHelloBeanFeature1(HelloBeanFeature1 helloBeanFeature1) {
        this.helloBeanFeature1 = helloBeanFeature1;
    }

    public HelloBeanFeature1 getHelloBeanFeature1() {
        return helloBeanFeature1;
    }
}

 特征1 有构造函数,有getset方法

package com.glodon.springdemo1;

import java.util.List;

public class HelloBeanFeature1 {

    public HelloBeanFeature1(int num, String feature, List<String> list) {
        this.num = num;
        this.feature = feature;
        this.list = list;
    }

    private int num;
    private String feature;
    private List<String> list;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getFeature() {
        return feature;
    }

    public void setFeature(String feature) {
        this.feature = feature;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

}

 特征2 有构造函数,有getset方法

package com.glodon.springdemo1;

import java.util.List;

public class HelloBeanFeature2 {

    private String featureName;
    private String featureNum;
    private List<String> list;

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public HelloBeanFeature2() {
    }

    public String getFeatureName() {
        return featureName;
    }

    public void setFeatureName(String featureName) {
        this.featureName = featureName;
    }

    public String getFeatureNum() {
        return featureNum;
    }

    public void setFeatureNum(String featureNum) {
        this.featureNum = featureNum;
    }

    public HelloBeanFeature2(String featureName, String featureNum, List<String> list) {
        this.featureName = featureName;
        this.featureNum = featureNum;
        this.list = list;
    }
}

 applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

  <bean id="helloBean" class="com.glodon.springdemo1.HelloBean">
    <property name="helloBeanFeature1" ref="helloBeanFeature1"/>
  </bean>

  <bean id="helloBeanFeature1" class="com.glodon.springdemo1.HelloBeanFeature1">
    <constructor-arg name="feature" value="myfature"/>
    <constructor-arg name="num" value="12"/>
    <constructor-arg name="list">
      <list>
        <value>my</value>
        <value>name</value>
        <value>is</value>
        <value>zhangshuo</value>
      </list>
    </constructor-arg>
  </bean>

    <util:list id="featureList">
        <value>my</value>
        <value>feature</value>
        <value>zhangshuo</value>
    </util:list>

    <bean id="helloBeanFeature2" class="com.glodon.springdemo1.HelloBeanFeature2"
          p:featureName="zhangshuo" p:featureNum="26" p:list-ref="featureList"/>
</beans>

helloBean与helloBeanFeature1呈现了两种不同的注入方式,helloBean为getset注入,helloBeanFeature1为构造器注入。

构造器注入必须要求类具备相应的构造器。

helloBeanFeature2采用的也是getset注入,只是其使用的是命名空间的方式,命名空间是一种简单的写法。

P:表示property name =value; <util:>命名空间包含list set map等一堆元素,它可以被写作一个引用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值