spring 如何获取当前类在容器中的beanname呢?
下文笔者讲述在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; } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。