Spring注解之@Autowired:Setter 方法上使用@Autowired注解
可以在 JavaBean中的 setter 方法中使用 @Autowired 注解。当 Spring遇到一个在 setter 方法中使用的 @Autowired 注解时,它会在方法中按照类型自动装配参数值。创建测试类User,并且添加属性student,
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import java.io.Serializable; /**
* @author Wiener
*/
@Component
public class User implements Serializable {
private static final long serialVersionUID = 6089103683553156328L;
private Long id;
private Student student; public Student getStudent() {
return student;
} @Autowired
public void setStudent(Student student) {
this.student = student;
} public void isStu() {
student.studentStudy();
System.out.println("------ 装配Bean成功 ---------");
}
}
下面创建依赖的类文件 Student.java,切莫忘记在类文件上添加注解 @Component:
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component; import java.io.Serializable;
import java.util.Date; /**
* @author Wiener
*/
@Getter
@Setter
@Component
public class Student implements Serializable { private static final long serialVersionUID = -5246589941647210011L; //姓名
private String name; public Student() {
System.out.println("A default student constructor." );
}
public void studentStudy() {
System.out.println("A student is studying." );
}
}
修改Spring Boot启动类,通过Spring容器拿到Bean实例user:
import com.east7.bean.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext; /**
* @author Wiener
*/
@SpringBootApplication
public class East7Application { public static void main(String[] args) {
ApplicationContext act = SpringApplication.run(East7Application.class, args);
User user = (User) act.getBean("user");
user.isStu();
} }
启动应用程序,控制台将会输出以下消息:
A default student constructor.
A student is studying.
------ 装配Bean成功 ---------
说明student属性被装配成功。如果setStudent方法不加注解,程序运行时,会抛出如下异常:
Exception in thread "main" java.lang.NullPointerException
at com.east7.bean.User.isStu(User.java:28)
at com.east7.East7Application.main(East7Application.java:25)
Spring注解之@Autowired:Setter 方法上使用@Autowired注解的更多相关文章
- Spring源码学习之:@async 方法上添加该注解实现异步调用的原理
在我们使用spring框架的过程中,在很多时候我们会使用@async注解来异步执行某一些方法,提高系统的执行效率.今天我们来探讨下 spring 是如何完成这个功能的. spring 在扫描be ...
- Spring @async 方法上添加该注解实现异步调用的原理
Spring @async 方法上添加该注解实现异步调用的原理 学习了:https://www.cnblogs.com/shangxiaofei/p/6211367.html 使用异步方法进行方法调用 ...
- @async 方法上添加该注解实现异步调用的原理
在我们使用spring框架的过程中,在很多时候我们会使用@async注解来异步执行某一些方法,提高系统的执行效率.今天我们来探讨下 spring 是如何完成这个功能的. spring 在扫描bean的 ...
- spring构造函数注入、setter方法注入和接口注入
Spring开发指南中所说的三种注入方式: Type1 接口注入 我们常常借助接口来将调用者与实现者分离.如: public class ClassA { private InterfaceB clz ...
- spring 构造方法注入和setter方法注入的XML表达
1.构造方法注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC ...
- Spring、使用注解方式装配对象(@Resource、@Autowired)
使用手工注解方式有两种方式@Resource.@Autowired 首先,引入注解所使用的Jar包 :common-annotations.jar 然后在beans.xml中加入命名空间空间 xml ...
- 18.3.2从Class上获取信息(注解)
package d18_3_1; /** * Class类上所包含的注解 * * getAnnotation(Class annotationClass) 获取该元素上指定的类型的注解 * getAn ...
- spring aop获取目标对象的方法对象(包括方法上的注解)
这两天在学习权限控制模块.以前看过传智播客黎活明老师的巴巴运动网视频教程,里面就讲到权限控制的解决方案,当时也只是看看视频,没有动手实践,虽说看过几遍,可是对于系统中的权限控制还是很迷茫,所以借着这次 ...
- SpringBoot之SpringSecurity权限注解在方法上进行权限认证多种方式
前言 Spring Security支持方法级别的权限控制.在此机制上,我们可以在任意层的任意方法上加入权限注解,加入注解的方法将自动被Spring Security保护起来,仅仅允许特定的用户访问, ...
- 面试官:spring中定义bean的方法有哪些?我一口气说出了12种,把面试官整懵了。
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
随机推荐
- springboot本地运行正常,打包jar包上传Linux服务器后报错,无法正常运行解决方法
问题描述:springboot本地运行正常,打包jar包上传Linux服务器后报错,无法正常运行 说明:以下两种打包方式均在IDEA软件内完成,上传服务器使用宝塔面板管理 1.第一次打包方式: 设置完 ...
- Abaqus压电材料分析
目录 1. ABAQUS压电材料定义[快速版] 2. 压电材料的主要特性参数 3. Abaqus 压电分析 1 电响应 2 initial condition & BCs 4 Load 5 M ...
- DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
大家好,我是狂师. 前阵子在知乎闲逛时,有个问题激起了大家的热议:"DeepSeek 如何颠覆传统软件测试?测试工程师会被淘汰吗".这看似简单的一问,激起层层思考,针对这个问题,今 ...
- Docker daemon error cannot allocate memory
Issue When getting theses errors: Jan 12 00:21:55 -- dockerd[904]: time="2018-01-12T00:21:55.32 ...
- 痞子衡嵌入式:恩智浦i.MX RT1xxx上特色外设XBAR那些事(1)- 初识
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是恩智浦i.MX RT1xxx系列上的XBAR外设. 得益于 Arm Cortex-M 内核的普及,现如今 MCU 厂商遍地开花,只要能取 ...
- Caddy一个强大的web服务器和代理服务器
什么是Caddy Caddy是一个强大的.可扩展的平台,可以为您的站点.服务和应用程序提供服务,它是用Go编写的.虽然大多数人使用它作为web服务器或代理,但其实他支持更多的功能: Caddy是唯一自 ...
- 一次windows下使用cmake遇到的问题
背景 在windows下的cmake和mingw提供的make,在windows环境下进行了简单尝试,结果发现make的时候失败: #include <iostream> int main ...
- public void add(int index, E element)的方法源码分析
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...
- Mybatis的*Dao.XML中的配置与其对应的接口、resultMap的运用
例子. <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC &quo ...
- Nerf和3DGS神经重建技术在自动驾驶模拟中的应用
验证自动驾驶软件需要数百万公里的测试.这不仅意味着系统开发周期长,而且系统的复杂度也会不断增加,同时,大规模的实车测试也会耗费巨量的资源并且可能会面临未知的安全问题.aiSim这样的虚拟仿真工具可以减 ...