________________________________________________________
Implement ApplicationContextAware to access object variables from "Triangle" class. This implements a method to setApplicationContext(ApplicationContext context) , that gives access to the context to modify Object variables of "Triangle class
BeanAwareContext is implemented to find teh name of the Bean Class(down in the implemeneted setter method)
_________________________________________________________
Spring annotations
@Required : is an indication on nullpointer variables.. if they are not declared within the spring.xml, while their values are used in some related methods. This turns out to be useful, as it throws an exception during the BeanPostProcessing itself, instead of a scenario where the code accesses the method at a later stage of the application.
1)
@Required is placed over a variable declaration or the variables setter method, in the object class.
2)
<bean class= "org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
</bean>
@Autowired
- Holds same functionaly as the "autowire" parameter in the <bean> tag.
- It first autowires the class object beanvariables based on type, then name and then the qualifier(refer annotation below)
- Since it autowires by type first, and if there and many <bean> with same "class", then it then looks for the name of the beans as in match to teh variable names in the class, However, in cases where more than 2 classes use the same bean with different variable names, then we also include another annotation called @qualifier. Refer the next anotation for the same.
This is also placed over a variable declaration or the variables setter method, in the object class.
1)
@Autowired is placed over a variable declaration or the variables setter method, in the object class.
2)
<bean class= "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
</bean>
@Qualifier("qualifiername")
Refer @Autowire Annotation to understand this one better. This is also placed right below the @autowire annotation, which in-turn is being placed over a variable declaration or the variables setter method, in the object class.
1)
@Qualifier("testQualifier") is placed UNDER the @autowire in the object class for additional reference help to identify the right bean
2)
<bean class= "org.springframework.beans.factory.annotation.QualifierAnnotationBeanPostProcessor">
</bean>
3)include a <qualifier> tag
<beans>
<bean id=".." class="...">
<qualifier value="testQualifier" />
<property name="x" value="someValue" />
<bean>
<bean class= "org.springframework.beans.factory.annotation.QualifierAnnotationBeanPostProcessor">
</bean>
</beans>
NOTE:
- step 2) for all the annotations can be skipped and simply replaced with just one line, in the spring.xml
- <context:annotation-config/>
- adding a <qualifier> tag can bring in other related xml exceptions.. hence... copy and paste xml namespaces at the beginning of the spring.xml
- comment the default<DOC.... tag
- replace <beans> tag with the below content
<!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">
<bean ..............................> </bean>
</beans>
Stereotype Annotations:
Configured in spring.xml as below:
@Component(org.springframework.stereotype.Component)
@Service(org.springframework.stereotype.Service)
@Repository(org.springframework.stereotype.Repository)
@Controller(org.springframework.stereotype.Controller)
Configured in spring.xml as below:
- <context:component-scan base-package= "<<javaPackageName>>"/>
Note:
including the above tag enables a scan for all the annotations below within the <<javaPackageName>>
@Service(org.springframework.stereotype.Service)
@Repository(org.springframework.stereotype.Repository)
@Controller(org.springframework.stereotype.Controller)
External Annotations used in Spring framework
(JSR-250)annotations- standard annotations across multiple technologies, also used here.
- @Resource(javax.annotation.Resource) - used as @autowire
- @Resource (or) @Resource(resourceName")
- @PostConstruct(javax.annotation.Resource) - placed above a method treats it like a init() method(same method as the init() method created for execution of logic just after bean creation)
- @PreDestroy(javax.annotation.Resource) - placed above a method treats it like a destroy() method(same method as the destroy() method created for execution of logic just before bean destroyed).
- As discussed earlier, Replacing of AbstractApplicationContext class and call to registerShutdownHook is required only while implementing destroy annotation.
- @Component
________________________________________________________
Using Properties file for Strings externalizing: ResourceBundleMessageSource
- This is used to access string values from a properties file within a class file
Done is 3 steps:
1)Create a .properties file with required string and relevant values in the src location of teh project. 2)Configure the ResourceBundleMessageSource withing the spring.xml with a specific "id" as below:
.... <bean id="" class=".....ResourceBundleMessageSource">
<property>.....</property> // for multiple properties files.
.....
3)Now access this message source using context as below :
......context.getMessage(....);
4)Now, to make it available for any object.. we need to declare it as a local variable with the object class with the same name as "id"(in spring.xml) and provide its gettign and seetter.. n access it with the getter method of the object.
..............
Also consider accessing dyamic paceholders within the .properties files.. later passed as messages ot the messageSource in class.
........
________________________________________________________
EVENT HANDLING in Spring Framework
org.springframework.context.
ApplicationListener(using ApplicationEvent as input)
/ApplicationEventPublisher
/ApplicationEventPublisherAware
1)Publishing Application Events
- Create a class that implement ApplicationListener and implement its unimplemented method(onApplicationEvent(ApplicationEvent event), which gets executed when the event occurs. Once this is done, this class listens to all events published on Spring framework.
- Configure this as a bean in the spring.xml; either by manually entereing the <bean> tag for this newly created class (OR) simply include @Component annotation above the class declaration, which does the same function.
2)Publishing Custom ApplicationEvent that gets listened and published when a method that is desired to be published when executed, executes..
- create a customEvent class that extends ApplicationEvent and overwrite the toString() method
- Now publish this event(say, you want to publish the execution of Draw method in the Shape class. For this you need to do the following steps:
- Implement ApplicationEventPublisherAware and implement the local variable ApplicationEventPublisher in the unimplemented setter method in the Shape class that has the draw() method.
- Now, define the Event you just created(customEvent) and pass this event to the publisher as below in the draw() method:
- ... draw(){ .... CustomEvent ce = new CustomEvent(); publisher.publishEvent(ce);}
- Now, when draw() method is called everytime, the toString() of CustomEvent is executed too.
________________________________________________________