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以及注解的简单介绍的更多相关文章

  1. spring的IOC,DI及案例详解

    一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...

  2. Spring的Ioc与DI

    一.前言 Spring框架的核心基于控制反转的原理. IoC是一种将组件依赖关系的创建和管理外部化的技术. 考虑一个示例,其中Foo类依赖于Bar类的实例来执行某种处理. 传统上,Foo使用new运算 ...

  3. 对Spring中IOC和DI的理解

    前几篇讲了Spring中IOC和DI的用法,本篇应该放到三篇之前,但一直没有想到好的讲解方式,后参考https://blog.csdn.net/luoyepiaoxue2014/article/det ...

  4. 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 ...

  5. 关于Spring的IOC和DI

    原始调用模型 Spring的演化过程 Spring的调用过程 ======================================= IoC[理解][应用][重点] 1.IoC(Inversi ...

  6. 转载百度百科上的强回复,关于spring的IOC和DI

    IoC与DI   首先想说说IoC(Inversion of Control,控制倒转).这是spring的核心,贯穿始终.所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命 ...

  7. Java 反射和内省实现spring的IOC和DI

    1.构造两个JavaBean package com.spring.model; public class People { private Car car; public Car getCar() ...

  8. 总结一下 Spring的IOC、DI

    国庆节刚过,应一些朋友的提问,总结一下Spring中IOC也即DI的通俗理解. 网友wm5920解释: IOC控制反转:说的是创建对象实例的控制权从代码控制剥离到IOC容器控制,实际就是你在xml文件 ...

  9. Spring:Ioc和DI

    一.摘要         本文为作者搜集的Spring关于IoC/DI相关知识的记录整理笔记.介绍了IoC(控制反转)是一种设计原则,用于降低代码的耦合度.介绍了IoC是通过BeanDefinitio ...

随机推荐

  1. Python模块 - paramiko

    paramiko模块提供了ssh及sft进行远程登录服务器执行命令和上传下载文件的功能.这是一个第三方的软件包,使用之前需要安装. 1 基于用户名和密码的 sshclient 方式登录 # 建立一个s ...

  2. python2 当中 遇到 UnicodeDecodeError UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 37: ordinal not in range(128)

    使用python2 总是遇到 UnicodeDecodeErrorUnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in positio ...

  3. python——模块与包2

    模块与包2 1 什么是包 包是一种通过使用.'模块名'来组织python模块名称空间的方式. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都 ...

  4. 判断字符串的后缀.endswith()

    可以用str.endswith('.jpg')来判断字符串是否以jpg结尾,返回True或者False

  5. Spring学习(1)——快速入门

    认识 Spring 框架 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control,控制反转) 和 AOP( ...

  6. day3 自定义指令详解

    在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足 ...

  7. 【DataMagic】如何在万亿级别规模的数据量上使用Spark

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文首发在云+社区,未经许可,不得转载. 作者:张国鹏 | 腾讯 运营开发工程师 一.前言 Spark作为大数据计算引擎,凭借其快速.稳定. ...

  8. [LeetCode] Prefix and Suffix Search 前后缀搜索

    Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...

  9. [LeetCode] My Calendar III 我的日历之三

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...

  10. css 的一些知识点的整理

    css的一些标签整理   background-attachment: scroll;背景图可滚动 background-attachment: fixed; 固定背景的位置,不随着滚动条移动而移动 ...