作者:imba-Y_685 | 来源:互联网 | 2023-10-13 13:10
这篇文章主要介绍了Spring获取当前类在容器中的beanname,实现思路只需继承BeanNameAware接口,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定
下文笔者讲述在spring中获取beanname的方法分享,如下所示
实现思路:
1.只需继承BeanNameAware接口
2.然后重写
@Override
public void setBeanName(String name) {
/*业务代码*/
}
3.setBeanName中传入的参数即代表beanname
例:
package com.adeal;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
public class Person implements BeanNameAware {
private String id;
private String userName;
private String beanName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", userName='" + userName + '\'' +
'}';
}
@Override
public void setBeanName(String name) {
this.beanName = name;
}
public String getBeanName(){
return this.beanName;
}
}