7.对象依赖注入
依赖注入是指运行时将容器内对象利用反射赋给其他对象的操作,依赖注入有两种形式 1.基于setter方法注入对象 2.基于构造方法注入对象。
基于setter方法注入对象包含两种使用场景
1.利用setter实现静态数值注入(value)
<?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">
<!--IoC容器自动利用反射机制运行时调用setXXX方法为属性赋值-->
<property name="title" value="红富士"></property>
<property name="color" value="红色"></property>
<property name="origin" value="欧洲"></property>
<property name="price" value="19.8"></property>
</bean>
</beans>
package com.spring.ioc;
import com.spring.ioc.entity.Apple;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Apple sweetApple = context.getBean("sweetApple", Apple.class);
System.out.println(sweetApple.getTitle());
}
}
//Apple的setTitle方法
/*public void setTitle(String title) {
System.out.println("title属性设置为:"+title);
this.title = title;
}*/
springIoC会将value中字符串自动转换成目标属性的类型,比如Intege
2.利用setter方法实现对象注入(ref)
<?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">
<!--IoC容器自动利用反射机制运行时调用setXXX方法为属性赋值-->
<property name="title" value="红富士"></property>
<property name="color" value="红色"></property>
<property name="origin" value="欧洲"></property>
<property name="price" value="19.8"></property>
</bean>
<bean id="lily" class="com.spring.ioc.entity.Child">
<property name="name" value="莉莉"></property>
<!--利用ref注入依赖对象-->
<property name="apple" ref="sweetApple"></property>
</bean>
</beans>
package com.spring.ioc;
import com.spring.ioc.entity.Apple;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Apple sweetApple = context.getBean("sweetApple", Apple.class);
//System.out.println(sweetApple.getTitle());
}
}
基于构造方法注入对象
<?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="sourApple" class="com.spring.ioc.entity.Apple">
<property name="title" value="青苹果"></property>
<property name="color" value="绿色"></property>
<property name="origin" value="中亚"></property>
<property name="price" value="9.8"></property>
</bean>
<bean id="andy" class="com.spring.ioc.entity.Child">
<constructor-arg name="name" value="安迪"></constructor-arg>
<constructor-arg name="apple" ref="sourApple"></constructor-arg>
</bean>
</beans>
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) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Child andy = context.getBean("andy", Child.class);
andy.eat();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!