spring 屬性注入

1、創(chuàng)建對象時,向類屬性里面設置值
2、java屬性注入的三種方式
  • set方法


    image.png
  • 有參構造


    image.png
  • 接口注入


    image.png
3、在spring中只支持兩種方式
(1)set方法注入

PropertyUser.java

package work.zhangdoudou.Property;

public class PropertyUser {
    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }   
}

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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- set方法注入屬性 -->
    <bean id="propertyUser" class="work.zhangdoudou.Property.PropertyUser">
        <!-- set方法注入屬性 -->
        <property name="username" value="lisi"></property>
    </bean>
</beans>

測試類TestPropertyUser.java

package work.zhangdoudou.test;

import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import work.zhangdoudou.Property.PropertyUser;

public class TestPropertyUser {

    @Test
    public void test() {
        //1加載配置文件,根據(jù)創(chuàng)建對象
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2得到創(chuàng)建的對象
        PropertyUser propertyUser=(PropertyUser) context.getBean("propertyUser");
        System.out.println(propertyUser.getUsername());
    }

}

運行結果


image.png
(2)有參數(shù)構造注入

PropertyUser1.java

package work.zhangdoudou.Property;

public class PropertyUser1 {
    private String username;

    public PropertyUser1(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }   
}

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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
    <!-- 有參數(shù)構造注入屬性 -->
    <bean id="propertyUser1" class="work.zhangdoudou.Property.PropertyUser1">
        <!-- 有參數(shù)構造注入 -->
        <constructor-arg name="username" value="zhangsan"></constructor-arg>
    </bean>
</beans>

測試類TestPropertyUser1.java

package work.zhangdoudou.test;

import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import work.zhangdoudou.Property.PropertyUser1;

public class TestPropertyUser1 {

    @Test
    public void test() {
        //1加載配置文件,根據(jù)創(chuàng)建對象
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2得到創(chuàng)建的對象
        PropertyUser1 propertyUser1=(PropertyUser1) context.getBean("propertyUser1");
        System.out.println(propertyUser1.getUsername());
    }

}

運行結果


image.png
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 1.1 spring IoC容器和beans的簡介 Spring 框架的最核心基礎的功能是IoC(控制反轉)容器,...
    simoscode閱讀 6,851評論 2 22
  • applicationContext.xml中的配置。 構造函數(shù)不同,輸出的name,car的順序也不同。 復雜類...
    Explorer_Mi閱讀 192評論 0 0
  • 注入方式 set方法注入(重點中的重點) 如果需要注入引用類型的對象,必須先將對象注入,然后用引用類型引用其nam...
    簡單coder閱讀 169評論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • 我們是光榮的加油員 通過嚴格的考核,我 成了一名光榮的中國石油加油員 雖然,沒有帽徽肩章 但是寶石花在胸前閃光 雖...
    姚國勝_小刺猬閱讀 559評論 1 4

友情鏈接更多精彩內(nèi)容