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的装配. 有一些场景是我们不能用自动装配的,比如我们要给第三方库中的组件装配到我们的应用中,这时自动装配无效,因为自动装配只能扫描本应用 ...
随机推荐
- Redis搭建(五):Cluster集群搭建
一.方案 1. 介绍 redis3.0及以上版本实现,集群中至少应该有奇数个节点,所以至少有三个节点,官方推荐三主三从的配置方式 使用哈希槽的概念,Redis 集群有16384个哈希槽,每个key通过 ...
- https ssl
HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP下加入 ...
- Effective ObjectiveC 2.0 Note
[Effective ObjectiveC 2.0 Note] 1.The memory for objects is always allocated in heap space and never ...
- 【bzoj1602】[Usaco2008 Oct]牧场行走
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1793 Solved: 935[Submit][St ...
- libevent 源码深度剖析十三
libevent 源码深度剖析十三 —— libevent 信号处理注意点 前面讲到了 libevent 实现多线程的方法,然而在多线程的环境中注册信号事件,还是有一些情况需要小心处理,那就是不能在多 ...
- spring aop自动代理注解配置之二
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 安装CentOS 6网络配置问题
安装CentOS 6网络配置问题 今天决定把家中的CentOS从5升级至6.但安装完CentOS 6.2之后发现eth0没有像往常一样通过DHCP自动获取IP.打开“/etc/sysconfig/ne ...
- Debian上SCST的设置
一)创建SCST,无infiniband支持 1:载入最小化支持 #aptitude install build-essentials linux-headers checkinstall #wget ...
- Socket接口原理及用C#语言实现
首先从原理上解释一下采用Socket接口的网络通讯,这里以最常用的C/S模式作为范例,首先,服务端有一个进程(或多个进程)在指定的端口等待客户来连接,服务程序等待客户的连接信息,一旦连接上之后,就可以 ...
- Cannot resolve the collation conflict between "Chinese_PRC_CI_AS" and "SQL_L及由于排序规则不同导致查询结果为空的问题
报错:Cannot resolve the collation conflict between "Chinese_PRC_CI_AS" and "SQL_L 出错原因: ...