spring学习笔记(转)

本文详细介绍Spring框架的基础配置、Bean管理、依赖注入、AOP、事务处理等核心功能,并提供了多种配置示例,包括XML和注解方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[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数据。

基于数据挖掘的音乐推荐系统设计与实现 需要一个代码说明,不需要论文 采用python语言,django框架,mysql数据库开发 编程环境:pycharm,mysql8.0 系统分为前台+后台模式开发 网站前台: 用户注册, 登录 搜索音乐,音乐欣赏(可以在线进行播放) 用户登陆时选择相关感兴趣的音乐风格 音乐收藏 音乐推荐算法:(重点) 本课题需要大量用户行为(如播放记录、收藏列表)、音乐特征(如音频特征、歌曲元数据)等数据 (1)根据用户之间相似性或关联性,给一个用户推荐与其相似或有关联的其他用户所感兴趣的音乐; (2)根据音乐之间的相似性或关联性,给一个用户推荐与其感兴趣的音乐相似或有关联的其他音乐。 基于用户的推荐和基于物品的推荐 其中基于用户的推荐是基于用户的相似度找出相似相似用户,然后向目标用户推荐其相似用户喜欢的东西(和你类似的人也喜欢**东西); 而基于物品的推荐是基于物品的相似度找出相似的物品做推荐(喜欢该音乐的人还喜欢了**音乐); 管理员 管理员信息管理 注册用户管理,审核 音乐爬虫(爬虫方式爬取网站音乐数据) 音乐信息管理(上传歌曲MP3,以便前台播放) 音乐收藏管理 用户 用户资料修改 我的音乐收藏 完整前后端源码,部署后可正常运行! 环境说明 开发语言:python后端 python版本:3.7 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:pycharm
MPU6050是一款广泛应用在无人机、机器人和运动设备中的六轴姿态传感器,它集成了三轴陀螺仪和三轴加速度计。这款传感器能够实时监测并提供设备的角速度和线性加速度数据,对于理解物体的动态运动状态至关重要。在Arduino平台上,通过特定的库文件可以方便地与MPU6050进行通信,获取并解析传感器数据。 `MPU6050.cpp`和`MPU6050.h`是Arduino库的关键组成部分。`MPU6050.h`是头文件,包含了定义传感器接口和函数声明。它定义了类`MPU6050`,该类包含了初始化传感器、取数据等方法。例如,`begin()`函数用于设置传感器的工作模式和I2C地址,`getAcceleration()`和`getGyroscope()`则分别用于获取加速度和角速度数据。 在Arduino项目中,首先需要包含`MPU6050.h`头文件,然后创建`MPU6050`对象,并调用`begin()`函数初始化传感器。之后,可以通过循环调用`getAcceleration()`和`getGyroscope()`来不断更新传感器数。为了处理这些原始数据,通常还需要进行校准和滤波,以消除噪声和漂移。 I2C通信协议是MPU6050与Arduino交互的基础,它是一种低引脚数的串行通信协议,允许多个设备共享一对数据线。Arduino板上的Wire库提供了I2C通信的底层支持,使得用户无需深入了解通信细节,就能方便地与MPU6050交互。 MPU6050传感器的数据包括加速度(X、Y、Z轴)和角速度(同样为X、Y、Z轴)。加速度数据可以用来计算物体的静态位置和动态运动,而角速度数据则能反映物体动的速度。结合这两个数据,可以进一步计算出物体的姿态(如角度和角速度变化)。 在嵌入式开发领域,特别是使用STM32微控制器时,也可以找到类似的库来驱动MPU6050。STM32通常具有更强大的处理能力和更多的GPIO口,可以实现更复杂的控制算法。然而,基本的传感器操作流程和数据处理原理与Arduino平台相似。 在实际应用中,除了基本的传感器取,还可能涉及到温度补偿、低功耗模式设置、DMP(数字运动处理器)功能的利用等高级特性。DMP可以帮助处理传感器数据,实现更高级的运动估计,减轻主控制器的计算负担。 MPU6050是一个强大的六轴传感器,广泛应用于各种需要实时运动追踪的项目中。通过 Arduino 或 STM32 的库文件,开发者可以轻松地与传感器交互,获取并处理数据,实现各种创新应用。博客和其他开源资源是学习和解决问题的重要途径,通过这些资源,开发者可以获得关于MPU6050的详细信息和实践指南
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值