Spring——Aware相關介面
阿新 • • 發佈:2018-11-10
Aware,是感應和感知的意思。當bean實現了對應的Aware介面時,BeanFactory會在生產bean時根據它所實現的Aware介面,給bean注入對應的屬性,從而讓bean獲取外界的資訊。
Spring提供了一堆Aware介面:
下面列出幾個主要Aware介面作用:
org.springframework.context.ApplicationContextAware介面:
實現類的例項將會獲取ApplicationContext的引用,因此可以程式設計式的使用ApplicationContext手動建立bean.
public interface ApplicationContextAware { void setApplicationContext(ApplicationContext applicationContext) throws BeansException; }
自Spring2.5起,可使用自動裝配模式獲取ApplicationContext引用:
@RestController public class HelloWorldController2 { @Autowired ApplicationContext applicationContext; @RequestMapping("/helloworld") public String helloWorld() { System.out.println("通過applicationContext獲取的bean例項:" + applicationContext.getBean("helloService1")); return "Hello World!"; } }
org.springframework.beans.factory.BeanNameAware介面:
在bean內部,它並不知道容器給自己取了個什麼id,如果想要獲取自己在容器中的id,可以實現BeanNameAware介面獲取。其setBeanName(string name)方法的引數就是容器為該bean注入的它本身的id。
public interface BeanNameAware {
void setBeanName(string name) throws BeansException;
}
org.springframework.beans.factory.BeanFactoryAware 介面:
實現類的例項將會獲取BeanFactoryAware的引用。BeanFactoryAware 介面中只有一個方法setBeanFactory(BeanFactory beanFactory)。用法和ApplicationContextAware類似。
舉個栗子:
現在新建一個AwareTest類,實現了BeanNameAware和ApplicationContextAware介面,程式碼如下:
package twm.spring.LifecycleTest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class AwareTest implements BeanNameAware, ApplicationContextAware,
BeanFactoryAware {
String beanname;
ApplicationContext appct;
BeanFactory bFactory;
@Override
public void setBeanName(String name) {
this.beanname = name;
System.out.println("通過BeanNameAware介面的實現,我知道我的名字是:" + this.beanname);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.appct = applicationContext;
System.out.println("通過ApplicationContextAware介面實現,獲得了容器物件:"
+ this.appct.toString().substring(0, 35) +"......");
footballPlayer player = appct.getBean("messi", footballPlayer.class);
player.setName("maladona");
player.pass();
player.shoot();
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.bFactory = beanFactory;
System.out.println("通過BeanFactoryAware介面實現,獲得了容器物件:"
+ this.bFactory.toString().substring(0, 35)+"......");
}
}
beans.xml:
<bean id="messi" class="twm.spring.LifecycleTest.footballPlayer">
<property name="name" value="Messi"></property>
<property name="team" value="Barcelona"></property>
</bean>
<bean id="aware_suibianqu" class="twm.spring.LifecycleTest.AwareTest" />
在程式中呼叫:new ClassPathXmlApplicationContext(“beans.xml”);初始化容器和bean時,輸出結果:
通過BeanNameAware介面的實現,我知道我的名字是:aware_suibianqu
通過BeanFactoryAware介面實現,獲得了容器物件:org.springframework.beans.factory.s……
通過ApplicationContextAware介面實現,獲得了容器物件:org.springframework.context.support……
maladona邊路傳中 maladona射門