关于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 ...
随机推荐
- Python之进程
进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体:在当代 ...
- 通过java api统计hive库下的所有表的文件个数、文件大小
更新hadoop fs 命令实现: [ss@db csv]$ hadoop fs -count /my_rc/my_hive_db/* 18/01/14 15:40:19 INFO hdfs.Peer ...
- MVC系列 引入MVC
1.必须的类库 system.web.Mvc system.Web.Razor system.web.webPages system.web.webpages.razor 添加方式如下图 2.MVC项 ...
- 初探java对象比较
判断两个对象的属性值是否相等的方法, class Book{ private String title; private double price; public Book(String title, ...
- [LeetCode] Merge Two Binary Trees 合并二叉树
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- [LeetCode] Super Washing Machines 超级洗衣机
You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...
- 在Unity3D中利用 RenderTexture 实现游戏内截图
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; publ ...
- Java面试题—初级(7)
作为一枚Java后端开发者,数据库知识必不可少,对数据库的掌握熟悉度的考察也是对这个人是否有扎实基本功的考察.特别对于初级开发者,面试可能不会去问框架相关知识,但是绝对不会不去考察数据库知识,这里收集 ...
- python 装饰器统计某个函数的运行时间
import datetime def count_time(func): def int_time(*args, **kwargs): start_time = datetime.datetime. ...
- [python]使用django快速生成自己的博客小站,含详细部署方法
前言 人生苦短,我用python 这是之前经常听到的一句笑谈.因为新公司很多业务是用的python语言,所以这几天也一直在学习python的一些东西. 作为一个之前一直java后端的开发人员,对比ja ...