浅谈Spring框架注解的用法分析
1.@Component是Spring定义的一个通用注解,可以注解任何bean。
2.@Scope定义bean的作用域,其默认作用域是”singleton”,除此之外还有prototype,request,session和global session。
案例:@Component和@Scope用法分析:
BeanAnnotation类:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Scope
@Component
public class BeanAnnotation {
public void say(String arg) {
System.out.println( "BeanAnnotation : " + arg);
}
public void myHashCode() {
System.out.println( "BeanAnnotation : " + this .hashCode());
}
}
|
junit4测试类→TestBeanAnnotation类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@RunWith (BlockJUnit4ClassRunner. class )
public class TestBeanAnnotation extends UnitTestBase {
public TestBeanAnnotation() {
super ( "classpath*:spring-beanannotation.xml" );
}
@Test
public void testSay() {
BeanAnnotation bean = super .getBean( "beanAnnotation" );
bean.say( "This is test." );
}
@Test
public void testScpoe() {
BeanAnnotation bean = super .getBean( "beanAnnotation" );
bean.myHashCode();
bean = super .getBean( "beanAnnotation" );
bean.myHashCode();
}
}
|
Spring配置文件→spring-beanannotation.xml:
1
|
< context:component-scan base-package = "com.beanannotation" ></ context:component-scan >
|
我们先从Spring配置文件分析,base-package="com.beanannotation"
说明我们只处理这个包名下面的注解。
然后分析BeanAnnotation类,有一个say的方法。假设我们不清楚这是一个什么类型(注:Service或者DAO)的类,我们可以用一个通用的注解@Component。
最后分析TestBeanAnnotation类,testSay方法里super.getBean("beanAnnotation")
是从IOC的容器中取到这个bean,并调用bean的say方法。
提出问题的时间到了,当我们super.getBean的时候是通过bean的id从IOC容器中获取的,那么这个id是什么呢?因为在我们添加@Component到BeanAnnotation类上的时候,默认的id为beanAnnotation。如果指定了@Component的名称,譬如指定为@Component(”bean”)
的时候,在单元测试的时候就必须把super.getBean得到的id与之相对应才能测试成功。
在这里我把@Scope注解单独分离出来分析,在TestBeanAnnotation类里面有一个testScpoe方法。在BeanAnnotation类里面有一个myHashCode方法,可能大家有些疑惑,为什么要用this.hashCode()?因为@Scope指定的是bean的作用域,为了保证测试类的结果准确明了,所以采用哈希码值来判断是否为同一个对象。
3.@Repository、@Service、@Controller是更具有针对性的注解。
PS:这里我们需要明白这三个注解是基于@Component定义的注解哦:
①、@Repository通常用于注解DAO类,也就是我们常说的持久层。
②、@Service通常用于注解Service类,也就是服务层。
③、@Controller通常用于Controller类,也就是控制层(MVC)。
4.@Autowired理解为“传统”的setter方法,可以用在setter方法上,也可以用在构造器或者成员变量,能够进行Spring Bean的自动装配。
案例:@Autowired用法分析一:
Spring配置文件→spring-beanannotation.xml:
1
|
< context:component-scan base-package = "com.beanannotation" ></ context:component-scan >
|
SimpleMovieLister类:
1
2
3
4
5
6
7
8
9
10
|
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired (required= false )
public void setMovieFinder(MovieFinder movieFinder) {
this .movieFinder = movieFinder;
}
}
|
在默认的情况下,如果找不到合适的bean将会导致autowiring失败抛出异常,我们可以将@Autowired注解在这个set方法上,标记required=false来避免。但是,这不是一个必须的,如果找不到movieFinder的实例,是不会抛出异常的,只有在使用的时候发现movieFinder为null,在这种情况下,就要求我们在使用的时候,首先判断movieFinder是不是为null,如果是就会报空指针异常 。
值得注意的是,我们知道每个类可以有很多个构造器,但是在使用@Autowired的时候,有且只能有一个构造器能够被标记为required=true(注:required的默认值为false)。
案例:@Autowired用法分析二:
BeanImplOne类:
1
2
3
4
5
|
@Order
@Component
public class BeanImplOne implements BeanInterface {
}
|
BeanImplTwo类:
1
2
3
4
5
|
@Order
@Component
public class BeanImplTwo implements BeanInterface {
}
|
BeanInterface类:
1
2
3
|
public interface BeanInterface {
}
|
BeanInvoker类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@Component
public class BeanInvoker {
@Autowired
private List<BeanInterface> list;
@Autowired
private Map<String, BeanInterface> map;
public void say() {
if ( null != list && 0 != list.size()) {
for (BeanInterface bean : list) {
System.out.println(bean.getClass().getName());
}
} else {
System.out.println( " list is null !" );
}
if ( null != map && 0 != map.size()) {
for (Map.Entry<String, BeanInterface> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue().getClass().getName());
}
} else {
System.out.println( "map is null !" );
}
}
}
|
测试类TestInjection:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@RunWith (BlockJUnit4ClassRunner. class )
public class TestInjection extends UnitTestBase {
public TestInjection() {
super ( "classpath:spring-beanannotation.xml" );
}
@Test
public void testMultiBean() {
BeanInvoker invoker = super .getBean( "beanInvoker" );
invoker.say();
}
}
|
首先,我们清楚BeanImplOne类和BeanImplTwo类是实现了BeanInterface接口的,在BeanInvoker类里面我们定义了list和map,我们通过@Autowired注解把BeanImplOne类和BeanImplTwo类注解进入其中。那么怎么证实是@Autowired注解把这两个类注入到list或者map中的呢?那么请看if循环语句和foreach循环打印,通过这个逻辑判断,如果能够打印出BeanImplOne类和BeanImplTwo类的路径名,就说明这样是可以的。如果有些小伙伴可能不信,那么可以试着不使用@Autowired注解,看结果怎么样。
测试类没有什么好说的,各位小伙伴有没有注意到@Order注解呢?这里需要解释的就是,如果在@Order注解里面输入执行的数字,比如1或者2,那么打印出来的路径名就会按顺序,也就是说通过指定@Order注解的内容可以实现优先级的功能。
5.@ImportResource注解引入一个资源,对应一个xml文件
6.@Value注解从资源文件中,取出它的key并赋值给当前类的成员变量
案例:@ImportResource和@Value用法分析:
MyDriverManager类:
1
2
3
4
5
6
7
8
9
|
public class MyDriverManager {
public MyDriverManager(String url, String userName, String password) {
System.out.println( "url : " + url);
System.out.println( "userName: " + userName);
System.out.println( "password: " + password);
}
}
|
config.xml:
1
|
< context:property-placeholder location = "classpath:/config.properties" />
|
StoreConfig类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Configuration
@ImportResource ( "classpath:config.xml" )
public class StoreConfig {
@Value ( "${jdbc.url}" )
private String url;
@Value ( "${jdbc.username}" )
private String username;
@Value ( "${jdbc.password}" )
private String password;
@Bean
public MyDriverManager myDriverManager() {
return new MyDriverManager(url, username, password);
}
|
这个案例我们使用注解配置jdbc数据库的连接,首先创建一个内含构造器的MyDriverManager类,然后配置config.xml里面的资源文件路径,以便@ImportResource注解获取,最后配置StoreConfig类。(注意url、username、password也必须要和数据库的保持一致哦)
详解StoreConfig类:首先我们定义三个成员变量,然后给每一个成员变量打上一个@value注解,注意@value里面的内容一定是资源文件里面的key值。这里的@ImportResource注解就是指明一个资源文件,在这个资源文件里面获取到对应的数据。那么@Configuration注解是用来干嘛的呢?为什么不用@Component注解呢?其实是这样的,@Component注解用于将所标注的类加载到 Spring 环境中,这时候是需要配置component-scan才能使用的,而@Configuration注解是Spring 3.X后提供的注解,它用于取代XML来配置 Spring。
7.@Bean注解用来标识配置和初始化一个由SpringIOC容器管理的新对象的方法,类似XML中配置文件的<bean/>
ps:默认的@Bean注解是单例的,那么有什么方式可以指定它的范围呢?所以这里才出现了@Scope注解
8.@Scope注解,在@Scope注解里面value的范围和Bean的作用域是通用的,proxyMode的属性是采用哪一种的单例方式(一种是基于接口的注解,一种是基于类的代理)
案例:@Bean和@Scope用法分析:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Bean
@Scope (value = "session" ,proxyMode = "scopedProxyMode.TARGET_CLASS" )
public UserPreferences userPreferences(){
return new userPreferences();
}
@Bean
public service userService(){
UserService service = new SimpleUserService();
service.setUserPreferences(userPreferences);
return service;
}
|
浅谈Spring框架注解的用法分析相关推荐
- 浅谈 Spring 框架注解的用法分析
1.@Component是Spring定义的一个通用注解,可以注解任何bean. 2.@Scope定义bean的作用域,其默认作用域是"singleton",除此之外还有proto ...
- 浅谈Spring框架应用的设计模式(一)——工厂模式
文章目录 前言 一.工厂模式介绍 1.简单工厂模式 (1)静态工厂模式 (2)利用反射机制实现的简单工厂 2.工厂方法模式 3.抽象工厂模式 二.Spring框架中工厂模式的重要应用 1.BeanFa ...
- 【JAVAEE框架】浅谈 Spring 框架的两大核心思想 AOP 与 IOP
哈喽~大家好呀,这篇来看看Spring 框架的两大核心思想.
- 浅谈Spring框架AOP概念
何为AOP? AOP(Aspect Oriented Programming,面向切面编程):AOP是一种新的方法论,在这之前相信我们都听过面向对象编程(OOP),AOP就是对OOP的补充,它们的关系 ...
- 浅谈:Spring Boot原理分析,切换内置web服务器,SpringBoot监听项目(使用springboot-admin),将springboot的项目打成war包
浅谈:Spring Boot原理分析(更多细节解释在代码注释中) 通过@EnableAutoConfiguration注解加载Springboot内置的自动初始化类(加载什么类是配置在spring.f ...
- 浅谈Spring IOC的理解
浅谈Spring IOC的理解 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊 ...
- 浅谈Spring定时任务
浅谈Spring定时任务 三种定时任务基于原理 多定时任务并发配置 动态定时任务 定时任务Demo 三种定时任务基于原理 SpringBoot配置定时任务主要有Spring Schedule.JDK自 ...
- 浅谈 Spring IOC和AOP
浅谈 Spring IOC和AOP IOC 控制反转 以前创建对象的主动权和时机是由于自己把握的,现在将这种权利转移到Spring容器中,并且根据配置文件去创建对象管理对象 ioc的注入方式有三种:构 ...
- 由openSession、getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持
由openSession.getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持 Spring和Hibernate的集成的一个要点就是对事务的支持,op ...
最新文章
- cmd命令运行jar包
- java程序编六角星_跨平台移动端解决方案—Weex
- spring解析配置文件(三)
- Android功耗优化(10)---功耗基础知识
- Spring注解大全(更新中)
- iPhone8带来AR新技术,AR游戏要火了吗?
- uva 563(最大流)
- 蓝桥杯.历届试题: 核桃数量
- BZOJ1906树上的蚂蚁BZOJ3700发展城市——RMQ求LCA+树链的交
- 计算机控制器的简写,工业控制常用英语及缩写
- 基于QT实现的数独游戏DPLL的SAT求解器设计
- google开发者大会的倒计时动画,没有用Flash
- 使用Python实现通过doi下载文献pdf
- (openCV 十二)图像增强(对数变换/伽马变换/分段线性变换)
- python爬虫读后感-学习爬虫的感想和心得
- 练习4闭合导线平差计算
- R7 5800H 和 R5 5600H的差距大吗 哪个好
- 第一次软工作业(数独)
- 谷歌浏览器网页打不开怎么办
- 针对儿子买的将近一万的笔记本电脑
热门文章
- 中国平安:杀进智能合约,你怕不怕?
- 2015年微软亚洲研究院的惊艳项目,人工智能抢眼
- Android个人信息管理系统 源代码,个人信息管理系统源代码(自己写的).doc
- 从一副漫画说编码思维,编码习惯,编码风格 (条件判断,死代码,true 或false)
- [并发编程] - Executor框架#ThreadPoolExecutor源码解读02
- WebView完全解读
- Spring+MyBatis 多数据源配置和切换
- php 得到 ts文件,获取TypeScript声明文件.d.ts步骤详解
- java更新 位置_请求位置信息更新 | Android 开发者 | Android Developers
- pythontcp服务器框架_tcp服务器简单框架-python客户端