For International Support we need to implement properties files for each language. In order to support this, Spring provides a ResourceBundleMessageSource which we define in the spring-config.xml. With this in place, we can supply Spring with a list of properties files and a locale to use. We can now use this combination to provide messages... Continue Reading →
annotated Java Bean to read config.properties
This example is a little contrived in order to keep it simple. So far we have only covered Spring beans that are defined using the spring-config.xml. In this example we will use annotations in the bean class declaration to identify it as a Spring bean and then tell Spring where to look for these beans.... Continue Reading →
Spring , @Resource, @PostConstruct and @PreDestroy
Spring supports JSR-250 which provides @Resource, @PostConstruct and @PreDestroy annotations. @Resource The @Resource annotation is used in the bean class and is attached to the setter methods of the properties. It is used to inject a specific bean resource. - will look for a bean with the same name as the property XXX - will... Continue Reading →
Spring context:annotation-config
In our previous examples we have to define a bean for each BeanPostProcessor we wanted to use. Spring provides a shortcut to do this in the context namespace. We simply need to add the context namespace to our declared namespaces at the top of our config xml file and add the context:annotation-config to our bean... Continue Reading →
@Autowired and @Qualifier Annotations for Spring
We use @Autowired to automatically wire our bean dependencies. Example In the following example, we have autowired the home address for our student from the previous example. Spring will look for a single bean with a type that matches the type for home. If it finds this bean then it will automatically inject that bean... Continue Reading →
@Required Spring Annotation
@Required We use the @Required annotation to tell Spring that a property on a bean is required. Student.java Here we have applied the @Required annotation to the method setStudentId. student1.xml This will do nothing if we do not add the RequiredAnnotationBeanPostProcessor bean to our Spring config xml file. TestStudent1.java Now we can bring it all... Continue Reading →
Using a Bean PostProcessor for Spring Annotations
I will cover the easy way to do this in a later post, but in this post we will look at how Spring processes annotations. Available JSR-250 Spring Annotations The spring-beans-3.x.jar contains the package org.springframework.beans.factory.annotation. In this package you will find the following BeanPostProcessors: AutowiredAnnotationBeanPostProcessorInitDestroyAnnotationBeanPostProcessorQualifierAnnotationBeanPostProcessorRequiredAnnotationBeanPostProcesor These PostProcessors facilitate the following annotations which can be found... Continue Reading →
Read Spring Bean Values from Property File
This example uses a BeanFactoryPostProcessor called PropertyPlaceholderConfigurer to read spring.xml config values from a property file. Example people2.properties people2.xml note that we use the PropertyPlaceholderConfigurer which is a BeanFactoryPostProcessor provided specifically for working with property configuration. TestPeople2.java
BeanFactoryPostProcessor
The BeanFactoryPostProcessor is useful for custom config files targeted at system administrators that override bean properties configured in the application context. Use this to execute code once the BeanFactory itself has been initialised. Example MyBeanFactoryPostProcessor Create the class MyBeanFactoryPostProcessor that implements BeanFactoryPostProcessoroverride the method postProcessBeanFactoryprint a message in the method Configure the PostProcessor Bean Declare... Continue Reading →
BeanPostProcessor
What is it for? Execute a block of code for each and every bean that is initialised. Bean Post Processor is a separate class Spring executes the code in the Post Processor after initializing each and every bean Example Print the Name of Every Bean that is Initialized DisplayNameBeanPostProcessor Register the BeanPostProcessor with Spring Spring.xml