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文件中的配置 < ...
随机推荐
- Linux运行级别及解释
Linux中有七个运行级别,分别为0~7 0级:关机 1级:单用户(找回丢失密码) 2级:多用户无网络 3级:多用户有网络,最常用的级别 4级:保留,目前Linux暂无使用 5级:图像界面 6级:重启 ...
- 总结下Mac环境下按照appium
第10天休息 先来总结下Mac环境下按照appium 一.相关网站 官网: http://appium.io/ 测试论坛 https://testerhome.com/wiki 二.环境准备 从官 ...
- SQL代码规范
1. 建表规约 1) 表中字段名称 a) 表达是否概念的字段,必须使用is_xxx的方式命名,数据类型是bit b) 小数类型为decimal,禁止使用float和double. 说 ...
- Solution -「ARC 101E」「AT 4352」Ribbons on Tree
\(\mathcal{Description}\) Link. 给定一棵 \(n\) 个点的树,其中 \(2|n\),你需要把这些点两两配对,并把每对点间的路径染色.求使得所有边被染色的方案数 ...
- Solution -「CF 1132G」Greedy Subsequences
\(\mathcal{Description}\) Link. 定义 \(\{a\}\) 最长贪心严格上升子序列(LGIS) \(\{b\}\) 为满足以下两点的最长序列: \(\{b\}\) ...
- react 也就这么回事 01 —— React 元素的创建和渲染
React 是一个用于构建用户界面的 JavaScript 库 它包括两个库:react.js 和 react-dom.js react.js:React 的核心库,提供了 React.js 的核心功 ...
- Nginx 配置apple-app-site-association
ios突然给我发了如上链接和一个json,说他那边需要放一个 apple-app-site-association 文件用来支持他那边的功能,文件不需要后缀. 先说一下要求:线上官网的地址后面跟上他所 ...
- 【C#TAP 异步编程】异步接口 OOP
在我们深入研究"异步OOP"之前,让我们解决一个相当常见的问题:如何处理异步方法的继承?那么"异步接口"呢? 幸运的是,它确实可以很好地与继承(和接口)一起使用 ...
- window 消息传递机制【复杂版本】
一.消息概述 众人周知,window系统是一个消息驱动的系统, windows操作系统本身有自己的消息队列称做系统消息队列(操作系统队列),消息循环,它捕捉键盘,鼠标的动作生成消息,并将这个消 ...
- linux中docker容器安装vi命令详解
在使用docker容器时,同时你docker里的系统正好是debian或ubuntu的时候,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要 ...