Spring学习笔记(三)—— 使用注解配置spring
一、使用步骤
1.1 导包

1.2 为主配置文件引入新的命名空间(约束)
在applicationContext.xml中引入context约束

1.3 编写相关的类
public class UserDaoImpl implements UserDao {
@Override
public void sayHello() {
System.out.println("Hello Spring...");
}
}
1.4 配置注解扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!-- 指定扫描cn.itcast.dao包下的所有类中的注解.
注意:扫描包时,会扫描指定包下的所有子孙包
-->
<context:component-scan base-package="cn.itcast.dao"></context:component-scan>
</beans>
1.5 在相关类上添加注解
@Component(value="userDao")
public class UserDaoImpl implements UserDao { @Override
public void sayHello() {
System.out.println("Hello Spring...");
} }
1.6 编写测试方法
@Test
public void demo1() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.sayHello();
}
二、Bean管理中常用的注解
2.1 @Component组件
该组件作用在类上,spring还提供了@Component的三个衍生注解(功能目前来讲是一致的)
- @Controller :WEB层
- @Service :业务层
- @Repository :持久层
这三个注解是为了让标注类本身的用途清晰。
/**
* 将对象注册到容器中
*/
@Repository("userDao")
// @Component(value="userDao")
// @Component("userDao")就相当于在xml中配置<bean name="userDao" class="cn.itcast.dao.impl.UserDaoImpl")
public class UserDaoImpl implements UserDao {
2.2 属性注入的注解
【值类型注入】@Value
- 方式1:通过反射的Field赋值(破坏了封装性)
@Value("18")
private Integer age; - 方式2:通过set方法赋值
@Value("lisi")
public void setName(String name) {
this.name = name;
}
【引用类型注入】
// @Autowired
// 问题:如果匹配多个类型一致的对象,将无法选择具体注入哪一个对象
// @Qualifier("car2") //使用@Qualifier注解告诉spring容器自动装配哪个名称的对象
@Resource(name="car")
private Car car;
- @Autowired:自动装配,默认按类型进行装配
- @Qualifier:强制使用名称注入
- @Resource:手动注入,指定注入哪个名称的对象,相当于@Autowired和@Qualifier一起用
2.3 Bean的作用范围注解@Scope
@Scope(scopeName="singleton")
- singleton:单例
- prototype:多例
2.4 Bean的生命周期配置
- @PostConstruct:在对象被创建后调用,相当于init-method
- @PreDestroy:在销毁之前调用,相当于destory-method
三、Bean管理的方式对比

XML和注解:
- XML:结构清晰
- 注解:开发方便(属性注入)
Spring学习笔记(三)—— 使用注解配置spring的更多相关文章
- spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)
Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...
- Spring学习笔记3——使用注解的方式完成注入对象中的效果
第一步:修改applicationContext.xml 添加<context:annotation-config/>表示告诉Spring要用注解的方式进行配置 <?xml vers ...
- Spring学习笔记(1)——初识Spring
一.Spring是什么 通常说的Spring其实指的是Spring Framework,它是Spring下的一个子项目,Spring围绕Spring Framework这个核心项目开发了大 ...
- Spring 学习笔记(四):Spring AOP
1 概述 本文主要讲述了AOP的基本概念以及在Spring中AOP的几种实现方式. 2 AOP AOP,即Aspect-Oriented Programming,面向切面编程,与OOP相辅相成.类似的 ...
- Spring学习笔记(二) 初探Spring
版权声明 笔记出自<Spring 开发指南>一书. Spring 初探 前面我们简单介绍了 Spring 的基本组件和功能,现在我们来看一个简单示例: Person接口Person接口定义 ...
- Spark学习笔记-三种属性配置详细说明【转】
相关资料:Spark属性配置 http://www.cnblogs.com/chengxin1982/p/4023111.html 本文出处:转载自过往记忆(http://www.iteblog.c ...
- Spring学习笔记(三):面向切面的Spring
Spring之面向切面编程 一.理解何为面向切面编程 对于这个的理解,我觉得Spring实战中的例子讲得很明白: 假设我现在是一个小区用户,每个月小区都要收电费,这时候就会来人查看电表,算出来这个月电 ...
- Spring学习笔记之三----基于Annotation的Spring IOC配置
使用Annotation 来创建Bean有两种方式 在配置类中创建bean(配置类是指标注为@Configuration的类),在配置类中每一个创建bean的方法都应该标注为@Bean,可以在@Bea ...
- Spring学习笔记(2)——Bean的配置
要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...
- Spring学习笔记(三)之装配Bean
除了组件扫描与自动装配之外还有基于Java代码的装配与基于XML的装配. 有一些场景是我们不能用自动装配的,比如我们要给第三方库中的组件装配到我们的应用中,这时自动装配无效,因为自动装配只能扫描本应用 ...
随机推荐
- ROS 不能安装 Ros Packages
我的linux版本是16.04,安装的是kinetic 1. E: Some index files failed to download. They have been ignored, or ol ...
- 基于 EntityFramework 的数据库主从读写分离架构 - 目录
基于 EntityFramework 的数据库主从读写分离架构 回到目录,完整代码请查看(https://github.com/cjw0511/NDF.Infrastructure)中的目 ...
- 41、OrthoMCL和mcl软件进行基因家族分析
转载:http://www.realbio.cn/news/124.html https://blog.csdn.net/seallama/article/details/43820763 http: ...
- 2.Hive的几种常见的数据导入方式
好久没写Hive的那些事了,今天开始写点吧.今天的话题是总结Hive的几种常见的数据导入方式,我总结为四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3 ...
- xgboost dmatrix中的 weight的重要性
https://stackoverflow.com/questions/35983565/how-is-the-parameter-weight-dmatrix-used-in-the-gradien ...
- 几种导入osm(openstreetmap)数据的方法
一osm2pgsql+postgresql+postgis osm2pgsql——是由OpenStreetMap开发的一个命令行工具负责将OSM数据导入到基于PostgresSql的Postgis的 ...
- Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法
Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法 摘自:https://blog.csdn.net/qq_31635851/article/details/ ...
- 制作3D旋转视频展示区
CSS3 3D变形制作视频展示区 <!doctype html> <html lang="en"> <head> <meta charse ...
- cgroup初步分析(1)
cgroup的功能和作用不废话,直说一下cgroup的几条设计准则,有了几条设计准则的约束,就比较容易理解其中的数据结构和函数,至于源代码cgroup.c,无非是两个内容,一是task_struct. ...
- 福大软工1816|K班—alpha冲刺
Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 了解前端方面的相关内容,便于后续对进 ...