自学Spring


Spring官网地址https://spring.io/

springManven官网地址:https://mvnrepository.com
————————————————————————

springManven框架地址:https://mvnrepository.com/artifact/org.springframework/spring-webmvc/5.3.13




org.springframework
spring-webmvc
5.3.13

 

——————jdbc:——————————



org.springframework
spring-jdbc
5.3.13

——————jdbc:——————————

 

————————————————————————————————————————————
spring官网框架地址:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core
源码:
<?xml version="1.0" encoding="UTF-8"?>

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">





————————————————————————————————————————————

 


结论:
1)IOC编程思想,是由主动的编程编程被动的接收


2)IOC就是对象由spring创建,管理,装配

 

 


xml读取
//从项目中去拿我的容器,可以拿一个,也可以拿多个|||固定死的,必须写这一行
//("demo.xml","XXX.xml",.....)
ApplicationContext context = new ClassPathXmlApplicationContext("demo.xml");


创建容器(xml):
1)ref :引用spring容器中创建好的对象
2)value: 具体的值,基本数据类型!
3)id:自己取的名称,到时候要从页面中取容器中的id来使用
4)class:地址,方法包的地址 --有提示!
例子:


IOC创建对象的方式

1)使用无参构造创建对象,默认!
2)使用有残构造创建对象,
1.下标赋值


Spring配置
1)alias:别名
可以根据bean里的id来进行设置别名,在页面中也可以通过调用他的别名来获取到这个对象
例子:
2)name:也是别名
更高级的别名,可以创建多个别名,通过 逗号||空格 进行分割
例子:
3):import:导入
把不同的xml文件导入到一个总的xml文件里,使用的时候直接拿总的xml就可以了



依赖注入

1)构造器注入

2)set注入*
————————————————————————————————————<?xml version="1.0" encoding="UTF-8"?>

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">









红楼梦
西游记
三国演义
水浒传





打篮球
吃饭
睡觉
旅游












LOL
Moba
CF







SqlServer
//localhost:8080
sa
sa




————————————————————————————————————

3)扩展方式注入
使用 p/c 命名空间来注入
!!!需要导入约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"需要构造器,有参和无参
————————————————————————————————————<?xml version="1.0" encoding="UTF-8"?>

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作用域
1)单例模式(Spring默认机制)
好处:单线程推荐使用单例模式
坏处:并发的情况下,会导致数据延迟或者不一致
【scope="singleton"】
例子:

2)原型模式(Spring默认机制)
好处:多线程推荐使用原型模式
坏处:每一次都会产生一个新对象,导致资源浪费
【scope="prototype"】
例子:


bean的自动装配
·自动装配是Spring满足bean依赖的一种方式!
·Spring会在上下文中自动寻找,并自动给bean装配属性


在Spring中三种装配的方式

1.在xml中显示的配置
2.在java中显示配置
3.隐私的自动装配【重要】
byname的时候,需要保证bean的id唯一,并且这个bean需要和自动注入的属性的set方法一致!
bytype的时候,需要保证bean的class唯一,并且这个bean需要和自动注入的属性类型一致!


使用注解实现自动配置

1.导入约束

2.配置注解的支持
<?xml version="1.0" encoding="UTF-8"?>

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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">