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 definitionsdocumentation.4.3.1. Construction OptionsInstantiating the <strong>JavaConfig</strong>ApplicationContext can be done by supplying @Configuration-annotated classliterals to the constructor, and/or strings representing packages to scan for @Configuration-annotated classes.4.3.1.1. Construction by class literalEach of the class literals supplied to the constructor will be processed, and for each @Bean-annotated methodencountered, <strong>JavaConfig</strong> will create a bean definition and ultimately instantiate and initialize the bean.<strong>JavaConfig</strong>ApplicationContext context =new <strong>JavaConfig</strong>ApplicationContext(AppConfig.class);Service service = context.getBean(Service.class);<strong>JavaConfig</strong>ApplicationContext context =new <strong>JavaConfig</strong>ApplicationContext(AppConfig.class, DataConfig.class);Service service = context.getBean(Service.class);4.3.1.2. Construction by base packageBase packages will be scanned for the existence of any @Configuration-annotated classes. Any candidateclasses will then be processed much as if they had been supplied directly as class literals to the constructor.<strong>JavaConfig</strong>ApplicationContext context =new <strong>JavaConfig</strong>ApplicationContext("**/configuration/**/*.class");Service service = (Service) context.getBean("serviceA");<strong>JavaConfig</strong>ApplicationContext context =new <strong>JavaConfig</strong>ApplicationContext("**/configuration/**/*.class", "**/other/*Config.class);Service service = (Service) context.getBean("serviceA");4.3.1.3. Post-construction configurationWhen one or more classes/packages are used during construction, a <strong>JavaConfig</strong>ApplicationContext cannot befurther configured. If post-construction configuration is preferred or required, use either the no-arg constructor,configure by calling setters, then manually refresh the context. After the call to refresh(), the context will be'closed for configuration'.<strong>JavaConfig</strong>ApplicationContext context = new <strong>JavaConfig</strong>ApplicationContext();context.set<strong>Parent</strong>(otherConfig);context.setConfigClasses(AppConfig.class, DataConfig.class);context.setBasePackages("**/configuration/**/*.class");context.refresh();Service service = (Service) context.getBean("serviceA");NoteWhenever multiple packages and/or classes are used to instantiate a<strong>JavaConfig</strong>ApplicationContext, order matters. This is important when considering what happensif two configuration classes define a bean with the same name. The last-specified class wins.(1.0.0.m4-SNAPSHOT)

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

Saved successfully!

Ooh no, something went wrong!