Spring(七)用@Resource和@Autowired注解完成属性装配及自动装配

2024-06-16 11:08

本文主要是介绍Spring(七)用@Resource和@Autowired注解完成属性装配及自动装配,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用到注解需导入jar包:common-annotations.jar
手工装配依赖对象有两种编程方式:
一、在xml配置文件中通过bean节点进行配置,如:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="personDao" class="test.spring.dao.impl.PersonDaoBean" /><bean id="personService" class="test.spring.service.impl.PersonServiceBean3"><constructor-arg index="0" type="test.spring.dao.PersonDao" ref="personDao"/><constructor-arg index="1" value="LinDL"/></bean>
</beans> 

二、在Java代码中使用@Autowired或@Resource注解方式进行装配。但需在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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- 把针对注解的容器注射到Spring容器中 --><context:annotation-config /><bean id="..." class="..." /></beans> 

这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequireAnnotationBeanPostProcessor

在代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:
@Autowired默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
用于字段上
@Resource
private PersonDao personDao;
用于属性的setter方法上
@Resource
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}

一个应用@Resource的例子:

package test.spring.dao;public interface PersonDao {public abstract void add();}
package test.spring.dao.impl;import test.spring.dao.PersonDao;public class PersonDaoBean implements PersonDao {@Overridepublic void add(){System.out.println("执行PersonDaoBean里的test1()方法");}
}
package test.spring.service;public interface PersonService2 {public abstract void save();
}
package test.spring.service.impl;import javax.annotation.Resource;import test.spring.dao.PersonDao;
import test.spring.service.PersonService2;public class PersonServiceBean4 implements PersonService2 {// 先根据属性名personDao在beans2.xml中查找注入,如果找不到对应的就会按照类型进行注入// @Resource// private PersonDao personDao;// 也可以指定配置文件中的id进行注入// @Resource(name = "personDaoxx")private PersonDao personDao;public PersonServiceBean4() {}public PersonDao getPersonDao() {return personDao;}// 也可以通过属性的setter方法进行注入,不能用于属性的getter方法@Resourcepublic void setPersonDao(PersonDao personDao) {this.personDao = personDao;}@Overridepublic void save() {personDao.add();}
}

beans2.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- 把针对注解的容器注射到Spring容器中 --><context:annotation-config /><bean id="personDaoxx" class="test.spring.dao.impl.PersonDaoBean" /><bean id="personService" class="test.spring.service.impl.PersonServiceBean4"></bean>
</beans> 
package test.spring.jnit;import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.spring.service.PersonService2;public class SpringTest3 {@Testpublic void testInject() {AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans2.xml");PersonService2 personService = (PersonService2) applicationContext.getBean("personService");personService.save();applicationContext.close();}}

@Autowired的应用

package test.spring.service.impl;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;import test.spring.dao.PersonDao;
import test.spring.service.PersonService2;public class PersonServiceBean5 implements PersonService2 {// 按类型进行装配// @Autowired// private PersonDao personDao;// 按名称进行装配// @Autowired@Qualifier("personDaoxx")// private PersonDao personDao;/** required=true,如果在Spring中根据类型找不到对应的容器,就会报没有成功地为这个字段注入值;* required=false,如果在Spring容器里面找不到与类型匹配的bean,就会把这个变量设为null*/@Autowired(required = true)@Qualifier("personDaoxx")private PersonDao personDao;public PersonServiceBean5() {}public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}@Overridepublic void save() {personDao.add();}
}

自动装配,使用自动装配方式难以调试,程序中的bug通常在运行过程中出现问题才发现,而且容易出现难以预料的问题,故不建议使用。
beans3.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><bean id="personDaoxx" class="test.spring.dao.impl.PersonDaoBean" /><bean id="personService" class="test.spring.service.impl.PersonServiceBean6" autowire="byType"></bean></beans> 
package test.spring.service.impl;import test.spring.dao.PersonDao;
import test.spring.service.PersonService2;public class PersonServiceBean6 implements PersonService2 {private PersonDao personDao;public PersonServiceBean6() {}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}@Overridepublic void save() {personDao.add();}
}
package test.spring.jnit;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import test.spring.service.PersonService2;public class SpringTest5 {@Testpublic void testAutowired() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans3.xml");PersonService2 personService = (PersonService2) applicationContext.getBean("personService");personService.save();}}

这篇关于Spring(七)用@Resource和@Autowired注解完成属性装配及自动装配的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1066292

相关文章

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp