execute method after all dependencies are injected

Hi,

In Spring, if you want to execute a method after all the dependencies are injected, you can use InitializingBean interface.

A Sample code is as follows :

import org.springframework.beans.factory.InitializingBean;

public class MyViewClass implements InitializingBean {

@Autowired

private AnotherBean anotherBean;

@Autowired

private SomeOtherBean someOtherBean;
.....
@Override
public void afterPropertiesSet() throws Exception {
someOtherBean.someMethod();
// any other thing you want as everything is injected...
}

}

calling method after properties are set in Spring

Hi,

if you want to call a method which you want to be run after all the injections are done just do the following:

1. Make your class implement org.springframework.beans.factory.InitializingBean
2. override afterPropertiesSet method

and that is it. Sample code will be like:

public class MyBean implements InitializingBean {

@Autowired
private MyDao dao;

@Autowired
private MyOtherDao otherDao;

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("this will be called after all the setters are done.");
}
}

Remember me in Spring Security

Hi,

I love Spring when it is very easy to add new stuff to your current projects. Recently I was asked to add Remember me checkbox to login page. If you are using Spring Security what you just need to do is as follows:

add this to your spring xml : <remember-me key="Your_key_with_values_like*!casAA='0401./e;2009NrI"/>

and add a checkbox to your login page ( id is important here ) : <h:selectBooleanCheckbox id="_spring_security_remember_me" />

and that is. Spring will do all the work for you.

Just remember if you have an exception about transactions, just add a @Transactional to your method which loads the user from the DB.

Using Enums with map in Spring

If you want to use enums as a key in a map in spring, you can use the following code in your spring.xml:

<bean id="fileCopyService" class="my.package.FileCopyService">
<property name="fileCopier">
<map>
<entry key="IMAGE">
<ref bean="imageFileCopier"/>
</entry>
<entry key="VIDEO">
<ref bean="videoFileCopier"/>
</entry>
<entry key="AUDIO">
<ref bean="audioFileCopier"/>
</entry>
</map>
</property>
</bean>

<bean id="imageFileCopier" class="my.package.ImageFileCopier"/>
<bean id="videoFileCopier" class="my.package.VideoFileCopier"/>
<bean id="audioFileCopier" class="my.package.AudioFileCopier"/>

and your FileCopyService class is as follows:

private Map<ContentType, FileCopier> fileCopier = new HashMap<ContentType, FileCopier>();

and that is all. So you can access a fileCopier withing the code with :

fileCopier.get(getContentType())

no if – else statements, or long switches.

View scope in Spring

If you want to have View Scope in Spring for JSF, firstly read here and here.

The problem with above was, those needed our @Scope(“view”) beans to be serialized.

We didn’t want to serialize our beans so we did the following workaround:

we defined our Autowired DAO classes as :

@Autowired
private transient DAO myDao;

and based on Rick’s above ViewScope implementation we added those 2 lines of code:

public class ViewScope implements Scope, ApplicationContextAware

….

@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}


and finally in get method, after getting the object we did

applicationContext.getAutowireCapableBeanFactory().autowireBean(objToReturn);

to autowire the dependencies again.

hope it helps for you also…