2.springIoc初体验

传统的对象创建和对象关联设置

复制代码
package com.spring.ioc;

import com.spring.ioc.entity.Apple;
import com.spring.ioc.entity.Child;

public class Application {
    public static void main(String[] args) {
        Apple apple1=new Apple("红富士","红色","欧洲");
        Apple apple2=new Apple("青苹果","绿色","中亚");
        Apple apple3=new Apple("金帅","黄色","中国");
        Child lily = new Child("lily",apple1);
        Child andy = new Child("andy",apple2);
        Child luna = new Child("luna",apple3);
        lily.eat();
        andy.eat();
        luna.eat();
    }
}
复制代码

 采用spring进行开发后,绝大多数的静态数据不用写在程序中,而采用配置的形式放在xml文件中,一但信息发生变化,不需要修改代码,直接修改配置文件就可以了

利用springIoc容器完成对象的创建以及关系

1.在pom.xml中添加依赖

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>spring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>

    <name>s01</name>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.7.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.17</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
            </plugin>
        </plugins>
    </build>
</project>
复制代码

2.在rescources中添加SpringIoc核心配置文件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.xsd">
    <!--在IoC容器启动时,自动由Spring实例化Apple对象,取名sweetApple放入到容器中-->
    <bean id="sweetApple" class="com.spring.ioc.entity.Apple">
        <property name="title" value="红富士"></property>
        <property name="origin" value="欧洲"></property>
        <property name="color" value="红色"></property>
    </bean>

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

    <bean id="softApple" class="com.spring.ioc.entity.Apple">
        <property name="title" value="金帅"></property>
        <property name="origin" value="中国"></property>
        <property name="color" value="黄色"></property>
    </bean>
    <!--在property标签中通过ref属性关联其他对象-->
    <bean id="lily" class="com.spring.ioc.entity.Child">
        <property name="name" value="lily"></property>
        <property name="apple" ref="sweetApple"></property>
    </bean>
    <bean id="andy" class="com.spring.ioc.entity.Child">
        <property name="name" value="andy"></property>
        <property name="apple" ref="sourApple"></property>
    </bean>
    <bean id="luna" class="com.spring.ioc.entity.Child">
        <property name="name" value="luna"></property>
        <property name="apple" ref="softApple"></property>
    </bean>
</beans>
复制代码

3.通过ClassPathXmlApplicationContext对象加载指定的XML文件初始化Ioc容器,getBean()方法获取bean对象

复制代码
package com.spring.ioc;

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

public class SpringApplication {
    public static void main(String[] args) {
        //加载指定的xml文件初始化Ioc容器
        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();
    }
}
复制代码

 

 在原始的代码中,对象的创建和对象关联的设置通过new关键字在编译时完成的,对象的创建以及依赖都是由程序主动发起的。当引入了springIoc容器后,所有对象的创建以及关系都被Ioc容器管理,这个过程是在程序运行时通过反射进行动态注入,这种方式更加灵活。

 

posted @   南风知君  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示