自学Spring
Spring官网地址https://spring.io/
springManven官网地址:https://mvnrepository.com
————————————————————————
springManven框架地址:https://mvnrepository.com/artifact/org.springframework/spring-webmvc/5.3.13
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
——————jdbc:——————————
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.13</version>
</dependency>
——————jdbc:——————————
————————————————————————————————————————————
spring官网框架地址:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core
源码:
<?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="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
————————————————————————————————————————————
结论:
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:地址,方法包的地址 --有提示!
例子:<bean id="sqlserver" class="com.zui.dao.UserSqlServer" />
IOC创建对象的方式
1)使用无参构造创建对象,默认!
2)使用有残构造创建对象,
1.下标赋值
Spring配置
1)alias:别名
可以根据bean里的id来进行设置别名,在页面中也可以通过调用他的别名来获取到这个对象
例子:<alias name="user" alias="123"></alias>
2)name:也是别名
更高级的别名,可以创建多个别名,通过 逗号||空格 进行分割
例子:<bean id="sqlserver" class="com.zui.dao.UserSqlServer" name="123","wer",..... />
3):import:导入
把不同的xml文件导入到一个总的xml文件里,使用的时候直接拿总的xml就可以了
依赖注入
1)构造器注入
2)set注入*
————————————————————————————————————<?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="address" class="com.zui.pojo.Address" />
<bean id="student" class="com.zui.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="黑大帅" />
<!--第二种,Bean注入,ref-->
<property name="address" ref="address" />
<!--第三种,数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>三国演义</value>
<value>水浒传</value>
</array>
</property>
<!--第四种,List注入-->
<property name="hobbys">
<list>
<value>打篮球</value>
<value>吃饭</value>
<value>睡觉</value>
<value>旅游</value>
</list>
</property>
<!--第五种,Map注入-->
<property name="card">
<map>
<entry key="姓名" value="张亮" />
<entry key="实体店" value="麻辣烫" />
</map>
</property>
<!--第六种,Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>Moba</value>
<value>CF</value>
</set>
</property>
<!--第七种,null注入-->
<property name="wife">
<null />
</property>
<!--第八种,Properties注入-->
<property name="info">
<props>
<prop key="DatabaseName">SqlServer</prop>
<prop key="url">//localhost:8080</prop>
<prop key="userName">sa</prop>
<prop key="pwd">sa</prop>
</props>
</property>
</bean>
</beans>
————————————————————————————————————
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"?>
<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">
<!--p命名空间注入,可以直接注入属性的值-->
<bean id="user" class="com.zui.pojo.User" p:name="黑大帅" p:age="18" />
<!--c命名空间注入,通过构造器注入-->
<bean id="user2" class="com.zui.pojo.User" c:age="18" c:name="潇洒哥" />
</beans>
————————————————————————————————————
bean作用域
1)单例模式(Spring默认机制)
好处:单线程推荐使用单例模式
坏处:并发的情况下,会导致数据延迟或者不一致
【scope="singleton"】
例子:
<bean id="user2" class="com.zui.pojo.User" c:age="18" c:name="潇洒哥" scope="singleton" />
2)原型模式(Spring默认机制)
好处:多线程推荐使用原型模式
坏处:每一次都会产生一个新对象,导致资源浪费
【scope="prototype"】
例子:
<bean id="user2" class="com.zui.pojo.User" c:age="18" c:name="潇洒哥" scope="prototype" />
bean的自动装配
·自动装配是Spring满足bean依赖的一种方式!
·Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中三种装配的方式
1.在xml中显示的配置
2.在java中显示配置
3.隐私的自动装配【重要】
byname的时候,需要保证bean的id唯一,并且这个bean需要和自动注入的属性的set方法一致!
bytype的时候,需要保证bean的class唯一,并且这个bean需要和自动注入的属性类型一致!
使用注解实现自动配置
1.导入约束
<context:annotation-config/>
2.配置注解的支持
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
最新文章
- python基础-文件操作
- 2017-1-2 nfs服务器配置
- 浅谈C++设计模式之单例模式
- Photoshop: 机关单位公章
- windows下android环境的搭建:完成后添加android其他版本
- 【阿里云产品公测】利用PTS服务优化网站数据库读写性能
- 【Java重构系列】重构31式之搬移方法
- PixelFormat 枚举
- Maven项目引入log4j的详细配置
- 使用数据泵expdp、impdp备份和还原oracle数据库
- Codeforces Round #553 (Div. 2) B题
- 基于vue-cli3的vue项目移动端样式适配,lib-flexible和postcss-px2rem
- mysql从库Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: &#39;Could not find first log file name in binary log index file&#39;报错处理
- currentStyle&;getComputedStyle获取属性
- BrowserSync前端同步测试工具
- keras embeding设置初始值的两种方式
- 【基于初学者的SSH】struts2 值栈的详解与struts2标签库+ognl表达式
- mysql安装版卸载,解压版安装
- [转]How do I run msbuild from the command line using Windows SDK 7.1?
- 基于Kubernetes(k8s)的RabbitMQ 集群