BadPractice11 - SpotBugsExtensionForSpringFrameWork/CS5098 GitHub Wiki

Bug Name: avoid using the ApplicationContextAware interface.

Description

ApplicationContextAware interface

  • Why introduced?
    • Using the ApplicationContextAware interface, it is possible for your beans to get a reference to the ApplicationContext instance that configured them
    • (main reason) to allow a bean to access Spring’s ApplicationContext in your application, e.g, to acquire other Spring beans programmatically
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.GenericApplicationContext;
public class ShutdownHookBean implements ApplicationContextAware {
    private ApplicationContext ctx;
    /** @Implements {@link ApplicationContextAware#s
 etApplicationContext(ApplicationContext)} }*/
    public void setApplicationContext(ApplicationContext ctx)
        throws BeansException {
        if (ctx instanceof GenericApplicationContext) {
            ((GenericApplicationContext) ctx).registerShutdownHook();
        }
    }
}
<beans ...>
    <context:annotation-config/>
    <bean id="destructiveBean"
          class="com.apress.prospring5.ch4.DestructiveBeanWithInterface"
          p:filePath=
          "#{systemProperties'java.io.tmpdir'}#{systemProperties'file.separator'}test.txt"/>
    <bean id="shutdownHook"
          class="com.apress.prospring5.ch4.ShutdownHookBean"/>
</beans>
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import org.springframework.context.support.GenericXmlApplicationContext;
public class DestructiveBeanWithInterface {
    private File file;
    private String filePath;
    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        System.out.println("Initializing Bean");
        if (filePath == null) {
            throw new IllegalArgumentException(
                "You must specify the filePath property of " +
                DestructiveBeanWithInterface.class);
        }
        this.file = new File(filePath);
        this.file.createNewFile();
        System.out.println("File exists: " + file.exists());
    }
    @PreDestroy
    public void destroy() {
        System.out.println("Destroying Bean");
        if(!file.delete()) {
            System.err.println("ERROR: failed to delete file.");
        }
        System.out.println("File exists: " + file.exists());
    }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    public static void main(String... args) throws Exception {
        GenericXmlApplicationContext ctx =
            new GenericXmlApplicationContext();
        ctx.load("classpath:spring/app-context-annotation.xml");
        ctx.registerShutdownHook();
        ctx.refresh();
        ctx.getBean("destructiveBean",
                    DestructiveBeanWithInterface.class);
    }
}

Solutions

Avoid using this practice

Reference List

Pro Spring 5 5th edition (148 page)

⚠️ **GitHub.com Fallback** ⚠️