Аннотация @Configuration не разрешена для этого местоположения

Следуя руководству по дзона, чтобы создать простой проект spring + maven, я получаю сообщение об ошибке @Configuration is disallowed for this location.

Мои занятия -

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();
    }
}
0
0
3 834
2

Ответы 2

@Configuration может применяться только к элементу TYPE (класс), поэтому вы не можете не разместить эту аннотацию в середине вашего кода следующим образом:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration

Цель аннотации @Configuration - указать класс, в котором Spring найдет объекты (@Bean) для хранения в контексте Spring, чтобы они были доступны во всем приложении.

Чтобы указать такой класс, поместите @Configuration в начало объявления класса. Spring автоматически считывает методы @Bean для извлечения и хранения объектов bean-компонентов.

Другие вопросы по теме