理解什么是依赖注入?
我们使用的每个类都有可能使用其他类的对象,或者一些属性需要额外给出,这时候就要赋值给这个类。原先我们可以在类中直接赋值,但是随着代码的变多,我们有时候要修改源头的类的代码,这样有用到该类的方法或属性都要跟着改变。这种情况叫做高耦合。Spring要降低耦合度。采用依赖注入的方式配置要使用的属性。这个“依赖注入”看过去很高深,对象的依赖关系将由负责协调系统中各个对象的第三方组件在创建对象是设定,对象无需自行创建或管理他们的依赖关系—–依赖关系将被自动注入到需要它们的对象中去。
Aop 切面编程
在模块使用的时候,我们可能要扩展使用某个功能,或者复用它。如果在原先的类中再进行添加修改,则是非常不明智的做法。这时候就要用到切面。这个“切面”就是我们 要扩展的类和功能。“切入点”就是我们将要改造的方法。
一.装配bean
1.创建Spring配置
<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
Spring的核心框架自带10个命名空间
1. aop
2. beans
3. Context 为配置Spring应用上下文提供了配置元素,包括自动检测和自动装配bean,注入非Spring直接管理的对象
4. jee 提供了与javaEE API 的集成,例如JNDI 和 EJB
5. jms 为声明消息驱动的POJO提供给了配置元素
6.lang 支持配置有Groovy,JRUBY 或 beanSheel 等脚本实现的bean
7.mvc 启用Spring MVC的能力,例如面向注解的控制器,试图控制器和拦截器
8.oxm支持Spring的对象到xml 映射配置
9.tx 提供声明式事务配置
10.util 提供各种各样的工具类元素,包括把集合配置为bean 。支持属性占位元素
public class Juggler implements Performer {
private int beanBags = 3;
public Juggler(){
}
pubic Juggler(int beanBags){
}
public void perform()throws PerformanceExcetion{
System.out.println("Jugglering"+beanBags)
}
}
在Spring-ido1.xml中进行配置
<bean id="duke" class ="com.springinaction.springidol.Juggleer"/>
这句话相当于
new com.springinaction.springidol.Juggler();
我们如何使用这个类的方法呢?
Application context = new ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
Performer performer = (Performer)context.getBean("duck");
performer.perform();
发现我们还有属性没有进行配置,要怎么弄呢?
1.通过构造器注入属性
<bean id="duke" class ="com.springinaction.springidol.Juggleer">
<constructor-arg value ="15">
</bean>
如果属性是另一个引用变量呢?
<bean id="sonnet" class ="com.springinaction.springidol.Sonnet"/>
<bean id="duke" class ="com.springinaction.springidol.Juggleer">
<constructor-arg value ="15">
<constructor-arg ref="sonnet">//引用别的类的对象
</bean>
通过工厂方法创建bean
public class Stage{
private Stage(){}
private static Class StageSingletonHolder{
static Stage instance = new Stage();
}
public static Stage getInstance(){
return StageSingletonHolder.instance;
}
}
xml配置
<bean id="stage" class ="com.springinaction.springidol.Stage">
<factory-method ="getInstance"/>
</bean>
Bean的作用域
<bean id="sonnet" class ="com.springinaction.springidol.Sonnet" scope="prototype"/>//多例
还有其他种
1.singleton
单例在每一个Spring 容器中,一个bean定义只有一个对象实例(默认)
2.prototype
bean的定义可以被实例化多次,每次调用都创建一个实例
3.request
在一次Http请求中,每个bean定义一个实例,该作用域仅在基于web的Spring上下文中(SpringMVC)才有效
4.session
在一次Http Session请求中,每个bean定义一个实例,该作用域仅在基于web的Spring上下文中(SpringMVC)才有效
5.global-session
在一个全局Http Session 中,每个Bean定义对应一个实例,该作用域仅在Portlet上下文中才有效
- 初始化和销毁bean
<bean id="sonnet" class ="com.springinaction.springidol.Sonnet">
init-method="turnOnLights"
destory-method="turnOffLights"
</bean>
默认的init-method
<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
default-init-method=""
default-destroy-method="
</beans>
注入bean的属性
- 注入简单值
<property name="song" value="Jingle bells">
- 引用其他bean
<property name="song" ref="Jingle bells">
- 注入内部bean
<property name="song">
<bean class="com.springinaction.springidol.sonnet29 ">
</property>
<constructor-arg>
<bean class="com.springinaction.springidol.sonnet29 "
</constructor-arg>
- 使用Spring的命名空间p装配属性
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sonnet" class ="com.springinaction.springidol.Sonnet">
p:song="Jingle Bells"
p:instrument-ref="saxphone" />
装配集合
- list
- set
- map
- props 键值都是字符串
装配List Set Array
<property name="instruments"
<list>
<ref bean="guitar">
<ref bean="cymbal">
<ref bean="guitar">
</list>
</property>
<property name="instruments"
<set>
<ref bean="guitar">
<ref bean="cymbal">
<ref bean="guitar">
</set>
</property>
<property name="instruments"
<map>
<entry key="" value-ref="guitar"/>
<entry key="" value-ref="guitar"/>
<entry key="" value-ref="guitar"/>
</map>
</property>
<property name="instruments"
<props>
<prop key="">....</prop>
<prop key="">....</prop>
<prop key="">....</prop>
</props>
</property>
- 装配空置
<property name="..." ><null/></property>
- 使用表达式装配
<property name="..." value="#{5}" ></property>
<property name="..." value="#{le4}" ></property>
<property name="..." value="#{'chuck'}" ></property>
<property name="..." value="#{false}" ></property>
- 引用Bean Properties 和方法
<property name="..." value="#{saxophone}" ></property>
跟下面一样的效果
<property name="..." ref="saxophone" ></property>
还可以这样
<property name="..." value="#{saxophone.song}" ></property>
可以这样
<property name="..." value="#{saxophone。selector()}" ></property>
还可以这样
<property name="..." value="#{saxophone。selector().toUpperCase()}" ></property>
如果在SpEL中避免抛出讨厌的空指针异常
<property name="..." value="#{saxophone.selector()?.toUpperCase()}" ></property>