spring 属性注入

1、创建对象时,向类属性里面设置值
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加载配置文件,根据创建对象
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2得到创建的对象
        PropertyUser propertyUser=(PropertyUser) context.getBean("propertyUser");
        System.out.println(propertyUser.getUsername());
    }

}

运行结果


image.png
(2)有参数构造注入

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"> 
    <!-- 有参数构造注入属性 -->
    <bean id="propertyUser1" class="work.zhangdoudou.Property.PropertyUser1">
        <!-- 有参数构造注入 -->
        <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加载配置文件,根据创建对象
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2得到创建的对象
        PropertyUser1 propertyUser1=(PropertyUser1) context.getBean("propertyUser1");
        System.out.println(propertyUser1.getUsername());
    }

}

运行结果


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 11,705评论 2 22
  • applicationContext.xml中的配置。 构造函数不同,输出的name,car的顺序也不同。 复杂类...
    Explorer_Mi阅读 1,400评论 0 0
  • 注入方式 set方法注入(重点中的重点) 如果需要注入引用类型的对象,必须先将对象注入,然后用引用类型引用其nam...
    简单coder阅读 1,261评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,801评论 19 139
  • 我们是光荣的加油员 通过严格的考核,我 成了一名光荣的中国石油加油员 虽然,没有帽徽肩章 但是宝石花在胸前闪光 虽...
    姚国胜_小刺猬阅读 3,408评论 1 4

友情链接更多精彩内容