Ошибка использования компонента: тип аннотации неприменим к этому виду объявления - PullRequest
1 голос
/ 08 мая 2020

Я пытаюсь создать bean-компонент, к которому можно будет получить доступ через Inject / Autowired. Здесь я создал bean-компонент в ApplicationConfig. java,

import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanBuilder {
   @Bean
   public static String beanBuild() {
    return "Hello World";
   }
}

И здесь я использую Bean в моем Java файле,

 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;

 import javax.inject.Inject;
 import javax.inject.Named;
 import org.springframework.beans.factory.annotation.Autowired;
 @Controller
 @RequestMapping("/json/buildBean")
 public class BuildBean {
      @Autowired
      String beanBuild;

      @RequestMapping("/build")
      public String returnMessage(){
            return beanBuild;
      }
}

И я получаю эту ошибку:

error: annotation type not applicable to this kind of declaration
[javac]         @Autowired

[javac]         ^
[javac] 1 error

Правильно ли я делаю это?

PS: Используемая версия Spring - 4.2.x, и я не думаю, что она устарела.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...