【spring揭秘】1、关于IOC的基础概念
1、基础概念
IOC有三种注入方式:
1、构造方法注入,就是通过构造方法进行实例化成员属性对象,优点是实现对象之后直接就可以使用,但是参数过多也是个麻烦
2、setter方法注入,实现相应的setter方法,外界就可以使用set方法进行注入,这个比较方便,缺点是创建对象之后不能马上使用
3、接口注入,实现指定接口中的方法进行注入(不提倡)
2、配置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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="cn.cutter" /> </beans>
测试代码:
package spring; import java.io.File;
import java.io.IOException;
import java.net.URL; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.cutter.start.provider.FXNewsProvider; public class SpringTest { @Test
public void test1() throws IOException {
String path = FXNewsProvider.class.getResource("").getPath();
File f = new File("E:/Github/bishi/java/project/cutter-point-spring Maven Webapp/src/main/resources/application-bean.xml");
String path2 = f.getCanonicalPath();
URL path3 = FXNewsProvider.class.getResource(""); System.out.println(path);
System.out.println(path2);
System.out.println(path3); ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application-bean.xml");
FXNewsProvider fxNewsProvider = (FXNewsProvider) ctx.getBean("FXNewsProvider");
if(fxNewsProvider != null) {
System.out.println("ok");
}
} }
结果展示:


【spring揭秘】1、关于IOC的基础概念的更多相关文章
- spring揭秘读书笔记----ioc的基本概念
在看ico概念之前,先想一下我们平常需要依赖某个类时会怎么做? 无非是在要用到的地方写如下代码: Person person = new Person(); //然后就可以用person对象来获取Pe ...
- spring揭秘 读书笔记 一 IoC初探
本文是王福强所著<<spring揭秘>>一书的读书笔记 ioc的基本概念 一个例子 我们看下面这个类,getAndPersistNews方法干了四件事 1 通过newsList ...
- Spring Ioc源码分析系列--Ioc的基础知识准备
Spring Ioc源码分析系列--Ioc的基础知识准备 本系列文章代码基于Spring Framework 5.2.x Ioc的概念 在Spring里,Ioc的定义为The IoC Containe ...
- Spring学习笔记一:基础概念
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774310.html 一:Spring是什么 Spring的主要作用是作为对象的容器. 传统编程中,我们 ...
- Spring Cloud Security OAuth2.0 认证授权系列(一) 基础概念
世界上最快的捷径,就是脚踏实地,本文已收录[架构技术专栏]关注这个喜欢分享的地方. 前序 最近想搞下基于Spring Cloud的认证授权平台,总体想法是可以对服务间授权,想做一个基于Agent 的无 ...
- Spring.Net---2、IoC/DI基本概念
---------------------------------------------------------------------------------- (1)IoC/DI的概念 IoC ...
- spring源码浅析——IOC
=========================================== 原文链接: spring源码浅析--IOC 转载请注明出处! ======================= ...
- Spring MVC -- Spring框架入门(IoC和DI)
Spring MVC是Spring框架中用于Web应用开发的一个模块.Spring MVC的MVC是Model-View-Controller的缩写.它是一个广泛应用于图像化用户交互开发中的设计模式, ...
- Spring升级案例之IOC介绍和依赖注入
Spring升级案例之IOC介绍和依赖注入 一.IOC的概念和作用 1.什么是IOC 控制反转(Inversion of Control, IoC)是一种设计思想,在Java中就是将设计好的对象交给容 ...
随机推荐
- 2018.11.17 hdu5829Rikka with Subset(ntt)
传送门 nttnttntt基础题. 考虑计算每一个数在排名为kkk时被统计了多少次来更新答案. 这样的话,设anskans_kansk表示所有数的值乘上排名为kkk的子集数的总和. 则ansk=∑i ...
- 2018.11.02 洛谷P3952 时间复杂度(模拟)
传送门 惊叹考场dubuffdubuffdubuff. 这题还没有梭哈难啊233. 直接按照题意模拟就行了. 代码: #include<bits/stdc++.h> using names ...
- 指令发布中如何实现new新消息的提醒?
设计思路:反馈后,最急需了解反馈结果的是申请人,故给每一条反馈信息添加一个查看状态的字段,如CK_STATUS,并为这个状态设计为char(1)类型,java bean中使用integer可以实现默认 ...
- centos firewalld 基本操作【转】
1.firewalld的基本使用启动: systemctl start firewalld关闭: systemctl stop firewalld查看状态: systemctl status fire ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- s4-介质访问控制子层-1 MAC子层
数据链路层被分成了两个子层:MAC和LLC MAC子层要解决什么问题? 介质访问控制(Madia Access Control) 数据通信方式 单播(unicast):One - to - One ...
- Spring boot 集成Dubbo简单版,准备工作,
一.GitHub上找寻Dubbo资源 阿里巴巴在其GitHub上已经写好一个Github案例所以我们只要进入其Git上就可以看到和clone这个项目 二.阿里巴巴GitHub使用 https://gi ...
- HTML5与相关类的扩充
1.getElementsByclassName()方法 <body> <div class='a1'>klkx1</div> <ul id='ul1'> ...
- ASP.NET 压缩输出的HTML字符
重写Render using System; using System.Collections.Generic; using System.Text; using System.Web.UI; usi ...
- myeclipse安装maven
开始开门见山了,至于maven是什么请百度百科. 第一步: 下载maven,进入maven官网下载:http://maven.apache.org/download.cgi 第二步: 1.解压下载好的 ...