Reputation: 816
While following the tutorial on dzone to make a simple spring+maven project, i am getting error message @Configuration is disallowed for this location.
My class -
package com.xyz;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@EnableWebMvc
@ComponentScan(basePackages = "com.example")
public class SpringConfig extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver viewResolver() {@Configuration
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Upvotes: 0
Views: 6610
Reputation: 156
Purpose of @Configuration annotation is to specify a class where spring will find Objects (@Bean) to hold in spring context so that they are available through out the application.
To specify such class put @Configuration on the top of class declaration. Spring will automatically read @Bean methods to fetch and store bean objects.
Upvotes: 1
Reputation: 18235
@Configuration
is apply-able to element TYPE (class) only, so you can't not place that annotation in the middle of your code like that:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration
Upvotes: 0