Spring获取容器beanname
戚薇 人气:0下文笔者讲述在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; } }
加载全部内容