关于spring的IOC和DI的xml以及注解的简单介绍
xml
一 目的:通过ApplicationContext对象的getBean方法获取所需类的对象.
编写一个service类
public class service {
private String name;public void add(){
System.out.println("add ....");
}
}
编写applicationContext.xml文件
<bean id="service" class="spring_xml_aop_start.service" />
编写测试类
public class test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//入门用,后期监听器加载
service us=(service) context.getBean("service");
us.add();
}
}
测试类运行结果

二 目的基于目的一进行简单属性注入
属性注入有两种形式:构造器注入和set方法注入
构造器注入:applicationContext.xml中的对应<bean>标签下<constructor-arg name="类中成员变量名" value="属性赋值"></constructor-arg>
类需要提供有参构造
简单值注入
在xml中的相应bean中配置constructor-arg时 类中有参构造中参数有几个就要配几个
可以直接value=""值 这个排序是根据类的初始化排序(有参下面this.xx=xx的顺序)
也可以通过index="0/1/2"或者type="类型" 进行手动排序
也可以 name="类成员名" value=""进行配置
引用另一个类的复杂注入
在类中 private dao dao;并在有参构造 this.dao=dao;
set注入:<property name="类成员名" value="属性赋值"></property>
只需在类中给成员变量提供set方法即可
service类
public class service {
private String name;
private dao dao;
public service(String name,dao dao) {
super();
this.name = name;
this.dao = dao;
}
public void add(){
dao.daotest();
System.out.println("add ...."+name);
}
}
dao类
public class dao {
private String name;
public void setDao(String dao) {
this.name = dao;
}
public void daotest(){
System.out.println("复杂注入"+name);
}
}
配置文件
<bean id="dao" class="spring_xml_aop_start.dao">
<property name="dao" value="我是dao"></property>
</bean>
<bean id="service" class="spring_xml_aop_start.service">
<constructor-arg name="name" value="我是service"></constructor-arg>
<constructor-arg name="dao" ref="dao"></constructor-arg>
</bean>
测试类同上
运行结果

注解
首先是在applicationContext.xml中配置
<context:component-scan base-package="基包" /> //扫描的基包
在然后把类交个spring管理有他们4个都可以("名称") 相当于xml中的<bean id=""或者name="">
@Component:组件 ---所有类都能用 @Controller :修饰web层类 @Service :修饰业务层类 @Repository :修饰持久层类
属性注入 :
普通注入:@Value("值")
对象类型属性: @Resource(name="上面类注解的名字")可以不用谢
@Autowired 按类型注解结合@Qualifier按名称注入
测试类:在类名前加上下面两段注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
声明需要测试的类
@Resource
private Service service;
测试方法加@Test注解ctrl+1导包
!!注解注意事项:
使用注解初始化类的时候,类中千万不要有有参构造,不然会被报出累没有初始化的异常!
因为spring 在初始化bean的时候要先调用构造函数,再set其它属性.
service类
@Service("service")
public class service {
@Value("service")
private String name;
@Resource(name="daoss")
private dao dao;
//这里之前xml配置时调用构造器进行注入属性的,注解操作时要去掉
/*public service(String name, spring_anno_start.dao dao) {
super();
this.name = name;
this.dao = dao;
}*/
public void add(){
dao.daotest();
System.out.println("add ...."+name);
}
}
dao类
@Repository("daoss")
public class dao {
@Value("dao")
private String name;
public void daotest(){
System.out.println("复杂注入"+name);
}
}
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class test {
@Resource
private service service; @Test
public void test1(){
service.add();
}
}
applicationContext.xml
<context:component-scan base-package="spring_anno_start"></context:component-scan>
运行结果

关于spring的IOC和DI的xml以及注解的简单介绍的更多相关文章
- spring的IOC,DI及案例详解
一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...
- Spring的Ioc与DI
一.前言 Spring框架的核心基于控制反转的原理. IoC是一种将组件依赖关系的创建和管理外部化的技术. 考虑一个示例,其中Foo类依赖于Bar类的实例来执行某种处理. 传统上,Foo使用new运算 ...
- 对Spring中IOC和DI的理解
前几篇讲了Spring中IOC和DI的用法,本篇应该放到三篇之前,但一直没有想到好的讲解方式,后参考https://blog.csdn.net/luoyepiaoxue2014/article/det ...
- 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...
- 关于Spring的IOC和DI
原始调用模型 Spring的演化过程 Spring的调用过程 ======================================= IoC[理解][应用][重点] 1.IoC(Inversi ...
- 转载百度百科上的强回复,关于spring的IOC和DI
IoC与DI 首先想说说IoC(Inversion of Control,控制倒转).这是spring的核心,贯穿始终.所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命 ...
- Java 反射和内省实现spring的IOC和DI
1.构造两个JavaBean package com.spring.model; public class People { private Car car; public Car getCar() ...
- 总结一下 Spring的IOC、DI
国庆节刚过,应一些朋友的提问,总结一下Spring中IOC也即DI的通俗理解. 网友wm5920解释: IOC控制反转:说的是创建对象实例的控制权从代码控制剥离到IOC容器控制,实际就是你在xml文件 ...
- Spring:Ioc和DI
一.摘要 本文为作者搜集的Spring关于IoC/DI相关知识的记录整理笔记.介绍了IoC(控制反转)是一种设计原则,用于降低代码的耦合度.介绍了IoC是通过BeanDefinitio ...
随机推荐
- 【webGL入门2】点线面的绘制
用js绘制webGL的点: THREE.Vector3 = function ( x, y, z ) { //用THREE声明的变量都是全局变量.this.x = x || 0;this.y = y ...
- Web 项目报错No suitable driver found for jdbc:mysql://localhost:3306/book 的一个解决办法
确认jar包加入到了build path中,然后注意版本是否与数据库相配,还要留意将jar包放入WEB-INF下的lib文件夹中
- Vue框架
Vue框架 环境: windows python3.6.2 Vue的cdn: <script src="https://cdn.jsdelivr.net/npm/vue"&g ...
- java集合 源码解析 学习手册
学习路线: http://www.cnblogs.com/skywang12345/ 总结 1 总体框架 2 Collection架构 3 ArrayList详细介绍(源码解析)和使用示例 4 fai ...
- Chrome浏览器vue-devtools插件安装教程
1.打开https://github.com/vuejs/vue-devtools,cmd方式直接输入:git Clone https://github.com/vuejs/vue-devtools. ...
- 阿里云、腾讯云开通端口 telnet不通的原因
1.安全组是否已经开通相对应的端口: 阿里云:https://help.aliyun.com/document_detail/25471.html 腾讯云:http://bbs.qcloud.com/ ...
- C#在使用Assembly加载程序集时失败
错误现象: 进行插件读取时出现错误:"尝试从一个网络位置加载程序集,在早期版本的 .NET Framework 中,这会导致对该程序集进行沙盒处理.此发行版的 .NET Framework ...
- [LeetCode] Relative Ranks 相对排名
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 机器学习基石:09 Linear Regression
线性回归假设: 代价函数------均方误差: 最小化样本内代价函数: 只有满秩方阵才有逆矩阵. 线性回归算法流程: 线性回归算法是隐式迭代的. 线性回归算法泛化可能的保证: 根据矩阵的迹的性质:tr ...
- Oracle RAC环境下定位并杀掉最终阻塞的会话-续
之前在<Oracle RAC环境下定位并杀掉最终阻塞的会话>中,最终使用一个SQL查询出RAC实例之间的所有阻塞关系.但是实际在某些极端的生产环境,是不允许执行复杂的SQL语句,即使允许执 ...