1.实例化bean的三种方式
2.初始化Ioc容器
2.1 利用构造方法参数名实例化
2.2 利用构造方法参数位置实例化
2.3 实现步骤
2.3.1 src/main/java/com/imooc/spring/ioc/entity/Apple.java
package com.imooc.spring.ioc.entity; public class Apple { private String title; private String color; private String origin; public Apple() { //如果bean中不写属性,默认调用构造方法 System.out.println("Apple对象已创建," + this); } public Apple(String title, String color, String origin) { System.out.println("通过带参构造方法创建对象," + this); 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; } }
2.3.3 src/main/resources/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"> <!--bean标签默认通过构造方法构造对象--> <!--bean标签的作用是用于通知IOC容器实例化那个对象--> <!--class代表从那个类实例化--> <!--id代表标识--> <bean id="apple1" class="com.imooc.spring.ioc.entity.Apple"> </bean> <bean id="apple2" class="com.imooc.spring.ioc.entity.Apple"> <constructor-arg name="title" value="红富士"/> <constructor-arg name="color" value="红色"/> <constructor-arg name="origin" value="欧洲"/> </bean> </beans>
2.3.4 src/main/java/com/imooc/spring/ioc/SpringApplication.java
package com.imooc.spring.ioc; 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"); } }
2.3.5 作为在构造方法中的参数并不强制要求为String类型,可以为数字类型
package com.imooc.spring.ioc.entity; public class Apple { private String title; private String color; private String origin; private Float price; public Apple() { //如果bean中不写属性,默认调用构造方法 System.out.println("Apple对象已创建," + this); } public Apple(String title, String color, String origin) { System.out.println("通过带参构造方法创建对象," + this); this.title = title; this.color = color; this.origin = origin; } public Apple(String title, String color, String origin, Float price) { System.out.println("通过带参构造方法创建对象," + this); this.title = title; this.color = color; this.origin = origin; this.price = price; } 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; } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } }
2.3.6 src/main/resources/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"> <!--bean标签默认通过构造方法构造对象--> <!--bean标签的作用是用于通知IOC容器实例化那个对象--> <!--class代表从那个类实例化--> <!--id代表标识--> <bean id="apple1" class="com.imooc.spring.ioc.entity.Apple"> </bean> <bean id="apple2" class="com.imooc.spring.ioc.entity.Apple"> <constructor-arg name="title" value="红富士"/> <constructor-arg name="color" value="红色"/> <constructor-arg name="origin" value="欧洲"/> <constructor-arg name="price" value="11.98"/> </bean> <bean id="apple3" class="com.imooc.spring.ioc.entity.Apple"> <constructor-arg index="0" value="红富士"/> <constructor-arg index="1" value="红色"/> <constructor-arg index="2" value="欧洲"/> <constructor-arg index="3" value="11.98"/> </bean> </beans>