Spring学习笔记

[1]搭建环境

1.添加jar包

使用spring需要

sring.jar
commons-loggin.jar

如果使用aop组件需要

aspectjweaver.jar
aspectjrt.jar

如果使用jsr-250的注解需要

commom-annotations.jar

2.添加bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

</beans>


3.测试
@Test
public void instanceSpring() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}

4.运行。如果通过说明Spring环境搭建好了

[2]添加一个bean

添加一个bean就相当于定义一个组件,这个组件是用于具体实现某个功能的。
这里的所定义的bean就相当于给了你一个简洁方便的方法来调用这个组件实现你要完成的功能。
id属性:相当于定义了你的这个bean的别名,如果bean别名需要用到id不支持的字符(/user/list)可以使用 name代替

class属性:就是你这个功能所对应的类。该bean的类路径

scope属性 : 默认为singleton,即单实例模式,每次getBean(“id”)时获取的都是同一个实例,如果设置为prototype,即原型模式,则每次获取的是新创建的实例。

lazy-init属性:lazy-init="false" 立退加载, 表示spring启动时,立刻进行实例化。为true时,延迟实例化。(lazy-init 设置只对scop属性为singleton的bean起作用)

Init-method : 在bean实例化后要调用的方法(bean里定义好的方法)。

Destroy-method : bean从容器里删除之前要调用的方法。

Autowire : 其属性要通过何种方法进行属性的自动装配。

对于上述的各个属性,id和class是必要的,其他的则可以省略。例如如果设置了autowire的值,则表明需要自动装配,否则是手动装配。
eg:
<bean id="personService" class="cn.soldier.service.impl.PersonServiceBean" scope="singleton" lazy-init="false" init-method="init" destroy-method="destroy" />

[3]对象注入到组件bean中(控制反转)
有三种方法可以实现:
第一种:
通过构造器参数,让容器把创建好的依赖对象进行注入
1.PersonServiceBeanc.ass
private PersonDao personDao2;
private String name2;
//省略getter和setter
public PersonServiceBean(PersonDao personDao2, String name2) {
this.personDao2 = personDao2;
this.name2 = name2;
}
2.beans.xml
<bean id="personDao" class="cn.soldier.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.soldier.service.impl.PersonServiceBean">
<constructor-arg index="0" type="cn.soldier.dao.PersonDao" ref="personDao" />
<constructor-arg index="1" value="使用构造器方式将基本类型(String):【呵呵】注入到bean中" />
</bean>

----------------------------------------------------------------------------------------------------------------------
第二种


使用setter方法进行注入
PersonServiceBean.class
1.PersonServiceBean.class组件定义属性getter setter
private PersonDao personDao;
//省略getter和setter
2.调用对象的方法
personDao.xxxMethod();
3.配置bean

<bean id="personService" class="cn.soldier.service.impl.PersonServiceBean">
<property name="personDao" ref="personDaoAA"></property>
</bean>
<bean id="personDaoAA" class="cn.soldier.dao.impl.PersonDaoBean"></bean>


3.1方式二:(使用内部bean,但该bean不能够被其他备案使用)

<bean id="personService" class="cn.soldier.service.impl.PersonServiceBean">
<property name="personDao" >
<bean class="cn.soldier.dao.impl.PersonDaoBean" />
</property>
</bean>

-->将基本类型注入到bean中
1.提供getter,setter
PersonServiceBean.clsss
private String name;
//省略getter和setter
beans.xml
<bean id="personService" class="cn.soldier.service.impl.PersonServiceBean">
<property name="name" value="将基本类型(String):【呵呵】注入到bean中"></property>
</bean>

-->将集合注入到bean中
private Set<String> sets = new HashSet<String>();
private List<String> lists = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String, String> maps = new HashMap<String, String>();
//省略getter和setter
<property name="sets">
<set>
<value>呵呵呵1-set</value>
<value>呵呵呵2-set</value>
<value>呵呵呵3-set</value>
<value>呵呵呵4-set</value>
</set>
</property>
<property name="lists">
<list>
<value>呵呵呵1-list</value>
<value>呵呵呵2-list</value>
<value>呵呵呵3-list</value>
<value>呵呵呵4-list</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">呵呵呵1-properties</prop>
<prop key="key2">呵呵呵2-properties</prop>
<prop key="key3">呵呵呵3-properties</prop>
<prop key="key4">呵呵呵4-properties</prop>
</props>
</property>
<property name="maps">

