1.创建实体类
package com.imooc.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.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. pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.23</version> </dependency>
3. src/main/resources 下增加 applicationContext.xml(Spring的核心配置文件)
<?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用于实例化java对象-->
<!--id属性是标识单个 bean 定义的字符串。-->
<!--class属性定义 bean 的类型并使用完全限定的类名。-->
<!--property 是属性的意思-->
<bean id="sweetApple" class="com.imooc.entity.Apple">
<property name="title" value="红富士"/>
<property name="origin" value="欧洲"/>
<property name="color" value="红色"/>
</bean>
<bean id="sourApple" class="com.imooc.entity.Apple">
<property name="title" value="青苹果"/>
<property name="origin" value="中亚"/>
<property name="color" value="绿色"/>
</bean>
<bean id="softApple" class="com.imooc.entity.Apple">
<property name="title" value="金帅"/>
<property name="origin" value="中国"/>
<property name="color" value="黄色"/>
</bean>
<bean id="rdApple" class="com.imooc.entity.Apple">
<property name="title" value="蛇果"/>
<property name="origin" value="美国"/>
<property name="color" value="红色"/>
</bean>
<!--ref实现动态绑定-->
<!--用于指定该属性的值,用于指定的值是引用对象类型(即其他的Bean),ref后面的值为另一个Bean的id-->
<bean id="lily" class="com.imooc.entity.Child">
<property name="name" value="莉莉"/>
<property name="apple" ref="sweetApple"/>
</bean>
<bean id="andy" class="com.imooc.entity.Child">
<property name="name" value="安迪"/>
<property name="apple" ref="rdApple"/>
</bean>
<bean id="luna" class="com.imooc.entity.Child">
<property name="name" value="露娜"/>
<property name="apple" ref="softApple"/>
</bean>
</beans>
3. 测试类
package com.imooc;
import com.imooc.entity.Apple;
import com.imooc.entity.Child;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication
{
public static void main(String[] args)
{
/*
* 实例化一个容器
* 1.提供给构造函数的一个或多个位置路径ApplicationContext是资源字符串,允许容器从各种外部资源(例如本地文件系统、Java 等)加载配置元数据CLASSPATH。
* 2.格式:ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
* 3.创建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.property标签
name属性:用于指定属性的名称,与类中的set方法后方的名称一致 value属性:用于指定该属性的值,用于指定的值是基本类型、字符串类型 ref属性:用于指定该属性的值,用于指定的值是引用对象类型(即其他的Bean),ref后面的值为另一个Bean的id value标签:用于指定属性的值,类型为基本类型、字符串类型,值为标签内的文本内容,可以使用null值将属性的值设置为null ref标签:用于指定属性的值,类型为引用对象类型,值为其属性的值, 其属性有以下三种: local属性:用于指定依赖本地Bean实例,即同一XML文件中定义的Bean bean属性:用于指定依赖的Bean实例,可以是不同XML文件中的Bean parent属性:用于指定依赖的Bean实例,可以是当前BeanFactory或ApplicationContext的父BeanFactory或ApplicationContext中的Bean
标签:apple,String,Spring,Apple,003,案例,Child,public,name From: https://www.cnblogs.com/LLL0617/p/16910202.html