What exactly is Field Injection and how to avoid it?

Injection types There are three options for how dependencies can be injected into a bean: Through a constructor Through setters or other methods Through reflection, directly into fields You are using option 3. That is what is happening when you use @Autowired directly on your field. Injection guidelines A general guideline, which is recommended by … Read more

Spring Security : Multiple HTTP Config not working

Look at the Spring Security Reference: @EnableWebSecurity public class MultiHttpSecurityConfig { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) { 1 auth .inMemoryAuthentication() .withUser(“user”).password(“password”).roles(“USER”).and() .withUser(“admin”).password(“password”).roles(“USER”, “ADMIN”); } @Configuration @Order(1) 2 public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception { http .antMatcher(“/api/**”) 3 .authorizeRequests() .anyRequest().hasRole(“ADMIN”) .and() .httpBasic(); } } @Configuration 4 public static class … Read more

How to solve the “failed to lazily initialize a collection of role” Hibernate exception

If you know that you’ll want to see all Comments every time you retrieve a Topic then change your field mapping for comments to: @OneToMany(fetch = FetchType.EAGER, mappedBy = “topic”, cascade = CascadeType.ALL) private Collection<Comment> comments = new LinkedHashSet<Comment>(); Collections are lazy-loaded by default, take a look at this if you want to know more.

JQuery, Spring MVC @RequestBody and JSON – making it work together

I’m pretty sure you only have to register MappingJacksonHttpMessageConverter (the easiest way to do that is through <mvc:annotation-driven /> in XML or @EnableWebMvc in Java) See: this forum post and 7.6.5 Configuring Formatting in Spring MVC Here’s a working example: Maven POM <project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”> <modelVersion>4.0.0</modelVersion><groupId>test</groupId><artifactId>json</artifactId><packaging>war</packaging> <version>0.0.1-SNAPSHOT</version><name>json test</name> <dependencies> <dependency><!– spring mvc –> … Read more

Using env variable in Spring Boot’s application.properties

You don’t need to use java variables. To include system env variables add the following to your application.properties file: spring.datasource.url = ${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/”nameofDB” spring.datasource.username = ${OPENSHIFT_MYSQL_DB_USERNAME} spring.datasource.password = ${OPENSHIFT_MYSQL_DB_PASSWORD} But the way suggested by @Stefan Isele is more preferable, because in this case you have to declare just one env variable: spring.profiles.active. Spring will read the … Read more

What is @ModelAttribute in Spring MVC?

@ModelAttribute refers to a property of the Model object (the M in MVC 😉 so let’s say we have a form with a form backing object that is called “Person” Then you can have Spring MVC supply this object to a Controller method by using the @ModelAttribute annotation: public String processForm(@ModelAttribute(“person”) Person person){ person.getStuff(); } … Read more

What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

Web Application context extended Application Context which is designed to work with the standard javax.servlet.ServletContext so it’s able to communicate with the container. public interface WebApplicationContext extends ApplicationContext { ServletContext getServletContext(); } Beans, instantiated in WebApplicationContext will also be able to use ServletContext if they implement ServletContextAware interface package org.springframework.web.context; public interface ServletContextAware extends Aware … Read more

@RequestParam vs @PathVariable

@PathVariable is to obtain some placeholder from the URI (Spring call it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI Template Patterns @RequestParam is to obtain a parameter from the URI as well — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam If the URL http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013 gets the … Read more