<map>
<entry key="map-k-1" value="map-v-1" />
<entry key="map-k-2" value="map-v-2" />
<entry key="map-k-3" value="map-v-3" />
</map>
</property>
----------------------------------------------------------------------------------------------------------------------
第三种使用field方法(注解)
注入依赖对象

注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,
因为自动装配会产生未知情况,开发人员无法预见最终装配结果

1.手工装配依赖对象

在java代码中使用 [@Autowired] 、 [@Resource] 方式进行装配,这两个注释的区别是
@Autowired 默认按类型装配
@Resource 默认使用安名称装配,当找不到与名称匹配的bean时才会安类型转配

@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在。
如果允许null值,可以设置它的required属性为false。
如果想使用按名称装配,可以结合 [@Qualifier] 注解一起使用.
例子如下:
@Autowired @Qualifier["personDaoBean"]
private PersonDao personDao;


1.beans.xml
<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
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="personDaoCC" class="cn.soldier.dao.impl.PersonDaoBean"></bean>

</beans>
PersonServiceBean.class
使用
@Resource(name = "personDaoCC")
private PersonDao personDao3;
或者使用
private PersonDao personDao3;
@Resource //使用field方法的属性注入
public void setPersonDaoCC(PersonDao personDao3) {
this.personDao3 = personDao3;
}
或者使用
@Autowired(required=false)|@Qualifier("personDaoCC")
private PersonDao personDao3;


2.自动装配依赖对象
1.bean.xml
<bean id="personDao" class="cn.soldier.dao.impl.PersonDaoBean" />
<bean id="personService" class="cn.soldier.service.impl.PersonServiceBean" autowire="byType" />
2.PersonServiceBean.class
private PersonDao personDao;
//省略setter
personDao.add();

autowire属性取值如下:
byType属性:按类型匹配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。如果发现多个,那么将会抛出异常。如果没有找到,那么属性值为null。
byName属性:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean。如果没有找到,那么属性值为null。
constructor属性:constructor与byType方式相似,不同之处在于它应用构造器参数。如果没有找到与构造器参数类型一直的bean,那么将会抛出异常。
autodetect属性:通过bean类的自省机制(introspection)来决定使用constructor还是byType方式进行自动装配,<!--如果发现有默认的构造器,那么将使用byType方式-->

[4]通过在classpath自动扫描方式把组件纳入spring容器中管理

Spring2.5引入了自动扫描机制,它可以在类路径下寻找标注了 @Component @Service @Controller @Repository 注解的类,并把这些类纳入进spring
容器中管理。

@Service 用于标志业务层组件
@Controller 用于标注控制层组件(如struts的action)
@Repository 用于标注数据访问组件 即DAO层
@Component 泛指组件,当组件不好归类的时候,可以使用这个注释进行标注

1.beans.xml
<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
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="cn.soldier" />
<!--扫描包及子包下的所有类。同时包含了field处理器 <context:annotation-config /> -->

</beans>

@Service("xxxxx") //默认为类的简单名称,这里手动修改为xxxxx
public class PersonServiceBean implements PersonService {
...
}
@Service @Scope("prototype")//作用域改为原型
public class PersonServiceBean implements PersonService {
...
}

@Service
public class PersonServiceBean implements PersonService {
@PostConstruct
public void init(){
System.out.println("inint");//初始化启动
}
@PreDestroy
public void distory() {
System.out.println("distory");//注销是启动
}
}

[5] aop
Spring 提供了两种面向切面使用方式,实际工作中我们可以选用其中一种,
1.基于XML配置方式的AOP开发

2.基于注解方式的AOP开发

1.beans.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--通过在classpath自动扫描方式把组件纳入spring容器中管理 -->
<context:component-scan base-package="cn.soldier" />
<aop:aspectj-autoproxy /><!--启用@AspertJ注解的支持 -->
</beans>

