Spring(前部分)
Spring
Spring是一个轻量级非入侵式控制反转(IoC)和面向切面(AOP)的容器框架。强大的向后兼容性,解决企业级开发的复杂性
1、spring七大模块
springboot
- 一个快速开发的脚手架
spring cloud
- 基于springboot实现
2、IOC理论
DI(依赖注入)是实现IOC的一种实现方法
2、1 构造器注入
通过使用type属性显式指定构造函数参数的类型,则容器可以使用简单类型的类型匹配
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
使用该index属性来明确指定构造函数参数的索引
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
使用构造函数参数名称来消除歧义
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateAnswer" value="42"/>
</bean>
2、2 Set方式注入
-
依赖注入:set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
-
复杂类型
package com.demo.pojo;
public class Address {
private String address;
public Address() {
this.address = address;
}
public Address(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 真实测试对象
package com.demo.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String > hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
}
- beans.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="student" class="com.demo.pojo.Student" >
<!--普通值注入-->
<property name="name" value="ergou"></property>
</bean>
</beans>
- 测试类
import com.demo.pojo.Student;
import com.demo.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String[] args) {
//获取Spring上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
- 完善注入
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="address" class="com.demo.pojo.Address">
<property name="address" value="chaina"></property>
</bean>
<bean id="student" class="com.demo.pojo.Student" >
<!--普通值注入-->
<property name="name" value="ergou"></property>
<!--bean注入 ref -->
<property name="address" ref="address"></property>
<!--数组注入 -->
<property name="books" >
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
</array>
</property>
<!--List注入-->
<property name="hobbies">
<list>
<value>唱歌</value>
<value>代码</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="身份证" value="1212121212">
</entry>
<entry key="银行卡" value="1212121212">
</entry>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>lol</value>
<value>王者</value>
</set>
</property>
<!--null值-->
<property name="wife">
<null></null>
</property>
<!-- properties 注入-->
<property name="info">
<props>
<prop key="学号">20164004115</prop>
<prop key="姓名">二狗</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
2、3 拓展方式注入
<!--p 命名空间注入-->
<bean id="user23" class="com.demo.pojo.User" p:age="18">
</bean>
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- c命名空间-->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
- 注意
- p命名和c命名不能直接使用 需要导入xmlns
3、Spring配置
3、1 别名
可以通过别名获取对象
<alias name="user" alias="userNow"/>
3、2 bean的配置
<!--
id:bean的唯一标示,相当于对象名
class:bean对应的权限定名 包名+类名
name: 也是别名,而且name可以同时取多个别名
-->
<bean id="user2" class="com.demo.pojo.User" name="s,fe ef:f">
<property name="name" value="ergou"></property>
</bean>
3、3 improt
一般用于团队开发,可以讲多个bean文件合并为一个
在applicationContext.xml中整合其他bean文件
<import resource="beans1.xml"></import>
4、Bean Scopes
4、1 作用域
-
1、单例模式 singleton
-
scope ="singleton"
-
-
2、原型模式 prototype
- 每次从容器中get的时候,都会产生一个新对象
-
3、其他的只能在web开发中使用
5、bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式
- Spring会在上下文中自动寻找,并自动给bean装配属性
在spring有三种装配的方式
1、在xml中显示的配置
2、在java中显示配置
3、隐式的自动装置配置
测试
<bean id="cat" class="com.demo.pojo.Cat"/>
<bean id="dog" class="com.demo.pojo.Dog"/>
autowire="byName" 在容器上下文中查找 根据get方法后面的name 实现自动装配 保证所有bean的id唯一
<bean id="user2" class="com.demo.pojo.User" name="s,fe ef:f" autowire="byName">
<property name="name" value="ergou"></property>
</bean>
autowire="byType" 在容器上下文中查找 根据自己属性类型相同的bean 保证bean的class唯一
<bean id="user2" class="com.demo.pojo.User" name="s,fe ef:f" autowire="byType">
<property name="name" value="ergou"></property>
</bean>
-
注解自动装配
- jdk1.5之后
- 导入约束 xmlns:context="http://www.springframework.org/schema/context"
- jdk1.5之后
-
配置注解支持
-
<?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/> 注解支持 <bean id="cat" class="com.demo.pojo.Cat"/> <bean id="dog" class="com.demo.pojo.Dog"/> <bean id="user2" class="com.demo.pojo.User" /> </beans>-
在属性字段上添加@Autowired 即可使用
-
@nallable 可以为空 @Required =false 说明对象可以为null @Qualifier(value="dog222") 指定装配 一般配合使用
-
-
6、使用注解开发2
- 必须保证aop的包导入
- applicationContext.xml 头文件
- @componet 组件 扫描配置
- @value(“xxx”) 直接赋值

浙公网安备 33010602011771号