xml配置
beans.xml
标签
<bean><bean>
属性:id 为类名,首字母小写 默认为全限定名
class 指定类
lazy-init 值:default :
true :延迟初始化
false :
init-method bean初始化方法
destroy-method 关闭打开资源
-----------------------------------------------------------------
注入
<property name="" ref=""></property>
属性:name 为类名,首字母小写
ref 指定bean
--------------------------------------------------------------
解耦
dao service 在servicebean中通过get、set方法注入dao
--------------------------------------------------------------
控制反转
配置beans.xml
通过bean注入
1、给dao添加bean
2、在personservice bean中添加propertype
3、使用ref指定bean并注入daobean
使用内部bean注入
1、在propertype添加bean并指定class
--------------------------------------------------------------
基本类型注入值
1、在servicebean中添加private 数据类型 变量名;
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名 value=""
--------------------------------------------------------------
注入集合
Set<数据类型>
1、在servicebean中添加private Set<数据类型> 变量名=new HashSet<数据类型>();
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名
4、添加set标签
5、在set标签下添加value并制定值
List<数据类型>
1、在servicebean中添加private List<数据类型> 变量名=new ArrayList<数据类型>();
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名
4、添加list标签
5、在list标签下添加value并制定值
private Properties属性类型注入
1、private 变量名=new Properties();
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名
4、添加props标签
5、在props标签下添加<prop key="value"></prop>并制定值
Map<数据类型,数据类型>
1、在servicebean中添加private Map<String,String> 变量名=new HashMap<String, String>()
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名
4、添加map标签
5、在map标签下添加<entry key="key" value="vlaue"/>并制定值
通过构造器注入值
1、在servicebean中添加private javabean 变量名;String 变量名;
2、给属性添加get、set方法
3、在beans.xml下的bean constructor-arg 指定index=0、1;
0:javabean 指定类型 为javabean ref="bean的id"
1:String 指定类型 为String value="value"
------------------------------------------------------------------------------
注解
使用注解方式注入
@Autowrited 按类型装配
按名称装配 @Autowired @Qualifier("bean id")
@Autowired(required="true") 必须注入值
@Resource 按名称装配、当名称找不到时按类型装配
按名称装配 @Resource(name="bean id")
使用时在beans.xml 中添加<context:annotation-config/>
用法:@Autowrited/@Resource 属性;
@Autowrited/@Resource setter(){}方法;
----------------------------------------------------------------
自动装配
在bean中添加标签属性
autowire="类型"
byType:按类型装配
byName:按名称装配
使用时bean id=属性名
constructor:应用于构造器参数
autodetect:如果发现是默认构造器、将使用byType方式
----------------------------------------------------------------
将类交给spring管理
扫描com包下的类
<context:component-scan base-package="com">
在class前添加注解
@Compoent @Scope("作用域")
标注通用组件
@Service("bean") @Scope("作用域")
标注业务层组件
@Controller @Scope("作用域")
标注控制层组件 (比如struts中的action)
@Repository @Scope("作用域")
标注数据访问组件,即DAO组件
@PostConstruct 指定初始化的方法
@PreDestroy 指定销毁方法
AOP编程
Aspect(切面)、Joinpoint(连接点)、Pointcut(切入点)、Advice(通知)、target(目标对象)、Weave(织入)、Introduction(引入)
权限拦截
aop代理对象:
静态代理
动态代理
Proxy类 创建代理对象
Advice(通知)
advice 前置通知 @Before
afteradvice 后置通知 @AfterReturning
exceptionadvice 例外通知 @AfterThrowing
finallyadvice 最终通知 @After
Aroundadvice 环绕通知 @Around
SPRING AOP开发
添加命名空间
xmlns:aop="http://www.springframework.org/schema/aop
ttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
基于XML配置方式进行AOP开发
把切面交给sprig管理
< bean id="拦截器" class=""/>
<aop:config>
<aop:config>
<aop:aspect id="" ref="">
<aop:pointcut id="切入点" expression="execution(拦截类型 包名2.包名2.bean.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
基于注解方式进行AOP开发
<aop:aspectj-autoproxy/>
Interceptor 拦截器
<bean id="" class=""/>
在拦截器里面的方法前添加相应的注解
Advice(通知)
advice 前置通知 @Before
afteradvice 后置通知 @AfterReturning
exceptionadvice 例外通知 @AfterThrowing
finallyadvice 最终通知 @After
Aroundadvice 环绕通知 @Around
SPRING JDBC 开发
1、配置数据源
使用属性文件jdbc.properties
扫描属性文件
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}"/>
</bean>
2、基于注解声明事务
基于XML声明事务
事务传播属性
Required 创建事务
Not_Supported 不创建事务
RequireShew 不管是否存在事务,都会创建一个事务
Mandatory 指定该业务方法只能在已存在的事务中执行
Supports 如果业务方法在摸个事务范围内被调用,则方法成为该事务的一部分
Never 该业务方法没有关联事务时 才能正常执行
Nested 如果一个活动的事务存在,则运行在一个嵌套的事务中,如果没有事务 则按Required属性执行
数据库系统提供的事务隔离
Read Uncommited :读未提交数据
Read Commited :读已提交数据
Repeatable Read :可重复读
Serializable : 串行化
beans.xml
标签
<bean><bean>
属性:id 为类名,首字母小写 默认为全限定名
class 指定类
lazy-init 值:default :
true :延迟初始化
false :
init-method bean初始化方法
destroy-method 关闭打开资源
-----------------------------------------------------------------
注入
<property name="" ref=""></property>
属性:name 为类名,首字母小写
ref 指定bean
--------------------------------------------------------------
解耦
dao service 在servicebean中通过get、set方法注入dao
--------------------------------------------------------------
控制反转
配置beans.xml
通过bean注入
1、给dao添加bean
2、在personservice bean中添加propertype
3、使用ref指定bean并注入daobean
使用内部bean注入
1、在propertype添加bean并指定class
--------------------------------------------------------------
基本类型注入值
1、在servicebean中添加private 数据类型 变量名;
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名 value=""
--------------------------------------------------------------
注入集合
Set<数据类型>
1、在servicebean中添加private Set<数据类型> 变量名=new HashSet<数据类型>();
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名
4、添加set标签
5、在set标签下添加value并制定值
List<数据类型>
1、在servicebean中添加private List<数据类型> 变量名=new ArrayList<数据类型>();
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名
4、添加list标签
5、在list标签下添加value并制定值
private Properties属性类型注入
1、private 变量名=new Properties();
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名
4、添加props标签
5、在props标签下添加<prop key="value"></prop>并制定值
Map<数据类型,数据类型>
1、在servicebean中添加private Map<String,String> 变量名=new HashMap<String, String>()
2、给属性添加get、set方法
3、在beans.xml下的bean propertype 指定name=变量名
4、添加map标签
5、在map标签下添加<entry key="key" value="vlaue"/>并制定值
通过构造器注入值
1、在servicebean中添加private javabean 变量名;String 变量名;
2、给属性添加get、set方法
3、在beans.xml下的bean constructor-arg 指定index=0、1;
0:javabean 指定类型 为javabean ref="bean的id"
1:String 指定类型 为String value="value"
------------------------------------------------------------------------------
注解
使用注解方式注入
@Autowrited 按类型装配
按名称装配 @Autowired @Qualifier("bean id")
@Autowired(required="true") 必须注入值
@Resource 按名称装配、当名称找不到时按类型装配
按名称装配 @Resource(name="bean id")
使用时在beans.xml 中添加<context:annotation-config/>
用法:@Autowrited/@Resource 属性;
@Autowrited/@Resource setter(){}方法;
----------------------------------------------------------------
自动装配
在bean中添加标签属性
autowire="类型"
byType:按类型装配
byName:按名称装配
使用时bean id=属性名
constructor:应用于构造器参数
autodetect:如果发现是默认构造器、将使用byType方式
----------------------------------------------------------------
将类交给spring管理
扫描com包下的类
<context:component-scan base-package="com">
在class前添加注解
@Compoent @Scope("作用域")
标注通用组件
@Service("bean") @Scope("作用域")
标注业务层组件
@Controller @Scope("作用域")
标注控制层组件 (比如struts中的action)
@Repository @Scope("作用域")
标注数据访问组件,即DAO组件
@PostConstruct 指定初始化的方法
@PreDestroy 指定销毁方法
AOP编程
Aspect(切面)、Joinpoint(连接点)、Pointcut(切入点)、Advice(通知)、target(目标对象)、Weave(织入)、Introduction(引入)
权限拦截
aop代理对象:
静态代理
动态代理
Proxy类 创建代理对象
Advice(通知)
advice 前置通知 @Before
afteradvice 后置通知 @AfterReturning
exceptionadvice 例外通知 @AfterThrowing
finallyadvice 最终通知 @After
Aroundadvice 环绕通知 @Around
SPRING AOP开发
添加命名空间
xmlns:aop="http://www.springframework.org/schema/aop
ttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
基于XML配置方式进行AOP开发
把切面交给sprig管理
< bean id="拦截器" class=""/>
<aop:config>
<aop:config>
<aop:aspect id="" ref="">
<aop:pointcut id="切入点" expression="execution(拦截类型 包名2.包名2.bean.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
<aop:after pointcut-ref="mycut" method="doAfter"/>
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
基于注解方式进行AOP开发
<aop:aspectj-autoproxy/>
Interceptor 拦截器
<bean id="" class=""/>
在拦截器里面的方法前添加相应的注解
Advice(通知)
advice 前置通知 @Before
afteradvice 后置通知 @AfterReturning
exceptionadvice 例外通知 @AfterThrowing
finallyadvice 最终通知 @After
Aroundadvice 环绕通知 @Around
SPRING JDBC 开发
1、配置数据源
使用属性文件jdbc.properties
扫描属性文件
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}"/>
</bean>
2、基于注解声明事务
基于XML声明事务
事务传播属性
Required 创建事务
Not_Supported 不创建事务
RequireShew 不管是否存在事务,都会创建一个事务
Mandatory 指定该业务方法只能在已存在的事务中执行
Supports 如果业务方法在摸个事务范围内被调用,则方法成为该事务的一部分
Never 该业务方法没有关联事务时 才能正常执行
Nested 如果一个活动的事务存在,则运行在一个嵌套的事务中,如果没有事务 则按Required属性执行
数据库系统提供的事务隔离
Read Uncommited :读未提交数据
Read Commited :读已提交数据
Repeatable Read :可重复读
Serializable : 串行化