Spring笔记
目录
2、根据bean的类型获取对象 常用 getBean(Student.class);
3、根据bean的id和类型获取对象 getBean("student", Student.class)
接下来三个月所学
框架:spring;springmvc;mybatisplus(orm)实体类和数据库映射;这三个简称SSM
springboot;
数据库:redis(非关系型数据库)
概念:
Spring是一款轻量级框架,
核心:IOC 和 AOP 它能够集成其他优秀框架,比如mybtayis,springmvc等。
IOC:控制反转,把以前手动创建的对象交给spring创建对象
DI:依赖注入,事IOC的另外一种说法,常用的注入方式有set注入和构造器注入
Spring:春天------>给软件行业带来了春天!
2002,首次推出了Spring框架的雏形:interface21框架!
Spring框架即以interface21框架为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日发布了1.0正式版。
Spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架!
SSH:Struct2 + Spring + Hibernate!
SSM:SpringMVC + Spring + Mybatis!
优点
- Spring是一个开源的免费的框架(容器)!
- Spring是一个轻量级的、非入侵式的框架!
- 控制反转(IOC),面向切面编程(AOP)!
- 支持事务的处理,对框架整合的支持!
IOC 控制反转
总结:IOC是一个容器管理bean的,IOC就是将对象由Spring去创建,管理,装配!
bean:配置一个bean对象,将对象交给IOC容器管理
控制什么?
控制创建对象的方式;
谁来控制?
原本应用程序的对象是通过程序本身控制创建,
加入Spring后,对象由Spring来创建;
反转?
程序本身不再创建对象了,而是被动接收Spring创建的对象;
依赖注入DI?
就是利用set方法进行注入;
IOC和DI关系
DI(依赖注入)是实现IOC(控制反转)这个编程思想的一种方式;
IOC本质
- IOC控制反转 inversion of control
- IOC是一种编程思想,由主动编程变成被动的接收对象
- 通过 new ClassPathXMLApplicationContext去浏览底层代码
- IOC是Spring框架的核心内容
- IOC是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。
★获取IOC容器
ApplicationContext:上下文 ClassPathXmlApplicationContext:读取配置来用的
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
★bean的三种创建实例的方式
FactoryBean
/*
* FactoryBean是一个接口,需要创建一个实现类实现该接口
* 其中有三个方法
* getObject():通过一个对象交给IOC容器管理
* getObjectType():设置所提供对象的类型
* isSingleton():所提供的对象是否单例,默认true:单例 false:多例
* 当把FactoryBean的实现类配置bean时,会将当前类中getObject()所返回的对象交给IOC容器管理
* */
public class UserFactoryBean implements FactoryBean<Users> {
@Override
public Users getObject() throws Exception {
return new Users();
}
@Override
public Class<?> getObjectType() {
return Users.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
//配置bean
//bean拿到的是getObject()方法里面的对象
<bean class="com.springYKA.factory.UserFactoryBean"></bean>
//测试
@Test
public void Test(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-factory.xml");
Users bean = ioc.getBean(Users.class);
System.out.println(bean);
}
★获取IOC容器中的bean的三种方式:
getBean():获取类对象的
bean是一个组件,class参数是组件对象
bean:配置一个bean对象,将对象交给IOC容器管理
属性:
id:bean的唯一标识,不能重复
class:设置bean对象所对应的类型
这是.xml配置文件中的bean组件
通过反射创建对象的com.springYKA.entity.Student(包+类名=全类名)
<bean id="student" class="com.springYKA.entity.Student"></bean>
- 根据bean的id获取对象 getBean("student")
Student student1 = (Student) ioc.getBean("student");
2、根据bean的类型获取对象 常用 getBean(Student.class);
Student student2 = ioc.getBean(Student.class);
* 注意:根据类型获取bean时,要求IOC容器中有且只要一个类型匹配bean
若同一个类型匹配多个bean,以下代码此时抛出异常:NoUniqueBeanDefinitionException
<bean id="student" class="com.springYKA.entity.Student"></bean>-->
<bean id="student2" class="com.springYKA.entity.Student"></bean>-->
若没有任何一个类型匹配的bean,以下此时抛出异常:NoSuchBeanDefinitionException
空的
3、根据bean的id和类型获取对象 getBean("student", Student.class)
Student student3 = ioc.getBean("student", Student.class);
* 结论:
* 根据类型来获取bean时,在满足bean唯一性的前提下
* 其次只是看:【对象 instanceof 指定的类型】的返回结果
* 只要返回的时true就可以认定为和类型匹配 能够获取到
* 即通过备案的类型,bean所继承的类的类型,bean所实现的接口类型都可以获取bean
* People people = ioc.getBean(People.class);这是个接口,通过接口去获取Student对象
★DI:依赖注入
Set依赖注入
bean是一个组件,class参数是组件对象
property标签:bean的属性标签 通过组件类的setXxx()方法给组件对象设置属性
name属性:设置需要赋值的属性名;指定属性名(这个属性是getXxx()、setXxx()方法定义的,和成员变量无关)
value属性:指定属性值,设置为属性所赋的值
字面量类型:用value给属性赋值
private String name;
private Integer age;
private String sex;
类类型:
private Class clazz;
为类类型的属性赋值 类类型的用ref获取
ref:引用IOC容器中某个bean的id 引用外部bean的id 关联某个类的,指向关联类的id
设置时间类型
<bean name="sdf" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd"></constructor-arg>
</bean>
<bean id="studentTwo" class="com.springYKA.entity.Student">
<!-- setter依赖注入
bean是一个组件,class参数是组件对象
property标签:通过组件类的setXxx()方法给组件对象设置属性
name属性:设置需要赋值的属性名;指定属性名(这个属性是getXxx()、setXxx()方法定义的,和成员变量无关)
value属性:指定属性值,设置为属性所赋的值
-->
<property name="name" value="李四"></property>
<property name="age" value="88"></property>
<!-- 这样正确设置null 如果写出value=“null” 就变成字符串null了-->
<property name="sex">
<null/>
</property>
正确设置时间date类型
<property name="date">
<bean factory-bean="sdf" factory-method="parse">
<constructor-arg index="0" value="2022-11-10"></constructor-arg>
</bean>
</property>
</bean>
<!--为类类型的属性赋值-->private Class clazz;
两种常用方式:
1、引用外部bean的id 通过ref:引用IOC容器中某个bean的id
2、内部bean:只能在当前bean内部使用,不能直接通过IOC容器获取
<bean id="studentFour" class="com.springYKA.entity.Student">
<property name="name" value="孙佳佳"></property>
<property name="age" value="18"></property>
<property name="sex" value="男"></property>
<!--ref:引用IOC容器中某个bean的id 引用外部bean的id 关联某个类的,指向关联类的id
<property name="clazz" ref="clazzOne"></property>
<!-- <property name="clazz">
内部bean:只能在当前bean内部使用,不能直接通过IOC容器获取
<bean id="clazzInner" class="com.springYKA.entity.Class">
<property name="Cname" value="软工"></property>
<property name="Cid" value="2110A"></property>
</bean>
</property>-->
<!-- 字面量类型数组属性赋值-->
<property name="hobby">
<array>
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
</array>
</property>
<!--字面量类型集合类型属性赋值-->
<property name="list">
<list>
<value>米饭</value>
<value>面条</value>
<value>馒头</value>
</list>
</property>
<property name="set">
<set>
<value>香蕉</value>
<value>苹果</value>
<value>草莓</value>
</set>
</property>
<property name="map">
<map>
<!--字面量类型写法-->
<entry key="no1" value="今天"></entry>
<entry key="no2" value="明天"></entry>
<entry key="no3" value="后天"></entry>
类类型的:引用外部bean的写法
<!-- <entry key-ref="" value-ref=""></entry> -->
</map>
</property>
<!--类类型集合属性赋值-->
<property name="ListStudent">
内部
<list>
<ref bean="student"></ref>//类类型引用方式
<ref bean="studentTwo"></ref>
<ref bean="studentTree"></ref>
</list>
</property>
</bean>
<!-- 配置Clazz类型的bean
外部bean 也能直接通过IOC容器获取-->
<bean id="clazzOne" class="com.springYKA.entity.Class">
<property name="Cname" value="软工"></property>
<property name="Cid" value="2110A"></property>
</bean>
集合属性赋值
private List<String> list;
private Set<Student> set;
private Map<String,Car> map;
<bean id="user" class="com.springYKA.entity.User">
<!-引用外部的bean-->
<property name="list" ref="lists"></property>
<property name="set" ref="sets"></property>
<property name="map" ref="maps"></property>
</bean>
外部集合类型的bean
<!--配置一个集合类的bean,需要使用util约束-->
<!--list集合类型的bean -->
<util:list id="lists">
<value>1</value>
<value>2</value>
<value>3</value>
</util:list>
<!--set集合类型的bean -->
<util:set id="sets">
<ref bean="student"></ref>
<ref bean="studentTwo"></ref>
<ref bean="studentTree"></ref>
</util:set>
<!--map集合类型的bean -->
<util:map id="maps">
<entry>
<key>
<value>n01</value>
</key>
<ref bean="car"></ref>//类类型引用方式
</entry>
<entry>
<key>
<value>n02</value>
</key>
<ref bean="car"></ref>
</entry>
//两种写法
<entry key="no3" value-ref="car"></entry>
</util:map>
构造器依赖注入
<!--
构造器注入 必须在student类中添加有参构造器
注意:
constructor-arg标签还有两个属性可以进一步描述构造器参数:
index属性:指定参数所在位置的索引(从0开始)
name属性:指定参数名
-->
<!--
实现效果<王五> value中不能之间这样写 正确写法<王五>
<:<
>:>
-->
<bean id="studentSix" class="com.springYKA.entity.Student">
<constructor-arg index="0" value="张三"></constructor-arg>
<constructor-arg index="1" value="16"></constructor-arg>
<constructor-arg index="2" value="男"></constructor-arg>
<constructor-arg index