spring概念及其应用:
IOC:
管理对象之间的依赖关系和生命周期。将对象之间的依赖耦合关系抽取成模块,由IOC容器管理,提高程序的模块性和灵活性。
org.springframework.beans.factory.BeanFactory接口定义了对象管理的基本方法,包括创建对象,检查对象的属性、检查IOC容器是否包含该对象。是不是单例的。
对象之间的依赖关系通常通过XML文件配置好,交给spring的ioc容器管理。基本配置:
定义bean
<bean id="usedao" class="com.demo.dao.UserDAO" />
<bean name="userdao" class="com.demo.dao.UserDAO" />
其他属性:
scope : 定义对象的形态,可选值: singleton / prototype
depends-on:所依赖的对象,必须在本对象初始化之前初始化,其值为其他bean的ID值。
dependency-check:在注入属性后,是否执行对象属性的依赖检查,其值为:none / simple /object / all
autowire: 对象注入的方式,一般用byName / byType 两种方式。
lazy-init:延迟初始化,true 、 false
abstruct:定义是否为抽象类,此类可以不存在。
parent:java的继承概念的体现。用于指定对象的abstruct类
DI注入依赖:
setter方式:
<property name="atrributeName" value="aa" />
<property name="attributeName" ref="beanId" />
<pooperty name="attributeName">
<idref local="" /> //idref 和value 同义,用于将其他bean的id值作为一个字符串注入进来,local只是使id值
<idref bean="" /> //idref 和value 同义,用于将其他bean的id值作为一个字符串注入进来,bean可以使id或name值
<ref local="" /> //不能跨xml文件,只能指定其他bean的id值
<ref bean="" /> //跨xml文件 ,指定其他bean的id或name值
<ref parent="" /> //父容器bean的id或name值
</property>
constructor方式:
<constructor-arg index="0" ref="" /> // 将对应的bean注入到索引为0的构造函数中
<constructor-arg index="1" ref="" /> // 将对应的bean注入到索引为1的构造函数中
<constructor-arg type="java.lang.String" ref="" />// 根据类型匹配注入参数
<constructor-arg type="int" value="1233" /> // 根据类型注入参数
构造函数可以凭借索引和类型区别注入的参数,但spring推荐索引方式,这样可以避免由于类型冲突造成的错误
注入java.util.List / Map / Set 和 数组:
<property name="list" >
<list>
<value>234</value>
<ref bean="otherBean" />
</list>
</property>
<property name="set" >
<set>
<value>234</value>
<ref bean="otherBean" />
</set>
</property>
<property name="array" >
<list>
<value>234</value>
<ref bean="otherBean" />
</list>
</property>
<property name="properties" >
<props>
<prop key="key1" >prop1</prop>
<prop key="key2" >prop2</prop>
</props>
</property>
<property name="map">
<map>
<entry>
<key>key1<key>
<value>value1</value>
</entry>
<entry>
<key>key2</key>
<value>vlaue2</value>
</entry>
</map>
</property>
AOP: 面向切面编程
aspect: 切面模块化的类。此类中包含了 advice 、pointcut
advice: 横切面关注点实现,为before 、 after 、 after returning 、 after throwing 、around等类型
pointcut: advice其作用的范围,通过相关表达式创建
jointpoint: advice起作用的点
weave: 将目标对象和横切面关注点代码组织的过程
introduce: 动态的将方法添加的目标对象里面
proxy: 代理类 ,兄弟代理 JDK Dynamic proxy、 父子代理 cglib
target: 代理模式的目标类
具体采用哪种方式,spring会自动选择,关键点: target是否实现了接口。JDK Dynamic proxy 、cglib
强制待用cglib方式需配置 :<aop:spring-autoproxy target-proxy-class="true"/>
spring支持annotation和配置文件方式挂历切面类:
annocaton:
@Aspect
@Pointcut("execution(* com.demo.manger.*.*(..))")
@Before("allAddMethod()")
@After("allAfterMethod()")
<aop:spring-autoproxy />启用spring对annotation的支持
XML配置方式:
<aop:config>
<aop:aspect id="xmlAspect" ref="" >
<aop:pointcut id="myPointcut" expressing="execution(* add*(..))" />
<aop:advice pointcut-ref="myPointcut" method="beforeAdd()" />
</aop:aspect>
</aop:config>
IOC:
管理对象之间的依赖关系和生命周期。将对象之间的依赖耦合关系抽取成模块,由IOC容器管理,提高程序的模块性和灵活性。
org.springframework.beans.factory.BeanFactory接口定义了对象管理的基本方法,包括创建对象,检查对象的属性、检查IOC容器是否包含该对象。是不是单例的。
对象之间的依赖关系通常通过XML文件配置好,交给spring的ioc容器管理。基本配置:
定义bean
<bean id="usedao" class="com.demo.dao.UserDAO" />
<bean name="userdao" class="com.demo.dao.UserDAO" />
其他属性:
scope : 定义对象的形态,可选值: singleton / prototype
depends-on:所依赖的对象,必须在本对象初始化之前初始化,其值为其他bean的ID值。
dependency-check:在注入属性后,是否执行对象属性的依赖检查,其值为:none / simple /object / all
autowire: 对象注入的方式,一般用byName / byType 两种方式。
lazy-init:延迟初始化,true 、 false
abstruct:定义是否为抽象类,此类可以不存在。
parent:java的继承概念的体现。用于指定对象的abstruct类
DI注入依赖:
setter方式:
<property name="atrributeName" value="aa" />
<property name="attributeName" ref="beanId" />
<pooperty name="attributeName">
<idref local="" /> //idref 和value 同义,用于将其他bean的id值作为一个字符串注入进来,local只是使id值
<idref bean="" /> //idref 和value 同义,用于将其他bean的id值作为一个字符串注入进来,bean可以使id或name值
<ref local="" /> //不能跨xml文件,只能指定其他bean的id值
<ref bean="" /> //跨xml文件 ,指定其他bean的id或name值
<ref parent="" /> //父容器bean的id或name值
</property>
constructor方式:
<constructor-arg index="0" ref="" /> // 将对应的bean注入到索引为0的构造函数中
<constructor-arg index="1" ref="" /> // 将对应的bean注入到索引为1的构造函数中
<constructor-arg type="java.lang.String" ref="" />// 根据类型匹配注入参数
<constructor-arg type="int" value="1233" /> // 根据类型注入参数
构造函数可以凭借索引和类型区别注入的参数,但spring推荐索引方式,这样可以避免由于类型冲突造成的错误
注入java.util.List / Map / Set 和 数组:
<property name="list" >
<list>
<value>234</value>
<ref bean="otherBean" />
</list>
</property>
<property name="set" >
<set>
<value>234</value>
<ref bean="otherBean" />
</set>
</property>
<property name="array" >
<list>
<value>234</value>
<ref bean="otherBean" />
</list>
</property>
<property name="properties" >
<props>
<prop key="key1" >prop1</prop>
<prop key="key2" >prop2</prop>
</props>
</property>
<property name="map">
<map>
<entry>
<key>key1<key>
<value>value1</value>
</entry>
<entry>
<key>key2</key>
<value>vlaue2</value>
</entry>
</map>
</property>
AOP: 面向切面编程
aspect: 切面模块化的类。此类中包含了 advice 、pointcut
advice: 横切面关注点实现,为before 、 after 、 after returning 、 after throwing 、around等类型
pointcut: advice其作用的范围,通过相关表达式创建
jointpoint: advice起作用的点
weave: 将目标对象和横切面关注点代码组织的过程
introduce: 动态的将方法添加的目标对象里面
proxy: 代理类 ,兄弟代理 JDK Dynamic proxy、 父子代理 cglib
target: 代理模式的目标类
具体采用哪种方式,spring会自动选择,关键点: target是否实现了接口。JDK Dynamic proxy 、cglib
强制待用cglib方式需配置 :<aop:spring-autoproxy target-proxy-class="true"/>
spring支持annotation和配置文件方式挂历切面类:
annocaton:
@Aspect
@Pointcut("execution(* com.demo.manger.*.*(..))")
@Before("allAddMethod()")
@After("allAfterMethod()")
<aop:spring-autoproxy />启用spring对annotation的支持
XML配置方式:
<aop:config>
<aop:aspect id="xmlAspect" ref="" >
<aop:pointcut id="myPointcut" expressing="execution(* add*(..))" />
<aop:advice pointcut-ref="myPointcut" method="beforeAdd()" />
</aop:aspect>
</aop:config>