What is the use of afterPropertiesSet() life cycle method of spring bean? For what kind of activities it can be used in real world applications?
As the method name afterPropertiesSet() hints, it will be invoked by the BeanFactory after all the properties have been provided, and can be used to initialize a bean or making sure is in a valid state.
Let's say I have a bean with a String property that must not be null or empty.
public class MyBean implements InitializingBean {
private String myString;
public void afterPropertiesSet() {
if(myString == null || myString.isEmpty()){
throw IllegalStateException("myString must have text.");
}
}
public void setMyString(String myString) {
this.myString = myString;
}
}
Of course this is only a very simple code to demonstrate how can be used in real applications.
HTH,
Jose Luis
Related
Is there a way to intercept calls of bean methods annotated with #RabbitListener without using AspectJ.
The code is something like this
#OtherAnnotation
#RabbitListener
public void do(Message message)
I need to intercept all calls to #RabbitListener method, if the method has #OtherAnnotation annotation.
UPDATE:
I managed to make it work using Gary Russell solution.
public class CustomRabbitListenerAnnotationBeanPostProcessor extends RabbitListenerAnnotationBeanPostProcessor {
#Override
protected void processAmqpListener(RabbitListener rabbitListener, final Method method, Object bean, String beanName) {
if (method.isAnnotationPresent(OtherAnnotation.class)) {
ProxyFactory proxyFactory = new ProxyFactory(bean);
proxyFactory.addAdvisor(new StaticMethodMatcherPointcutAdvisor(new OtherAnnotationInterceptor()) {
#Override
public boolean matches(Method advisorMethod, Class<?> targetClass) {
return advisorMethod.equals(method);
}
});
Object proxiedBean = proxyFactory.getProxy();
super.processAmqpListener(rabbitListener, method, proxiedBean, beanName);
} else {
super.processAmqpListener(rabbitListener, method, bean, beanName);
}
}
}
The bean definition is like:
#Bean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)
public CustomRabbitListenerAnnotationBeanPostProcessor customRabbitListenerAnnotationBeanPostProcessor() {
return new CustomRabbitListenerAnnotationBeanPostProcessor();
}
It's a bit ugly, but it works. If anyone has better solution, please share it.
You can subclass RabbitListenerAnnotationBeanPostProcessor and override the processListener method and modify the bean before invoking the super version.
Then, replace the RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME bean registered by the #EnableRabbit with your subclass.
Or, simply add your advice to the container factory's advice chain and all listeners will be advised. You can then do a runtime check to see if the other annotation is present.
i think what you are looking for is a bean-post-processor
heres a simple example:
https://www.tutorialspoint.com/spring/spring_bean_post_processors.htm
if you need to intercept calls you can wrap a proxy over the returned instance. a good example is the org.springframework.validation.beanvalidation.MethodValidationPostProcessor. you can probably extend org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor as well.
EDIT i am just learning this myself so i hope that is the right way to do this but this worked for me when experimenting with it
#Component
public class MyInterceptAnnotationBeanPostProcessor
extends AbstractBeanFactoryAwareAdvisingPostProcessor
implements InitializingBean {
public void afterPropertiesSet() {
AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(
null,
MyIntercept.class);
this.advisor = new DefaultPointcutAdvisor(pointcut, this.createAdvice());
}
protected Advice createAdvice() {
return new MethodInterceptor() {
#Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("advice");
return arg0.proceed();
}
};
}
}
I currently employ the following "pattern" on all my Spring component/configuration/service classes:
#Component
final class SomeComponent implements InitializingBean {
private SomeBean someBean;
#Autowired
public SomeComponent(SomeBean someBean) {
this.someBean = someBean;
}
#Override
public void afterPropertiesSet() throws Exception {
Assert.state(someBean != null, "SomeBean should not be null.");
}
public void someMethod() {
someBean.doSomething();
}
}
Should I really be implementing things like this or if I am just adding up to initialization time by making pointless assertions? Does Spring make any guarantees about the nullity of injected beans?
The documentation says:
By default, the autowiring fails whenever zero candidate beans are available; the default behavior is to treat annotated methods, constructors, and fields as indicating required dependencies.
So yes, you're making pointless assertions. BTW, you could do these assertions in the constructor directly, or at least in a #PostConstruct-annotated method, instead of having to override InitializingBean.
I am new to the Spring Framework. Please guide me regarding basic understanding of Spring. I know Java and JSF, but I don't know anything about Struts or other frameworks. I have searched the Internet and was able to download a PDF, but I don't understand the Spring flow like I understand JSF flow. Please provide me with links for a simple way to understand Spring's flow.
Transition 1 – User sends request to server by submitting form / by
clicking hyperlink etc. Request is initially given to WEB.XML.
Transition 2 – WEB.XML routes request to DispatcherServlet by looking
at tag.
Transition 3 – Inside DispatcherServlet,
First ‘HandlerMapping’ handed over request to suitable ‘Controller’.
Transition 4 – Controller maps request to proper Model class. All
BUSINESS LOGIC is done inside Model class.
Transition 5 – If database
operation is needed then Model class will route request to suitable
DAO. All database operations should be carried out in DAO.
Transition6 – If needed then attach attributes into request/session/application
scope and return back to Model.
Transition 7 – If needed then attach
attributes into request/session/application scope and return back to
Controller.
Transition 8 – Controller simply returns it to any View
(JSP/HTML etc).
Transition 9 – JSP/Html is viewed back to user.
Spring MVC Application Flow:
I'm new to Spring Framework too.
Up to now, the document below is the most basic one. I'm reading it as well, hope it could help you.
Introduction to Spring Framework
I am new to Spring too and time ago had similar question. First of all I want to recommend you 'Spring in Action' book by Craig Walls, I found it very usefull and easy to understand, also
http://www.tutorialspoint.com/spring/ helped me to figure many things out. If I understood your question right, then we can divide “Spring's flow” into Spring IoC container's and Spring bean's life cycles. Here is very small overview with exapmle on Spring bean's life cycle. A bean goes through several steps between creation and destruction in the Spring
container. These steps are:
Instantiate
Populate properties
BeanNameAware`s setBeanName()
BeanFactoryAware`s setBeanFactory
ApplicationContextAware`s setApplicationContext()
Pre-initialization BeanPostProcessors
InitializingBean`s afterPropertiesSet()
Call custom init-method
Post-initialization BeanPostProcessors
DisponsableBean`s destroy
Call custom destroy-method
Each step provides own oportunities for customization. Here is some code which simply “traces” bean`s life:
For bean ClassA:
public class ClassA implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware{
private String messageA;
public ClassA() {
System.out.println("ClassA: default constructor called.");
}
public void customInitMethod(){
System.out.println("ClassA: customInitMethod() method called.");
}
public void customDestroyMethod(){
System.out.println("ClassA: customDestroyMethod() method called.");
}
public String getMessageA() {
System.out.println("ClassA: message get method called.");
return messageA;
}
public void setMessageA(String message) {
System.out.println("ClassA: message set method called.");
this.messageA = message;
}
public void afterPropertiesSet() throws Exception {
System.out.println("ClassA: afterPropertiesSet() called because InitializingBean interface.");
}
public void destroy() throws Exception {
System.out.println("ClassA: destroy() called because DisposableBean interface.");
}
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("ClassA: application context set: " + arg0.getApplicationName());
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("ClassA: beanFacrory set.");
}
public void setBeanName(String arg0) {
System.out.println("ClassA: bean name set: " + arg0);
}
}public class ClassA implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware{
private String messageA;
public ClassA() {
System.out.println("ClassA: default constructor called.");
}
public void customInitMethod(){
System.out.println("ClassA: customInitMethod() method called.");
}
public void customDestroyMethod(){
System.out.println("ClassA: customDestroyMethod() method called.");
}
public String getMessageA() {
System.out.println("ClassA: message get method called.");
return messageA;
}
public void setMessageA(String message) {
System.out.println("ClassA: message set method called.");
this.messageA = message;
}
public void afterPropertiesSet() throws Exception {
System.out.println("ClassA: afterPropertiesSet() called because InitializingBean interface.");
}
public void destroy() throws Exception {
System.out.println("ClassA: destroy() called because DisposableBean interface.");
}
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("ClassA: application context set: " + arg0.getApplicationName());
}
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("ClassA: beanFacrory set.");
}
public void setBeanName(String arg0) {
System.out.println("ClassA: bean name set: " + arg0);
}
}
For CustomPostProcessor:
public class CustomPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("CustomPostProcessor: beforeInitialization on: "
+ beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("CustomPostProcessor: afterInitialization on: "
+ beanName);
return bean;
}
}
In main class we creating ApplicationContext, getting bean and printing message out:
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"META_INF/spring/app-context.xml");
ClassA objA = (ClassA) context.getBean("classA");
System.out.println(objA.getMessageA());
context.registerShutdownHook();
}
In app-context.xml we have:
<bean id="classA" class="ClassA" init-method="customInitMethod"
destroy-method="customDestroyMethod">
<property name="messageA" value="messagA: Hello Spring!" />
</bean>
<bean class="CustomPostProcessor" />
As I understand output lines correspond to life cycle stages this way:
1.Instantiate
ClassA: default constructor called.
2.Populate properties
ClassA: message set method called.
3.BeanNameAware`s setBeanName()
ClassA: bean name set: classA
4.BeanFactoryAware`s setBeanFactory
ClassA: beanFacrory set.
5.ApplicationContextAware`s setApplicationContext()
ClassA: application context set:
6.Pre-initialization BeanPostProcessors
CustomPostProcessor: beforeInitialization on: classA
7.InitializingBean`s afterPropertiesSet()
ClassA: afterPropertiesSet() called because InitializingBean interface.
8.Call custom init-method
ClassA: customInitMethod() method called.
9.Post-initialization BeanPostProcessors
CustomPostProcessor: afterInitialization on: classA
Program prints message
ClassA: message get method called.
messagA: Hello Spring!
10.DisponsableBean`s destroy
ClassA: destroy() called because DisposableBean interface.
11.Call custom destroy-method
ClassA: customDestroyMethod() method called.
I'm doing a Web application using Spring 3.1.0.RELEASE, JSF 2.x, JPA 2 with Hibernate Provider. I use PrettyFaces 3.3.2 for friendly URL. The application run on Tomcat 6.35 .
I wanted to use the Jsf ViewScope so I decided to follow the implementation found on the web : http://comdynamics.net/blog/109/spring3-jsf2-view-scope/
public class ViewScope implements Scope {
private static final Logger logger = LoggerFactory.getLogger(ViewScope.class);
#Override
public Object get(String name, ObjectFactory objectFactory) {
final Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
Object instance = viewMap.get(name);
if (instance == null) {
instance = objectFactory.getObject();
viewMap.put(name, instance);
}
return instance;
}
#Override
public Object remove(String name) {
logger.debug("ViewScope::remove {}", name);
return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
}
#Override
public String getConversationId() {
return null;
}
#Override
public void registerDestructionCallback(String name, Runnable callback) {
//Not supported
}
#Override
public Object resolveContextualObject(String key) {
return null;
}
}
I notice that #PreDestroy are not called on them like show this question #PreDestroy never called on #ViewScoped.
Does it mean that the Managed beans with ViewScope are never destruct ? Which conduct to memory leak. Should we use this scope so?
It's only happen with custom Viewscope on Spring or also on Mojarra ?
Thanks.
Problem is incorrect implementaiton of view scope. It is creates Spring bean objectFactory.getObject(); but never destroy it.
To solve it - check correct implementation with support for registerDestructionCallback.
BWT, current Mojjara implementation will not call #PreDestory on your bean too.
But it will free bean instance at least.
I tried the work around for Jsf view scope bean memory leaks using spring custom view scope. It works for both Jsf 2.1 & 2.2.Try the code in below link.
Memory leak with ViewScoped bean?
Is possible to get the proxy of a given object in Spring? I need to call a function of a subclass. But, obviously, when I do a direct call, the aspects aren't applied. Here's an example:
public class Parent {
public doSomething() {
Parent proxyOfMe = Spring.getProxyOfMe(this); // (please)
Method method = this.class.getMethod("sayHello");
method.invoke(proxyOfMe);
}
}
public class Child extends Parent {
#Secured("president")
public void sayHello() {
System.out.println("Hello Mr. President");
}
}
I've found a way of achieving this. It works, but I think is not very elegant:
public class Parent implements BeanNameAware {
#Autowired private ApplicationContext applicationContext;
private String beanName; // Getter
public doSomething() {
Parent proxyOfMe = applicationContext.getBean(beanName, Parent.class);
Method method = this.class.getMethod("sayHello");
method.invoke(proxyOfMe);
}
}
This hack is extremely awkward, please consider refactoring your code or using AspectJ weaving. You may feel warned, here is the solution
AopContext.currentProxy()
JavaDoc. I blogged about it here and here.
AopContext.currentProxy() as suggested by Tomasz will work. A more generic solution, that will work outside of the proxied class is to cast the object to org.springframework.aop.framework.Advised and get .getTargetSource().getTarget()
The former (getting the real object from the proxied object) is something that you should not really need. On the other hand getting the target proxy might be useful in some utility class that inspects existing beans in order to add some feature.
You can use a bean post-processor to set a reference to the proxy on the target bean. It moves the Spring-specifics from your beans to a single class.
Post-Processor
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class SelfReferencingBeanPostProcessor implements BeanPostProcessor {
#Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
#Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof SelfReferencingBean) {
((SelfReferencingBean) bean).setProxy(bean);
}
return bean;
}
}
Context
Register the post-processor in applicationContext.xml.
<bean id="srbpp" class="SelfReferencingBeanPostProcessor"/>
Beans
Each bean must implement SelfReferencingBean to tell the post-processor that it needs a reference to the proxy.
public interface SelfReferencingBean {
void setProxy(Object proxy) ;
}
Now implement setProxy in each bean that needs to call itself through its proxy.
public class MyBean implements SelfReferencingBean {
MyBean proxy;
#Override
public void setProxy(Object proxy) {
this.proxy = (MyBean) proxy;
}
}
You could put this last bit of code into a bean base class if you don't mind casting proxy to bean's type when calling methods directly on it. Since you're going through Method.invoke you wouldn't even need the cast.
With a little work I bet this could be converted to an annotation processor a la #Autowired. Come to think of it, I don't recall if I even tried adding a self-reference using #Autowired itself.
public class MyBean implements SelfReferencingBean {
#Autowired MyBean proxy;
}