Spring中有不少繼承於aware
中的接口,這些接口究竟是作什麼用到的。
java
aware
,翻譯過來是知道的,已感知的,意識到的,因此這些接口從字面意思應該是能感知到全部Aware
前面的含義。git
先舉個BeanNameAware
的例子,實現BeanNameAware
接口,能夠讓該Bean
感知到自身的BeanName
(對應Spring容器的BeanId
屬性)屬性,舉個例子:github
BeanNameAware
接口的定義public interface BeanNameAware extends Aware { void setBeanName(String name); }
User
,一個實現BeanNameAware
,一個不實現。package com.github.jettyrun.springinterface.demo.aware.beannameaware; import org.springframework.beans.factory.BeanNameAware; /** * Created by jetty on 18/1/31. */ public class User implements BeanNameAware{ private String id; private String name; private String address; public void setBeanName(String beanName) { //ID保存BeanName的值 id=beanName; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", address='" + address + '\'' + '}'; } }
package com.github.jettyrun.springinterface.demo.aware.beannameaware; import org.springframework.beans.factory.BeanNameAware; /** * Created by jetty on 18/1/31. */ public class User2 { private String id; private String name; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", address='" + address + '\'' + '}'; } }
<bean id="zhangsan" class="com.github.jettyrun.springinterface.demo.aware.beannameaware.User"> <property name="name" value="zhangsan"></property> <property name="address" value="火星"></property> </bean> <bean id="lisi" class="com.github.jettyrun.springinterface.demo.aware.beannameaware.User2"> <property name="name" value="lisi"></property> <property name="address" value="火星"></property> </bean>
BeanNameAware
接口所起的做用。public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-beanaware.xml"); User user=context.getBean(User.class); System.out.println(String.format("實現了BeanNameAware接口的信息BeanId=%s,全部信息=%s",user.getId(),user.toString())); User2 user2=context.getBean(User2.class); System.out.println(String.format("未實現BeanNameAware接口的信息BeanId=%s,全部信息=%s",user2.getId(),user2.toString())); }
實現了BeanNameAware接口的信息BeanId=zhangsan,全部信息=User{id='zhangsan', name='zhangsan', address='火星'} 未實現BeanNameAware接口的信息BeanId=null,全部信息=User{id='null', name='lisi', address='火星'}
可以看到,咱們在實現了BeanNameAware
的 User
中,獲取到了Spring容器中的BeanId
(對應spring配置文件
中的id
屬性),而沒有實現BeanNameAware
的User2
,則不能獲取到Spring容器中的Id屬性。spring
因此BeanNameAware
接口是爲了讓自身Bean
可以感知到,獲取到自身在Spring容器中的id屬性。app
同理,其餘的Aware
接口也是爲了可以感知到自身的一些屬性。
好比實現了ApplicationContextAware
接口的類,可以獲取到ApplicationContext
,實現了BeanFactoryAware
接口的類,可以獲取到BeanFactory
對象。ide