spring注解详解

1.准备工作
(1)导入common-annotations.jar
(2)导入schema文件 文件名为spring-context-2.5.xsd
(3)在xml的beans节点中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans
.......
xmlns:context="http://www.springframework.org/schema/context "

xsi:schemaLocation="
.......
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd " >

.....
<!--将针对注解的处理器配置好 -->
<context:annotation-config />
.....
<beans>

2.在java代码中使用@Autowired或@Resource注解方式进行装配 ,这两个注解的区别是:
@Autowired默认按类型装配,@Resource默认按名称装配,当找不到名称匹配的bean才会按类型装配。

默认注解
@Resource  private PersonDao persondao;
<bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>
首先是判断persondao是否与xml里的personDao名字相同,相同则注入,
不同则判断persondao是否是com.hf.dao.impl.PersonDaoBean类型,是则注入不是则返回null.

@Resource(name="personDao")  private PersonDao dao;
<bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>

判断name名称是否与bean中id相同不同则返回null

Spring自带注解方式
@Autowired @Qualifier("personDao") private PersonDao persondao;
默认是按类型注入 加上@Qualifier("personDao")则按名称注入

3.通过在classpath 自动扫描方式把组件纳入spring容器中管理
spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component @Service @Controller @Repository
注解的类,并将这些类纳入进spring容器中管理。它们的作用和xml文件中使用bean 节点配饰组件是一样的。
(1)使用到了注解的功能(需要注解准备工作的内容)
(2)在xml中加入
<context:component-scan base-package="com.hf" />
其中base-package 为需要扫描的包(包含子包)
(3)
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件 ,即DAO 组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

(4)
业务类
@Service
public class PersonServiceBean implements PersonService {.....}
输出类
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService= (PersonService)cxt.getBean("personServiceBean");
System.out.println(personService);
cxt.close(););

使用注解中bean的id默认名称为类名称的首字母小写名称
--------------------------------------------------
自己指定名称
@Service("aa") //默认作用域范围 是单例范围
public class PersonServiceBean implements PersonService {.....}
输出类
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService= (PersonService)cxt.getBean("aa");
System.out.println(personService);
cxt.close();
--------------------------------------------------
@Service("aa") @Scope("prototype")//修改bean的作用域
public class PersonServiceBean implements PersonService {....}
-----------------------------------------------------------
@PostConstruct 
public void init(){
System.out.println("初始化");
}
@PreDestroy
public void destory(){
System.out.println("释放资源");
}


4.AOP注解方式
(1)准备工作:
.导入common-annotations.jar aspectjrt.jar aspectweaver.jar cglib-nodep-2.13.jar
.导入schema文件 文件名为spring-aop-2.0.xsd
.在xml的beans节点中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans
.......
xmlns:aop="http://www.springframework.org/schema/aop "

xsi:schemaLocation="
.......
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " >

.....
<!-- 配置解释处理器 为@AspectJ注解提供支持 -->
<aop:aspectj-autoproxy />
.....
<beans>

(2)
<bean id="myInterceptor" class="com.hf.service.impl.MyInterceptor"></bean>
<bean id="personService" class="com.hf.service.impl.PersonServiceBean" ></bean>
将切面和被拦截的类交给spring管理
(3)切面类

@Aspect //定义切面类
public class MyInterceptor {
/**
* @Pointcut("execution(* com.hf.service..*.*(..))")表达式含义
* 第一个* 表示返回值类型为任意类型
* com.hf.service.. 两个点表示包路径下的子包的类也要拦截
* com.hf.service..*.* 子包的所有类中的所有方法 第一个*是方法第二个*是类
* (..)代表方法参数随意 可有可无可多可少
* **/

@Pointcut("execution (* com.hf.service.impl.PersonServiceBean.*(..))")// 定义切入点
private void andMethod()//声明一个切入点
{}

/* @Before("andMethod()")
public void doAccessCheck(){
System.out.println("前置通知");
}
*/

@Before("andMethod() && args(name)") //带参数 只拦截符合参数类型的方法
public void doAccessCheck(String name){
System.out.println("前置通知"+name);
}

/* @AfterReturning("andMethod()")
public void doFaterReturning(){
System.out.println("后置通知");
}*/

@AfterReturning(pointcut="andMethod()",returning="result")//带返回值的 无返回值的方法 result为null
public void doFaterReturning(String result){//拦截方法执行后 获取返回值对象
System.out.println("后置通知:"+result);
}

@After("andMethod()")
public void doAfter(){
System.out.println("最终通知");
}

/* @AfterThrowing("andMethod()")
public void doAfterThrowing(){
System.out.println("例外通知");
}*/

@AfterThrowing(pointcut="andMethod()" , throwing="e") //获取例外并打印
public void doAfterThrowing(Exception e){
System.out.println("例外通知:"+e);
}

@Around("andMethod()")//环绕通知
public Object doBasecProfiling(ProceedingJoinPoint pjp )throws Throwable{
//if(){//判断是否与权限
System.out.println("进入通知");
Object result = pjp.proceed();
System.out.println("离开 通知");
//}
return result;

}
}

(4)业务类 PersonServiceBean
public class PersonServiceBean implements PersonService {

public void save(String name){
throw new RuntimeException("纯属例外");
// System.out.println("我是Save方法"+name);
}
public String update() {
return "我是update方法";
}
}
内容概要:本文介绍了一套针对智能穿戴设备的跑步/骑行轨迹记录系统实战方案,旨在解决传统运动APP存在的定位漂移、数据断层和路径分析单一等问题。系统基于北斗+GPS双模定位、惯性测量单元(IMU)和海拔传感器,实现高精度轨迹采集,并通过卡尔曼滤波算法修正定位误差,在信号弱环境下利用惯性导航补位,确保轨迹连续性。系统支持跑步与骑行两种场景的差异化功能,包括实时轨迹记录、多维度路径分析(如配速、坡度、能耗)、数据可视化(地图标注、曲线图、3D回放)、异常提醒及智能优化建议,并可通过蓝牙/Wi-Fi同步数据至手机APP,支持社交分享与专业软件导出。技术架构涵盖硬件层、设备端与手机端软件层以及云端数据存储,强调低功耗设计与用户体验优化。经过实测验证,系统在定位精度、续航能力和场景识别准确率方面均达到预期指标,具备良好的实用性和扩展性。; 适合人群:具备一定嵌入式开发或移动应用开发经验,熟悉物联网、传感器融合与数据可视化的技术人员,尤其是从事智能穿戴设备、运动健康类产品研发的工程师和产品经理;也适合高校相关专业学生作为项目实践参考。; 使用场景及目标:① 开发高精度运动轨迹记录功能,解决GPS漂移与断点问题;② 实现跑步与骑行场景下的差异化数据分析与个性化反馈;③ 构建完整的“终端采集-手机展示-云端存储”系统闭环,支持社交互动与商业拓展;④ 掌握低功耗优化、多源数据融合、动态功耗调节等关键技术在穿戴设备中的落地应用。; 阅读建议:此资源以真实项目为导向,不仅提供详细的技术实现路径,还包含硬件选型、测试验证与商业扩展思路,建议读者结合自身开发环境,逐步实现各模块功能,重点关注定位优化算法、功耗控制策略与跨平台数据同步机制的设计与调优。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值