2.定义定义切入点,,定义通知,
MyInterceptor.class
@Aspect
@Component
public class MyInterceptor {
@Pointcut("execution(* cn.soldier.service.impl.PersonServiceBean.*(..))")
// 定义切入点
private void anyMethod() {
System.out.println("MyInterceptor.anyMethod()");
}

// 定义前置通知
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name) {
System.out.println("MyInterceptor.doAccessCheck() 前置通知 ");
System.out.println("打印传进参数 " + name);
}

// 定义后置通知
@AfterReturning(pointcut = "anyMethod()", returning = "result")
public void doReturnCheek(String result) {
System.out.println("MyInterceptor.doReturnCheek() 后置通知 ");
System.out.println("打印执行函数后的返回结果 " + result);
}

// 定义例外通知
@AfterThrowing(pointcut = "anyMethod()", throwing = "ex")
public void doExceptionAction(Exception ex) {
System.out
.println("MyInterceptor.doExceptionAction() doExceptionAction");
ex.printStackTrace();
}

// 定义最终通知
@After("anyMethod()")
public void doReleaseAction() {
System.out.println("MyInterceptor.doReleaseAction() 最终通知");
}

// 环绕通知
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// if(){//判断用户是否有权限
System.out.println("-环绕通知-执行开始--");
Object result = pjp.proceed();
System.out.println("-环绕通知-执行结束--");
// }
return result;
}
}

[6] spring集成JDBC
1.添加jar文件
使用spring需要
sring.jar
commons-loggin.jar
如果使用aop组件
需要 aspectjweaver.jar
aspectjrt.jar
如果使用jsr-250的注解
需要
commom-annotations.jar
使用数据源
需要
commons-pool.jar
commons-dbcp.jar
使用mysql数据库需要
mysql-connector-java-5.1.7-bin.jar


2.配置数据源
<!--导入外部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>


3.配置事务管理
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

4.注启用事务组件
方法一:注解方式
1.启用@Transaction注解的支持
1.1添加tx命名空间
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
1.2启用@Transaction注解的支持
<!--启用@Transaction注解的支持--->
<tx:annotation-driven transaction-manager="txManager"/>


[6] spring使用注解方式管理事务

//@Transactional(rollbackFor=Exception.class) 回滚
//@Transactional(noRollbackFor=RuntimeException.class) 不回滚
public void delete(Integer personid) throws Exception{
jdbcTemplate.update(//
"delete from person where id=?"//
, new Object[] { personid }//
, new int[] { java.sql.Types.INTEGER });
throw new RuntimeException("运行期意外");
//如果throw的是运行期意外,事务回滚. 因为RuntimeException是一个unchecked异常,
//如果是checked异常的话,事务不会回滚
}
//事务的传播属性
@Transactional(propagation = Propagation.NOT_SUPPORTED)//指定不开启事务行为
public Person getPerson(Integer personid) {
return (Person) jdbcTemplate.queryForObject(//
"select * from person where id=?"//
, new Object[] { personid }//
, new int[] { java.sql.Types.INTEGER }//
, new PersonRowMapper());
}
@Transactional(propagation=Propagation.REQUIRED)//默认行为,表示方法需要在一个事务中运行。
public void save(Person person) {
jdbcTemplate.update(//
"insert into person (name) values(?)"//
, new Object[] { person.getName() }//
, new int[] { java.sql.Types.VARCHAR });
}
//数据库的事务隔离级别

数据库系统提供了四种事务隔离级别供用户选择。

Read Uncommited 读未提交数据(会出现脏读,不可重复读和幻读)
Read Commited 读已提交的数据(会出现不可重复读和幻读)
Repeatable Read 可重复读(会出现幻读)
Serializble 串行化

脏读:就是一个事务读取到另一个事务为提交的更新数据。
不可重复读:就是在一个事务中,多次读取同一数据返回的结果有所不同。换句话说就是,后续读取可以读到另一事务已提交的数据。
可重复读:就是在同一个事务中,多次读取数据时,能够保证所读取数据时一样的,也就是后续读取不能读到另一事务已提交的更新数据。
幻读:就是一个事务读取到另一事务已提交的insert数据。

原文地址:https://www.cnblogs.com/lhy_2011/p/4036311.html