Filter specific packages in @ComponentScan

You simply need to create two Config classes, for the two @ComponentScan annotations that you require. So for example you would have one Config class for your foo.bar package: @Configuration @ComponentScan(basePackages = {“foo.bar”}, excludeFilters = @ComponentScan.Filter(value = Service.class, type = FilterType.ANNOTATION) ) public class FooBarConfig { } and then a 2nd Config class for your … Read more

Bean injection inside a JPA @Entity

You can inject dependencies into objects not managed by the Spring container using @Configurable as explained here: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html#aop-atconfigurable. As you’ve realized by now, unless using the @Configurable and appropriate AspectJ weaving configuration, Spring does not inject dependencies into objects created using the new operator. In fact, it doesn’t inject dependencies into objects unless you’ve retrieved … Read more

How to get the @RequestBody in an @ExceptionHandler (Spring REST)

You can reference the request body object to a request-scoped bean. And then inject that request-scoped bean in your exception handler to retrieve the request body (or other request-context beans that you wish to reference). // @Component // @Scope(“request”) @ManagedBean @RequestScope public class RequestContext { // fields, getters, and setters for request-scoped beans } @RestController … Read more

How Do I Create Many to Many Hibernate Mapping for Additional Property from the Join Table?

You need to use @EmbeddedId and @Embeddable annotations to solve this issue: Lecturer Class: @Entity @Table(name=”LECTURER”) public class Lecturer { @OneToMany(fetch = FetchType.LAZY, mappedBy = “pk.lecturer”, cascade=CascadeType.ALL) Set<LecturerCourse> lecturerCourses == new HashSet<LecturerCourse>(); //all others properties Setters and getters are less relevant. } Course class: @Entity @Table(name=”COURSE”) public class Course { @OneToMany(fetch = FetchType.LAZY, mappedBy = … Read more

automatically add header to every response

I recently got into this issue and found this solution. You can use a filter to add these headers : import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.filter.OncePerRequestFilter; public class CorsFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.addHeader(“Access-Control-Allow-Origin”, “*”); if (request.getHeader(“Access-Control-Request-Method”) … Read more

How to java-configure separate datasources for spring batch data and business data? Should I even do it?

Ok, this is strange but it works. Moving the datasources to it’s own configuration class works just fine and one is able to autowire. The example is a multi-datasource version of Spring Batch Service Example: DataSourceConfiguration: public class DataSourceConfiguration { @Value(“classpath:schema-mysql.sql”) private Resource schemaScript; @Bean @Primary public DataSource hsqldbDataSource() throws SQLException { final SimpleDriverDataSource dataSource … Read more

can I include user information while issuing an access token?

You will need to implement a custom TokenEnhancer like so: public class CustomTokenEnhancer implements TokenEnhancer { @Override public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) { User user = (User) authentication.getPrincipal(); final Map<String, Object> additionalInfo = new HashMap<>(); additionalInfo.put(“customInfo”, “some_stuff_here”); additionalInfo.put(“authorities”, user.getAuthorities()); ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); return accessToken; } } and add it to your AuthorizationServerConfigurerAdapter as a bean … Read more

Spring catch all route for index.html

Since my react app could use the root as forward target this ended up working for me @Configuration public class WebConfiguration extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController(“/{spring:\\w+}”) .setViewName(“forward:/”); registry.addViewController(“/**/{spring:\\w+}”) .setViewName(“forward:/”); registry.addViewController(“/{spring:\\w+}/**{spring:?!(\\.js|\\.css)$}”) .setViewName(“forward:/”); } } To be honest I have no idea why it has to be exactly in this specific format … Read more

Spring Data JPA – Multiple EnableJpaRepositories

In order to let spring knows what DataSource is related to what Repository you should define it at the @EnableJpaRepositories annotation. Let’s assume that we have two entities, the Servers entity and the Domains entity and each one has its own Repo then each Repository has its own JpaDataSource configuration. 1. Group all the repositories … Read more