spring
1.spring简介
spring:是一个轻量级 的反控制(IOC) 和面向切面(AOP)的框架。Spring Boot一个快速开发的脚手架;基于spring Boot 可以开发单个服务;Spring Cloud基于spring Boot 实现的控制反转是一种设计思想(IOC),DI(依赖注入)是实现IOC的一种方式。IOC是将有自己(程序员)创建的对象,控制反转转移给第三方(用户);获得依赖注入的对象方式反转了;控制反转时通过一种通过描述(XML或注解) 并通过第三生产获取特定的对象的方式。在spring 中实现控制反转的时IOC容器,其实现方式时依赖注入(DependencyInjection,DI);
2.实例化对象
//获取spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("baens.xml");
//提取对象 类型 Object 强制转换;
Hello h = (Hello) context.getBean("hello");
System.out.println(h.toString());
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用spring对象-->
<bean id="hello" class="com.jbit.entitry.Hello">
<!--属性-->
<property name="Name" value="Spring"/>
</bean>
</beans>
3.反转
控制反转的过程:
控制:传传统应用的对象时有程序员来控制创建的,使用Spring之后,对象时有Spring来创建的;
反转:程序本生不创建对象,变为被动接收对象;
依赖注入: 就是利用set方法来进行注入;
IOC时一种编程思想,有主动编程编程被动的接收;
对象由Spring 来创建,管理,装配;
4.maven 依赖
<!--spring maven 依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
5.IOC 创建对象方式
1. 使用spring对象
<!--方式1 使用spring对象 (无参构造)-->
<bean id="hello" class="com.jbit.entitry.Hello">
<!--set属性赋值-->
<property name="Name" value="Spring"/>
</bean>
2. 下标赋值
<!--方式2 使用有参构造赋值-->
<bean id="hello2" class="com.jbit.entitry.Hello">
<!--下标赋值-->
<constructor-arg index="0" value="狂神"/>
</bean>
3.根据参数类型赋值
<!--方式3 使用有参构造赋值 (不推荐使用)-->
<bean id="hello3" class="com.jbit.entitry.Hello">
<!--根据参数类型赋值-->
<constructor-arg type="java.lang.String" value="狂神"/>
</bean>
4.根据构造函数中参数
<!--方式4 使用有参构造赋值-->
<bean id="hello4" class="com.jbit.entitry.Hello">
<!--直接更具构造函数中参数的名称赋值-->
<constructor-arg name="name" value="狂神"/>
</bean>
6.spring 配置
6.1别名
<!--别名 如果添加了别名也可以使用别名-->
<alias name="hello" alias="h"/>
6.2Bean配置
<!--
id: bean 的对象名 唯一标识;
class: bean 的对应的全限定类名
name: 别名 (可以其多个别名)
-->
<bean id="hello" class="com.jbit.entitry.Hello" name="别名1,别名2"></bean>
6.3Import
Impot 一般作用于团队,可将多个配置文件,导入集合成一个配置;
使用总配置文件即可;
<import resource="baens.xml"/>
7.依赖注入
7.1构造器注入
依赖注入 set注入
依赖:bean 对象的创建依赖于容器;
注入:bean 中所有的属性,有容器来注入;
7.2set注入 (重要)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="addes" class="com.jbit.entitry.Addes">
<property name="addresss" value="邵阳市邵东市宋家塘"/>
</bean>
<bean id="student" class="com.jbit.entitry.Student">
<!--普通注入-->
<property name="Name" value="佘莉"/>
<!--bean注入-->
<property name="addes" ref="addes"/>
<!--s数组注入-->
<property name="book">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>水浒传</value>
<value>西游记</value>
</array>
</property>
<!--list集合注入-->
<property name="list">
<list>
<value>玩游戏</value>
<value>听歌</value>
</list>
</property>
<!--map注入-->
<property name="map">
<map>
<entry key="身份证" value="123166565656"/>
</map>
</property>
<!--Set注入-->
<property name="se">
<set>
<value>LOL</value>
<value>CF</value>
</set>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">20200525123545</prop>
</props>
</property>
</bean>
</beans>
测试类
public static void main(String [] args){
ApplicationContext pa = new ClassPathXmlApplicationContext("apploctioncontext.xml");
Student stu = (Student) pa.getBean("student");
System.out.println(stu.toString());
}
7.3拓展注入
P命名空间注入: p - property 对应属性注入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="john-modern"
class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>
</beans>
c命名空间注入: C
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
</beans>
8.bean的作用域
范围 描述
singleton 默认)将每个Spring IoC容器的单个bean定义范围限定为单个对象实例。
proptotype 将单个bean定义的作用域限定为任意数量的对象实例。
request 将单个bean定义的范围限定为单个HTTP请求的生命周期。也就是说,每个HTTP请求都有一个在单个 bean 定义后面创建的bean实例。仅在可感知网络的Spring上下文中有ApplicationContext。
session 将单个bean定义的范围限定为HTTP的生命周期Session。仅在可感知网络的Spring上下文中有效 ApplicationContext。
apploctiong 将单个bean定义的作用域限定为的生命周期ServletContext。仅在可感知网络的Spring上下 文中有效ApplicationContext。
webscoket 将单个bean定义的作用域限定为的生命周期WebSocket。仅在可感知网络的Spring上下文中有效 ApplicationContext。
8.1单例模式
Spring(默认机制)scope=”singleton”
<bean id="accountService" class="com.something.DefaultAccountService"/>
<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
8.2原型模式
每次从容器中get的时候都会产生一个新的对象;scope=”prototype”
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
8.3web作用域
resquest :
sesstion:
apploction:
websocket:
9.Bean的自动装配
1.测试
一个人有两个宠物 (未装配)
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.jbit.entitry.Cat" p:name="狂小猫"></bean>
<bean id="dog" class="com.jbit.entitry.Dog" p:name="狂小狗"></bean>
<bean id="peopole" class="com.jbit.entitry.Peopole">
<property name="cat" ref="cat"/>
<property name="name" value="佘莉"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
2.byName(自动装配)
autowire=”byName”
byName:会自动查找容器中和自己对象set方法后面相同的 bean id;
注意: byName 的时候保证自己的属性于bean id 一致 并且唯一
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="peopole" class="com.jbit.entitry.Peopole" autowire="byName">
<property name="name" value="佘莉"/>
</bean>
</beans>
3.byType(自动装配)
autowire=”byType”
byType:会自动查找容器中和自己对象属性类型相同的 bean;
注意: byType 装配时 bean的类型唯一 并且以自己对象的属性的类型一致;
<bean id="peopole" class="com.jbit.entitry.Peopole" autowire="byType">
<property name="name" value="佘莉"/>
</bean>
4.注解装配
使用注解须知
导入约束
xmlns:context=”http://www.springframework.org/schema/context“
http://www.springframework.org/schema/context[https://www.springframework.org/schema/context/spring-context.xsd](https://www.springframework.org/schema/context/spring-context.xsd)配置注解支持 context:annotation-config/ ```xml <?xml version=”1.0” encoding=”UTF-8”?>
<br />[@Autowired ](/Autowired ) :
<br />可以在属性上直接使用
<br />也可在set方法上使用
```java
public class Peopole {
private String Name;
@Autowired
private Dog dog;
@Autowired
private Cat cat;
@Autowired
public void setName(String name) {
Name = name;
}
}
< 注解
@Nullable 该注解背后的参数可以未null
@Autowired(required = false)
Autowired 的属性required 为false 则允许为null,否则不允许为空,默认为true;
使用方法:(在类的属性或方法上)
@Autowired
@Qualifier(value = "cat")
Autowired 的装配环境比较复杂时 可以使用 Qualifier
@Qualifier(value = "cat") 去指定一个bean 进行装配
@Resource 先根据bean的 id 去查找 ,在根据类型去查找 id找不到时找 类型(必须唯一)
@Resource(name = "bean id") 更具 bean 的id去查找··
@Component(组件) 注解 :可将该类配置为容器中的bean
衍生注解对应 MVC 三成架构
dao @Repository
serivce @serivce
contorller: @contorller
@Scope("singleton")注解:作用域配置
>
<!--指定扫描的包-->
<context:component-scan base-package="com.sli.entrtiy"/>
在指定的包下的类中加入 @Component(组件) 注解 :类配置被Spring管理了 就是bean
等同于 <bean id="user" class="com.sli.entrtiy.User"/>
<!--作用域配置-->
<bean scope="singleton"/>
@Component 示例
@Component
public class User {
@Value("张三")
private String Name ;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
