spring的数据装配01基本数据类型

数据的装配

为bean的属性注入值,称为数据的装配,可装配不同类型的值

简单类型(共19中)

八种基本类型 使用value注入值

byte,short,int,long,float,double,char,boolean

String Class Resource
先添加jar包

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!--spring-core-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <!--spring-bean-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <!--spring-context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!--spring-expression-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
        </dependency>
    </dependencies>

第二步创建配置文件spring.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">
  
</beans>

第三步书写一个类Hello

package com.ioc05.entity;

/**
 * package_name:com.ioc05.entity
 * Author:徐亚远
 * Date:2020/1/19 18:44
 * 项目名:springDemo01
 * Desription:
 **/
public class Hello {
    private Integer age;
    private Byte sex;
    private String username;

    @Override
    public String toString() {
        return "Hello{" +
                "age=" + age +
                ", sex=" + sex +
                ", username='" + username + '\'' +
                ", price=" + price +
                ", aClass=" + aClass +
                '}';
    }

    private Double price;
    private Class aClass;

    public Integer getAge() {
        return age;
    }

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

    public Byte getSex() {
        return sex;
    }

    public void setSex(Byte sex) {
        this.sex = sex;
    }

    public String getUsername() {
        return username;
    }

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

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Class getaClass() {
        return aClass;
    }

    public void setaClass(Class aClass) {
        this.aClass = aClass;
    }
}

第四步 配置spring.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">
   <bean id="hello" class="com.ioc05.entity.Hello">
       <property name="age" value="21"/>
       <property name="price" value="1.23"/>
       <property name="sex" value="4"/>
       <property name="username" value="admin"/>
       <property name="aClass" value="java.lang.String"/>
   </bean>
</beans>

第五步书写测试方法HelloController

package com.ioc05.controller;

import com.ioc05.entity.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * package_name:com.ioc05.controller
 * Author:徐亚远
 * Date:2020/1/19 18:47
 * 项目名:springDemo01
 * Desription:
 **/
public class HelloController {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("ioc05/spring.xml");
        Hello helloOne = (Hello) ac.getBean("hello");
        System.out.println("hello: "+helloOne);
    }
}

执行结果如图
在这里插入图片描述
目录结构
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值