spring——通过xml文件配置IOC容器
创建相关的类(这里是直接在之前类的基础上进行修改)
package com.guan.dao; public interface Fruit {
String getFruit();
}
package com.guan.dao; public class FruitImpl implements Fruit {
public String getFruit() {
return "Buy Some fruit";
}
}
package com.guan.service; import com.guan.dao.Fruit; public interface UserService {
String buyFruit();
void setFruit(Fruit fruit);
}
package com.guan.service; import com.guan.dao.Apple;
import com.guan.dao.Fruit;
import com.guan.dao.FruitImpl; public class UserServiceImpl implements UserService{
Fruit fruit; public UserServiceImpl() {
fruit = new FruitImpl();
} public String buyFruit() {
return fruit.getFruit();
} public void setFruit(Fruit fruit){
this.fruit = fruit;
} }
在resources目录下添加配置文件:beans.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 id="fruit" class="com.guan.dao.FruitImpl"></bean>
<bean id="apple" class="com.guan.dao.Apple"></bean> <bean id="userService" class="com.guan.service.UserServiceImpl">
<property name="fruit" ref="fruit"></property>
</bean> </beans>
注:
(1).即便没有属性需要初始化也需要通过
<bean>
来对其进行实例化,而不再需要new(2).
<property>
的注入需要类中存在set方法(3).对于属性的赋值的两种方式
- 对于基本类型的赋值可以用value属性
- 对于对象类型可以用ref(reference)属性以及
<bean>
中的id初始化
创建并使用实例
import com.guan.dao.Fruit;
import com.guan.service.UserService;
import com.guan.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) {
//create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//retrieve configured instance
UserService userService = (UserService) context.getBean("userService");
System.out.println(userService.buyFruit());
}
}
其它相关配置
alias:给bean起别名,可以用多个不同的别名取出同一个类的实例
bean
(1).id:bean的唯一标识符
(2).class:bean对象所应的全限定名(包名+类名)
(3).name:也是别名,可以取同时取多个别名
import:用于团队开发使用,可以将多个配置文件导入合并为一个.那么在调用时只要导入一个配置文件即可
spring——通过xml文件配置IOC容器的更多相关文章
- Spring中xml文件配置也可以配置容器list、set、map
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- SSM框架中spring的XML文件配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- Spring的xml文件配置方式实现AOP
配置文件与注解方式的有很大不同,多了很多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"?> ...
- Spring(十二)使用Spring的xml文件配置方式实现AOP
配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"? & ...
- 重新学习之spring第一个程序,配置IOC容器
第一步:导入相关jar包(此范例导入的是spring3.2.4版本,spring2.5版本只需要导入spring核心包即可) 第二步:在项目的src下配置applicationContext.xml的 ...
- 【spring源码分析】IOC容器解析
参考: https://www.iteye.com/topic/1121913(自动注入bean的扫描器) https://m.imooc.com/mip/article/34150(循环依赖的解决方 ...
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- Spring整合Hibernate的XML文件配置,以及web.xml文件配置
利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...
- Spring 在xml文件中配置Bean
Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...
随机推荐
- HTTPStatus(状态码返回)详情
1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101 (切 ...
- 节点流和处理流(BufferedReader和BufferedWriter,BufferedInputStream和BufferedOutputStream,ObjectlnputStream和objectOutputStream)
一.基本介绍: 1.节点流可以从一个特定的数据源读写数据,如FileReader. FileWriter 如图:字节流是直接对数据源(文件,数组之类存放数据的地方)进行操作 2.处理流(也叫包装流)是 ...
- Eclipse、MyEclipse中代码提示框颜色
Matching breackets highlight = 匹配括号突出显示 Completion proposal background = 提示框背景色 Completion proposal ...
- python 定时任务apscheduler的使用
apscheduler 的使用 我们项目中总是避免不了要使用一些定时任务,比如说最近的项目,用户点击报名考试以后需要在考试日期临近的时候推送小程序消息提醒到客户微信上,翻了翻 fastapi 中的 ...
- MySQL 利用frm文件和ibd文件恢复表结构和表数据
文章目录 frm文件和ibd文件简介 frm文件恢复表结构 ibd文件恢复表数据 通过脚本利用ibd文件恢复数据 通过shell脚本导出mysql所有库的所有表的表结构 frm文件和ibd文件简介 在 ...
- 看我如何使用 shell 来获取所有 KVM 虚拟机的 IP 地址
文章目录 脚本说明 脚本展示 效果展示 此脚本的初衷是因为,KVM创建的桥接网卡的虚拟机,无法使用virsh domifaddr命令获取IP,而创建的nat网卡的虚拟机,则可以直接使用virsh do ...
- Java IO模型:BIO、NIO、AIO
Java IO模型:BIO.NIO.AIO 本来是打算直接学习网络框架Netty的,但是先补充了一下自己对Java 几种IO模型的学习和理解.分别是 BIO.NIO.AIO三种IO模型. IO模型的基 ...
- linux系统中实用shell脚本,请收藏!
1.Dos攻击防范(自动屏蔽攻击 IP) #!/bin/bashDATE=$(date +%d/%b/%Y:%H:%M)LOG_FILE=/usr/local/nginx/logs/demo2.acc ...
- 可视化BI工具选型,应该注意什么
伴随着大数据时代的到来,企业对数据的需求从"IT主导的报表模式"转向"业务主导的自助分析模式",可视化BI工具也随之应运而生.面对如此众多的可视化BI工具,我 ...
- C#淘汰的知识点
.NET 5+ 中已过时的功能 数组淘汰 .NET Framework 2以上的版本中,ArrayList可以说已经被淘汰了,应该用泛型类中的List<T> https://www.cnb ...