下文笔者讲述在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;
}
}
到此这篇关于Spring获取当前类在容器中的beanname的文章就介绍到这了,更多相关Spring获取容器beanname内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!