spring学习笔记(四)

IOC DI区别

1.IOC:控制反转,把对象创建交给spring进行配置

2.DI:依赖注入,向类里面的属性中设置值

3.关系:依赖注入不能单独存在,需要在ioc基础之上完成操作

 spring学习笔记(四)

 spring学习笔记(四)spring学习笔记(四)

 

 spring学习笔记(四)

重点 重点 重点

Spring bean管理(注解)

注解

1 代码里面特殊标记,使用注解可以完成功能

2 注解写法@注解名称(属性名称=属性值)

3 注解使用在类上面,方法上面,和属性上面

Spring 注解开发准备

 spring学习笔记(四)spring学习笔记(四)

 spring学习笔记(四)spring学习笔记(四)

2 创建类,创建方法

3 创建spring配置文件,引入约束

(1)第一天做ioc基本功能,引入约束beans

(2)springioc注解开发,引入新的约束

<?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"><!-- bean definitions here -->

</beans>

 spring学习笔记(四)spring学习笔记(四)

 

<!-- 开启注解扫描

到包里面扫描类、方法、属性上面是否有注解

-->

<context:component-scan base-package=cn.itcast></context:component-scan>

<context:component-scan base-package=cn.itcast.anno,cn.itcast.xmlanno></context:component-scan>

<!-- 扫描属性上面的注解-->

<context:annotation-config></context:annotation-config>

1 注解创建对象

 一、在创建对象的类上面使用注解实现

 spring学习笔记(四)

spring学习笔记(四)

 spring学习笔记(四)

 二、创建对象有四个注解

 spring学习笔记(四)

 spring学习笔记(四)

三、创建对象是单实例还是多实例

 spring学习笔记(四)spring学习笔记(四)


 

2 注解注入属性

一、创建service类,创建dao类,在service得到dao对象

注入属性第一个注解@Autowired

1)创建daoservice对象

@Component(value=”userDao123”)也可以

 spring学习笔记(四)spring学习笔记(四)spring学习笔记(四)

 spring学习笔记(四)

(2)service类里面定义dao类型属性

 spring学习笔记(四)spring学习笔记(四)

//name属性值写注解创建dao对象value

注入属性第二个注解@Resource

 

 spring学习笔记(四)

配置文件和注解混合使用

1 创建对象操作使用配置文件方式实现

注入属性的操作使用注解方式实现

 spring学习笔记(四)spring学习笔记(四)spring学习笔记(四)

 

spring学习笔记(四)