1.spring简介:
整合现有的技术框架。是一个轻量级的控制反转(IOC)和面向切面编程的框架(AOP),支持事务的处理和对框架整合的支持
DI(控制反转)就是IOC(依赖注入)的具体实现方式
弊端:配置十分繁琐!
官方下载地址: http://repo.spring.io/release/org/springframework/spring
GitHub地址:GitHub - spring-projects/spring-framework: Spring Framework
spring boot是快速开发的脚手架,微服务,约定大于配置
spring cloud是基于spring boot实现的
2.开发的基本配置
* 1.创建beans.xml spring的配置文件
<?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="hello" class="com.yang.pojo.Hello">
<constructor-arg index="0" value="spring学习"/>
</bean>
<bean id="hello" class="com.yang.pojo.Hello">
<constructor-arg type="java.lang.String" value="zhangsan"/>
</bean>
</beans>
3.IOC创建对象的四种方式
第一种必须要有无参构造,后面三种都是使用无参构造创建对象
1.常用的方式(默认),类中必须要有无参构造方法,否则报错!
<bean id="hello" class="com.yang.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
有dao,service三层结构的写法如下
<bean id="userDao" class="com.yang.dao.UserDaoImpl"/>
<bean id="userService" class="com.yang.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
2.使用有参构造创建对象
1.下标赋值法
<bean id="hello" class="com.yang.pojo.Hello">
<constructor-arg index="0" value="spring学习"/>
<constructor-arg index="1" value="23"/>
</bean>
2.属性类型赋值法,若是多个属性都是同一类型,就不好区分了----不推荐使用
<bean id="hello" class="com.yang.pojo.Hello">
<constructor-arg type="java.lang.String" value="zhangsan"/>
</bean>
3.参数名赋值
<bean id="hello" class="com.yang.pojo.Hello">
<constructor-arg name="str" value="nihao"/>
</bean>
4.spring配置文档学习
1.alise 别名 : 同bean标签下的name属性
<alias name="hello" alias="newHello"/>
2.bean的配置
<bean id="hello" class="com.yang.pojo.Hello" name="he1 he2,he3;he4"> <constructor-arg index="0" value="spring学习"/> <constructor-arg index="1" value="23"/></bean>
3.import
用于团队开发,可将多个文件导入合并成一个,注意bean不要重复!
<import resource="beans.xml"/>
##### 4.各种类型属性的依赖注入
<?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"> <import resource="beans.xml"/> <bean id="addre" class="com.yang.pojo.Address"> <property name="address" value="河南"/> </bean> <bean id="student" class="com.yang.pojo.Student"> <property name="name" value="zhangqian"/> <property name="address" ref="addre"/> <property name="hobbies" > <list> <value>编程</value> <value>舞蹈</value> <value>歌曲</value> </list> </property> <property name="games" > <set> <value>LOL</value> <value>BOB</value> <value>COC</value> </set> </property> <property name="card" > <map> <entry key="身份证" value="422535454"/> <entry key="银行卡" value="2324"/> </map> </property> <property name="books" > <array> <value>红楼梦</value> <value>西游记</value> <value>水浒传</value> </array> </property> <property name="wife" > <null/> </property> <property name="info" > <props merge="true"> <prop key="学号">12345</prop> <prop key="姓名">张三</prop> <prop key="性别">男</prop> </props> </property> </bean></beans>
public class Student { private String name; private Address address; private List<String> hobbies; private Map<String,String> card; private String[] books; private Set<String> games; private String wife; private Properties info;
总结依赖注入的三种方式:
1. 构造器注入2. set方式注入(**重点!**),即上面通过属性创建bean的方式 * 依赖:bean对象的创建依赖于容器 * 注入:bean对象中的所有属性都由容器来注入3. 标签注入,即p标签和c标签注入,扩展方式
5.命名空间
p命名空间和c命名空间
-
p命名空间: 属性命名空间,即property简写.必须有无参构造
先引入头文件
xmlns:p="http://www.springframework.org/schema/p"
<bean id="hello" class="com.yang.pojo.Hello" p:age="2" p:name="sansan"/>
-
c命名空间:构造器命名空间,即constructor简写.必须要有有参构造
先引入头文件
xmlns:c="http://www.springframework.org/schema/c"
<bean id="hello1" class="com.yang.pojo.Hello" c:age="1" c:name="lisi"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)