首页 技术 正文
技术 2022年11月14日
0 收藏 389 点赞 4,079 浏览 3643 个字

1)@Resource(JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解)

Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。

@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

@Resource装配顺序

1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常

3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(InjectionDAO)进行匹配,如果匹配则自动装配;

2)@PostConstruct 、@PreDestroy

JSR-250 为初始化之后/销毁之前方法的指定定义了两个注释类,分别是 @PostConstruct 和 @PreDestroy,这两个注释只能应用于方法上。标注了 @PostConstruct 注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁之前调用。

使用@PostConstruct@PreDestroy 注释可以指定多个初始化 / 销毁方法,那些被标注@PostConstruct  或 @PreDestroy 注释的方法都会在初始化 / 销毁时被执行。

package com.beanannotation.jsr;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;import org.springframework.stereotype.Service;@Service
public class JsrService {@PostConstruct
public void init(){
System.out.println("JsrService init.");
}@PreDestroy
public void destory(){
System.out.println("JsrService destory.");
}}

实例:

定义两个类JsrDAO、JsrService

package com.beanannotation.jsr;import org.springframework.stereotype.Repository;@Repository
public class JsrDAO {public void save(String s){
System.out.println("操作数据库保存数据:"+s);
}
}
package com.beanannotation.jsr;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;import org.springframework.stereotype.Service;@Service
public class JsrService {@Resource
private JsrDAO jsrDAO;@PostConstruct
public void init(){
System.out.println("JsrService init.");
}@PreDestroy
public void destory(){
System.out.println("JsrService destory.");
}public void save(String arg){
jsrDAO.save(arg);
}
}

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.beanannotation.jsr">
</context:component-scan>
</beans>

单元测试:

package com.beanannotation.jsr;import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.context.support.ClassPathXmlApplicationContext;@RunWith(BlockJUnit4ClassRunner.class)
public class UnitTest {@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beanannotation.xml");
JsrService service = (JsrService)context.getBean("jsrService");
service.save("data");
context.close(); // 关闭 Spring 容器,以触发 Bean 销毁方法的执行}}

结果:

2015-7-7 13:05:43 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ed1e89e: startup date [Tue Jul 07 13:05:43 CST 2015]; root of context hierarchy
2015-7-7 13:05:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-beanannotation.xml]
JsrService init.
2015-7-7 13:05:44 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4ed1e89e: startup date [Tue Jul 07 13:05:43 CST 2015]; root of context hierarchy
操作数据库保存数据:data
JsrService destory.
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295