13.07.2015 Views

Spring JavaConfig Reference Guide - Spring Web Services - Parent ...

Spring JavaConfig Reference Guide - Spring Web Services - Parent ...

Spring JavaConfig Reference Guide - Spring Web Services - Parent ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating and using bean definitions4.2.2. Using *Aware interfacesThe standard set of *Aware interfaces such as BeanFactoryAware, BeanNameAware, MessageSourceAware,ApplicationContextAware, etc. are fully supported. Consider an example class that implementsBeanFactoryAware:public class AwareBean implements BeanFactoryAware {private BeanFactory factory;// BeanFactoryAware setter (called by <strong>Spring</strong> during bean instantiation)public void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.factory = beanFactory;}}public void close(){// do clean-up}Also, the lifecycle callback methods are fully supported.4.2.3. Bean visibilityA feature unique to <strong>JavaConfig</strong> feature is bean visibility. <strong>JavaConfig</strong> uses standard Java method visibilitymodifiers to determine if the bean ultimately returned from a method can be accessed by an owning applicationcontext / bean factory.Consider the following configuration:@Configurationpublic abstract class VisibilityConfiguration {}@Beanpublic Bean publicBean() {Bean bean = new Bean();bean.setDependency(hiddenBean());return bean;}@Beanprotected HiddenBean hiddenBean() {return new Bean("protected bean");}@BeanHiddenBean secretBean() {Bean bean = new Bean("package-private bean");// hidden beans can access beans defined in the 'owning' contextbean.setDependency(outsideBean());}@ExternalBeanpublic abstract Bean outsideBean()Let's bootstrap the above configuration within a traditional XML configuration (for more information onmixing configuration strategies see Chapter 8, Combining configuration approaches). The application contextbeing instantiated agaist the XML file will be the 'owning' or 'enclosing' application context, and will not beable to 'see' the hidden beans:<strong>Spring</strong> <strong>JavaConfig</strong> 12

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!