二、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.0modelVersion>
<groupId>comgroupId>
<artifactId>springartifactId>
<version>1.0-SNAPSHOTversion>
<repositories>
<repository>
<id>aliyunid>
<name>aliyunname>
<url>https://maven.aliyun.com/repository/publicurl>
repository>
repositories>
<name>s01name>
<packaging>warpackaging>
<properties>
<maven.compiler.target>1.8maven.compiler.target>
<maven.compiler.source>1.8maven.compiler.source>
<junit.version>5.7.1junit.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.3.17version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>3.3.1version>
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">
<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>
<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容器管理,这个过程是在程序运行时通过反射进行动态注入,这种方式更加灵活。