首页 > 其他分享 >001.SpringIoc初体验

001.SpringIoc初体验

时间:2023-02-13 20:22:56浏览次数:44  
标签:初体验 apple String name 001 SpringIoc Child public Apple

1.创建entity(Apple、Child)

package com.imooc.spring.ioc.entity;

public class Apple
{
    private String title;
    private String color;
    private String origin;

    public Apple()
    {

    }

    public Apple(String title, String color, String origin)
    {
        this.title = title;
        this.color = color;
        this.origin = origin;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

    public String getOrigin()
    {
        return origin;
    }

    public void setOrigin(String origin)
    {
        this.origin = origin;
    }
}
package com.imooc.spring.ioc.entity;

public class Child
{
    private String name;
    private Apple apple;

    public Child()
    {
    }

    public Child(String name, Apple apple)
    {
        this.name = name;
        this.apple = apple;
    }

    public String getName()
    {
        return name;
    }

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

    public Apple getApple()
    {
        return apple;
    }

    public void setApple(Apple apple)
    {
        this.apple = apple;
    }

    public void eat()
    {
        System.out.println(name + "吃到了" + apple.getOrigin() + "种植的" + apple.getTitle());
    }
}

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

    <!--在Ioc容器启动时,自动由Spring实例化Apple对象,取名为sweetApple放到容器中-->
    <bean id="sweetApple" class="com.imooc.spring.ioc.entity.Apple">
        <property name="title" value="红富士"/>
        <property name="origin" value="欧洲"/>
        <property name="color" value="红色"/>
    </bean>

    <bean id="sourApple" class="com.imooc.spring.ioc.entity.Apple">
        <property name="title" value="青苹果"/>
        <property name="origin" value="中亚"/>
        <property name="color" value="绿色"/>
    </bean>

    <bean id="softApple" class="com.imooc.spring.ioc.entity.Apple">
        <property name="title" value="金帅"/>
        <property name="origin" value="中国"/>
        <property name="color" value="黄色"/>
    </bean>

    <bean id="rdApple" class="com.imooc.spring.ioc.entity.Apple">
        <property name="title" value="蛇果"/>
        <property name="origin" value="美国"/>
        <property name="color" value="红色"/>
    </bean>

    <bean id="lily" class="com.imooc.spring.ioc.entity.Child">
        <property name="name" value="莉莉"/>
        <property name="apple" ref="sweetApple"/>
    </bean>

    <bean id="andy" class="com.imooc.spring.ioc.entity.Child">
        <property name="name" value="安迪"/>
        <property name="apple" ref="rdApple"/>
    </bean>

    <bean id="luna" class="com.imooc.spring.ioc.entity.Child">
        <property name="name" value="露娜"/>
        <property name="apple" ref="softApple"/>
    </bean>

</beans>

3.在SpringApplication中测试

package com.imooc.spring.ioc;

import com.imooc.spring.ioc.entity.Apple;
import com.imooc.spring.ioc.entity.Child;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication
{
    public static void main(String[] args)
    {
        //创建SpringIoc容器,并根据配置文件在容器中实例化
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Apple sweetApple = context.getBean("sweetApple", Apple.class);
        System.out.println(sweetApple.getTitle());
        Child lily = context.getBean("lily", Child.class);
        lily.eat();
        Child andy = context.getBean("andy", Child.class);
        andy.eat();
        Child luna = context.getBean("luna", Child.class);
        luna.eat();
    }
}

4.测试结果

 

 

 

标签:初体验,apple,String,name,001,SpringIoc,Child,public,Apple
From: https://www.cnblogs.com/LLL0617/p/17117695.html

相关文章

  • chatGPT中文版-插件初体验
            话说当年上学那会校园流行的,是一个叫表表机器人(高校智能课程表)的软件,是我最早接触的智能对话,觉得非常有趣,常常一个人能上玩半个小时,后来随着智能......
  • sqlserver2019 服务启动失败 报10013
    排查:D:\ProgramFiles\MicrosoftSQLServer\MSSQL15.MSSQLSERVER\MSSQL\Log\ERRORLOG023-02-1310:57:05.22spid22sAself-generatedcertificatewassuccessfu......
  • SAP MM事务代码S_P00_07000139可以用于跟踪物料移动细节
    SAPMM事务代码S_P00_07000139可以用于跟踪物料移动细节SAP系统里有很多标准的报表可以查询指定时间段的期初期末库存,出库入库的数量金额等信息。除了比较常见的事务代码以......
  • 001.shell-每日练习一文件创建
    001.shell-每日练习一文件创建0x00.练习要求在/usr/local/uz654目录下,按照xxxx-xx-xx生成一个文件,如:2023-02-11.log把磁盘的使用情况写到这个文件中。删除该目录......
  • win10 0x0000011b共享打印机无法连接
    提供打印共享的win10由于升级后,其它需要使用打印机的windows电脑在连接共享的打印机时会有这个报错网上说在这台打印服务器上删除补丁即可,可以我没有找到,如:KB5005565补丁......
  • ThreeJS着色器初体验之平面波动效果
    起因搞了好久的cesium,好久没搞过THREE了,打算重拾一下THREE的内容,加深一下学习。效果平面波动+明亮颜色变化Three的着色器THREE写着色器效果分别是ShaderMaterial和Ra......
  • kx00016-顺序表--用C语言实现:多种方法合并2个非递减顺序表
    一、顺序表结构定义#defineINIT_SIZE10 //顺序表初始容量typedefvoid(myOpFunType)(void*); //定义操作函数类型typedefintseqType; //定义顺序表元素类型......
  • 001:C++基础知识
    1:***C++是一种高级语言,C++进一步扩充和完善了C语言,是一种面向对象的程序设计语言。  C++可运行于多种平台上,如Windows、MAC操作系统以及UNIX的各种版本。......
  • kx00015-顺序表--用C语言实现:删除顺序表中元素值等于x的所有元素
    一、顺序表结构定义#defineINIT_SIZE10 //顺序表初始容量typedefvoid(myOpFunType)(void*); //定义操作函数类型typedefintseqType; //定义顺序表元素类型......
  • kx00014-顺序表--查找x是否存在表中
    一、顺序表结构定义#defineINIT_SIZE10 //顺序表初始容量typedefvoid(myOpFunType)(void*); //定义操作函数类型typedefintseqType; //定义顺序表元素类型......