Autowiring in servlet

I followed the solution in the following link, and it works fine: Access Spring beans from a servlet in JBoss public class MyServlet extends HttpServlet { @Autowired private MyService myService; public void init(ServletConfig config) { super.init(config); SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext()); } }

Spring Boot with embedded Tomcat behind Apache proxy

I had the same problem the other day. After some debugging of Spring Boot 1.3 I found the following solution. 1. You have to setup the headers on your Apache proxy: <VirtualHost *:443> ServerName www.myapp.org ProxyPass / http://127.0.0.1:8080/ RequestHeader set X-Forwarded-Proto https RequestHeader set X-Forwarded-Port 443 ProxyPreserveHost On … (SSL directives omitted for readability) </VirtualHost> … Read more

Rollback on every checked exception, whenever I say @Transactional

Custom Shortcut Annotations I know, that I could create a custom annotation, but that seems unnatural. No, this is exactly the use case for a Custom Annotation. Here’s a quote from Custom Shortcut Annotations in the Spring Reference: If you find you are repeatedly using the same attributes with @Transactional on many different methods, then … Read more

How to rollback a database transaction when testing services with Spring in JUnit?

You need to extend transaction boundaries to the boundaries of your test method. You can do it by annotating your test method (or the whole test class) as @Transactional: @Test @Transactional public void testInsert(){ long id=myService.addPerson(“JUNIT”); assertNotNull(id); if(id<1){ fail(); } } You can also use this approach to ensure that data was correctly written before … Read more

how to set header no cache in spring mvc 3 by annotation

There is no such option. You can use an interceptor: <mvc:annotation-driven/> <mvc:interceptors> <bean id=”webContentInterceptor” class=”org.springframework.web.servlet.mvc.WebContentInterceptor”> <property name=”cacheSeconds” value=”0″/> <property name=”useExpiresHeader” value=”true”/> <property name=”useCacheControlHeader” value=”true”/> <property name=”useCacheControlNoStore” value=”true”/> </bean> </mvc:interceptors> (taken from here) On one hand it is logical not to have such annotation. Annotations on spring-mvc methods are primarily to let the container decide which … Read more

Reloading/Refreshing Spring configuration file without restarting the servlet container

For those stumbling on this more recently — the current and modern way to solve this problem is to use Spring Boot’s Cloud Config. Just add the @RefreshScope annotation on your refreshable beans and @EnableConfigServer on your main/configuration. So, for example, this Controller class: @RefreshScope @RestController class MessageRestController { @Value(“${message}”) private String message; @RequestMapping(“/message”) String … Read more

BeanFactoryPostProcessor and BeanPostProcessor in lifecycle events

BeanFactoryPostProcessor is an interface and beans that implement it are actually beans that undergo the Spring lifecycle (Example below) but these beans don’t take part of the other declared beans’ lifecycle. public class CustomBeanFactory implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { for (String beanName : beanFactory.getBeanDefinitionNames()) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); … Read more

AbstractMethodError on deploying Spring 4.0 in Tomcat 6

The error has nothing to do with the EL. It has all to do with the javax.validation api and hibernate. java.lang.AbstractMethodError: org.hibernate.validator.internal.engine.ConfigurationImpl.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider Hibernate validator 4.3.x is an implementation of javax.validation 1.0 (JSR-303). However you are including the 1.1 API version. Either downgrade the included javax.validation version or upgrade your hibernate validator to 5.0.x.