【学习笔记】 使用XML配置和注解实现Spring的依赖注入DI (2-3-2)
Spring的四个核心组件
1.beans
Bean是包装应用程序自定义对象Object的 Object中保存数据
2.core
3.context
一个Bean的关系集合
4.expression
ApplicationContext ctx=new ClasspathXmlApplicationContext("applicationContext.xml");
//容器一旦生成 所有bean都被加载到内存中
Printer service=(Printer)ctx.getBean("printer");
1.Spring容器创建的时候会将所有配置的Bean创建对象,默认bean都是单例的
BeanFactory和ApplicationContext
2.代码通过个体Bean()方法从容器获取指定的bean实例,容器会首先调用Bean类的无参构造器创建实例对象
3.bean的作用域
scope="prototype"
原型模式:真正使用的时候才会创建 每获取一次都会创建不同对象
scope="singleton"
单例模式:容器初始化时需要使用name建 每次获取的都是同一个对象,默认值
<bean id="stu1" class="xx.xx.Student" scope="prototype" ></bean>
4.bean的生命周期(见另一篇博客)
5.bean的id和name属性
当bean中含有特殊字符时需要使用name属性
6.基于xml的DI(Dependency Injection)
Spring 提供了多种依赖注入的手段
除了通过属性的setter访问器
还可以通过带参构造方法实现依赖注入
注入类型
1.设值注入
<bean id="stu1" class="xx.xx.Student" >
<property name="name" value="斯沃"/>
<property name="age" value="18"/>
</bean>
2.构造注入
<bean id="stu1" class="xx.xx.Student" >
<constructor-arg index="0" value="斯沃"></constructor-arg>
<constructor-arg index="1" value="18"></constructor-arg>
</bean>
public Student(String name, int age){
this.name=name;
this.age=age;
}
域属性注入(Java Bean属性)
<bean id=mcar" class="xx.xx.Car" >
<property name="color" value="红色"/>
</bean> <bean id="stu1" class="xx.xx.Student" >
<property name="name" value="斯沃"/>
<property name="age" value="18"/>
<property name="car" ref="mcar"/>
</bean>
真正开发时推荐使用设值注入 因为要使用构造注入 Bean必须写出所有的构造
3.命名空间注入
xmlns:p="http://www.springframework.org/schema/p" <bean id="stu1" class="xx.xx.Student" p:name="测试" p:age="18" p:car-ref="mcar">
</bean>
//命名空间p注入 p:xxx property
//同样 有命名空间 c注入
*4.集合属性注入
public class CollectionBean{
private List<String> list;
}
1.数组array
<property name="names">
<array>
<value>a</value>
<value>b</value>
</array>
</property>
2.List
<bean id="List" class="xx.xx.CollectionBean">
<property name="list">
<list>
<value>sword</value>
<value>fizz</value>
</list>
</property>
</bean>
3.Set
4.Map
<bean id="Map" class="xx.xx.CollectionBean">
<property name="map">
<map>
<entry key="a">
<value>shit</value>
</entry>
</map>
</property>
</bean>
Properties
7.基于注解的依赖注入
1.引入 spring-aop-4.2.0.REKEASE.jar
2.添加xsd约束
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
3.配置包扫描器
配置了包扫描器 该包下以及子包中的类才可以被Spring扫描
去寻找被注解的类和属性,让Spring容器管理赋值
component:组件
scan:
<context:component-scan base-package="sword.po"></context:component-scan>
//标识这个类
@Compoent("info")
public class User{
//普通属性
@Value("斯沃")
private String name;
@Value("18")
private int age;
//域属性
@Resource
private Car car;
} //@Autowired
@Compoent("car")
public class Car{
@Value("red")
private String color;
}
等价
@Compoent("car") //不分层
@Repository("info") //dao
@Service //biz
@Controller //Action
【学习笔记】 使用XML配置和注解实现Spring的依赖注入DI (2-3-2)的更多相关文章
- ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则
ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...
- go微服务框架kratos学习笔记五(kratos 配置中心 paladin config sdk [断剑重铸之日,骑士归来之时])
目录 go微服务框架kratos学习笔记五(kratos 配置中心 paladin config sdk [断剑重铸之日,骑士归来之时]) 静态配置 flag注入 在线热加载配置 远程配置中心 go微 ...
- Qt5学习笔记(1)-环境配置(win+64bit+VS2013)
Qt5学习笔记(1)-环境配置 工欲善其事必先-不装-所以装软件 久不露面,赶紧打下酱油. 下载 地址:http://download.qt.io/ 这个小网页就可以下载到跟Qt有关的几乎所有大部分东 ...
- mybatis 学习笔记(四):mybatis 和 spring 的整合
mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...
- Spring IOC 依赖注入的两种方式XML和注解
依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...
- Spring框架学习笔记(1)——控制反转IOC与依赖注入DI
Spring框架的主要作用,就是提供了一个容器,使用该容器就可以创建并管理对象.比如说Dao类等,又或者是具有多依赖关系的类(Student类中包含有Teacher类的成员变量) Spring有两个核 ...
- 框架源码系列九:依赖注入DI、三种Bean配置方式的注册和实例化过程
一.依赖注入DI 学习目标1)搞清楚构造参数依赖注入的过程及类2)搞清楚注解方式的属性依赖注入在哪里完成的.学习思路1)思考我们手写时是如何做的2)读 spring 源码对比看它的实现3)Spring ...
- Spring.NET依赖注入框架学习--简单对象注入
Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...
- 07 Spring框架 依赖注入(四)基于注解的依赖注入
前面几节我们都在使用xml进行依赖的注入,但是在实际的开发中我们往往偏爱于使用注解进行依赖注入,因为这样更符合我们人的思维,并且更加快捷,本节就来讲述Spring基于注解的依赖注入: 信息注入注解 @ ...
随机推荐
- Jade报错:Invalid indentation,you can use tabs or spaces but not both问题
现象:通过html生成jade文件之后,更改jade文件时,语句没什么问题的情况下,jade文件编译不通过,报错:Invalid indentation,you can use tabs or spa ...
- 2018/1/27 每日一学 最长不降序子序列的O(n*logn)算法
手动维护一个数组模拟即可,233-- 可以使用algorithm中的lower_bound(相当于二分) 代码如下: #include<cstdio> #include<algori ...
- Javascript学习--时间
digit = [ [ [0,0,1,1,1,0,0], [0,1,1,0,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1, ...
- linux_nginx环境配置
rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm # 安装阿里的epel源 国内使用web站点最多的w ...
- Windows挂钩注入DLL
注入DLL实现源码:HINSTANCE g_hInstDll = NULL; HHOOK g_hHook = NULL; DWORD g_dwThreadId = 0; #ifdef _MANAGED ...
- Linux指令--chown
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- linkin大话设计模式--建造模式
linkin大话设计模式--建造模式 建造模式是对象的创建模式,可以讲一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 建造模式的结构: 抽象建造者 ...
- java中可变长参数的定义及使用方法
JAVA中可以为方法定义可变长参数( Varargs)来匹配不确定数量的多个参数,其定义用“...”表示.其实,这类似于为方法传了一个数组,且在使用方法上也和数组相同,如下: public void ...
- nagios中监测dns 227.7.128.68的网络状态
[root@nhserver2 ~]# cd /usr/local/nagios/etc/objects [root@nhserver2 objects]# vim hosts_dns.cfgdefi ...
- php之冒泡排序
<?php//冒泡排序function shell_sort($arr){for($i=0;$i<count($arr)-1;$i++){for($j=0; $j< count($a ...