Spring学习总结(IOC)
来自bigjava 2020-12-13
1.0 Spring简介
-Spring是一个开源免費的框架、
-Spring是一轻量级,非侵入式的框架。
-控制反转(IOC),面相切面編程(AOP)。
-支持务处理事理,框架的整合。
2.0 Spring配置
文件名命名:applicationContext.xml (也可自定义)
官网:https://spring.io/
下载地址:https://repo.spring.io/release/org/springframework/spring/
需要的包:
(有几个包官网没有,可以去我主页下载)
xml配置:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
</beans>
3.0 IOC创建对象的方式
3.1 bean标签
ExeCotext类:
public class ExeCotext implements Context{
@Override
public void add() {
System.out.println("add()");
}
@Override
public void delete() {
System.out.println("delete");
}
}
applicationContext.xml:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!--id相当于new出来的属性
class为全额限定名
相当于
id=new class-->
<bean id="exeCotext" class="org.wuwen.context.dao.ExeCotext"></bean>
</beans>
Tset:
public class Tests {
public static void main(String[] args) {
String xml="applicationContext.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xml);
ExeCotext exeCotext= (ExeCotext) applicationContext.getBean("exeCotext");
exeCotext.add();
}
}
这里exeCotext的创建交给了Spring容器,那为啥要交给他呢?应为这样我们的程序就写死,继续往下看。
3.2 Spring 依赖注入(IOC)
service控制dao层:
//Context接口
public interface Context {
public void add();
public void delete();
}
//ExeCotext类dao
public class ExeCotext implements Context{
@Override
public void add() {
System.out.println("add()");
}
@Override
public void delete() {
System.out.println("delete");
}
}
//Acant类:service
public class Acant {
private Exception exception;
public void setException(Exception exception) {
this.exception = exception;
}
public Exception getException() {
return exception;
}
}
xml:
<bean id="exeCotext" class="org.wuwen.context.dao.ExeCotext"></bean>
<bean id="acant" class="org.wuwen.context.biz.Acant">
<!--naem是Acant类的属性名-->
<property name="exception" ref="exeCotext"/>
</bean>
Test:
public class Tests {
public static void main(String[] args) {
String xml="applicationContext.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xml);
Acant acant=(Acant) applicationContext.getBean("acant");
acant.getException().add();
}
}
这里是将ExeCotext类的创建交给Spring,等有多个Context接口的实现类时,就可以在不改变源码的情况下切换不同的实现类,切换时只要更XMl文件ref属性。
3.3 构造方法,setter注入
<bean id="exeCotext" class="org.wuwen.context.dao.ExeCotext"></bean>
<!--setter注入-->
<bean id="acant" class="org.wuwen.context.biz.Acant">
<property name="exception" ref="exeCotext"/>
</bean>
<!--构造方法注入-->
<bean id="acant" class="org.wuwen.context.biz.Acant">
<constructor-arg ref="exeCotext"/>
</bean>
3.4 属性注入
<bean id="acant" class="org.wuwen.context.biz.Acant">
<!--set注入-->
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<!--map注入-->
<property name="map">
<map>
<entry key="1">
<value>map1</value>
</entry>
<entry key="2">
<value>map2</value>
</entry>
<entry key="3">
<value>map3</value>
</entry>
</map>
</property>
<!--list注入-->
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<!--数组注入-->
<property name="arr">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<!--Properties注入-->
<property name="properties">
<props>
<prop key="username">name</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
3.5 c,p命名空间
需要导入头文件约束
C头文件约束:xmlns:c=“http://www.springframework.org/schema/c”
P头文件约束:xmlns:p=“http://www.springframework.org/schema/p”
<!--p命名空间,可以直接注入属性-->
<bean id="acant" class="org.wuwen.context.biz.Acant" p:arr="1">
</bean>
<!--c命名空间,在有参构造方法中注入-->
<bean id="acant" class="org.wuwen.context.biz.Acant" c:exception-ref="exeCotext">
</bean>
3.6 Bean的作用域
作用域 | 描述 |
---|---|
singleton | 在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,bean作用域范围的默认值。 |
prototype | 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()。 |
request | 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于web的Spring WebApplicationContext环境。 |
session | 同一个HTTP Session共享一个Bean,不同Session使用不同的Bean。该作用域仅适用于web的Spring WebApplicationContext环境。 |
application | 限定一个Bean的作用域为ServletContext 的生命周期。该作用域仅适用于web的Spring WebApplicationContext环境。 |
-
单例模式:(Spring默认模式)
<bean id="acant" class="org.wuwen.context.biz.Acant" c:exception-ref="exeCotext" scope="singleton"/>
-
圆形模式:每次从容器中get的时候,都会产生一个新对象
<bean id="acant" class="org.wuwen.context.biz.Acant" c:exception-ref="exeCotext" scope="prototype"/>
-
其余的request,session,application,只有wed开发中使用。