Use of designers in Java Spring



  • Just recently started studying Spring and a question arose. I have a configuration class of SpringConfig, which contains several bins. Also, I have a DetectiveBook class that contains a facility designer. Now, the problem is, if I want to identify a new facility, I need to write @Value for every variable in the designer (as the compiler screams and removes "could not autowire no beans of type found" and add value in the bins. Can it be possible to indicate values, for example, in the bin? SpringConfig code:

    package ru;
    

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;

    @Configuration
    @ComponentScan("ru")

    public class SpingConfig {
    @Bean(name = "detective")
    public DetectiveBook detectiveBook(){
    DetectiveBook detectiveBook = new DetectiveBook("TheFirstHuman", 326);
    detectiveBook.setNameOfAuthor("Piter Beilish");
    return detectiveBook;
    }
    /* @Bean
    public DetectiveBook dropnewdetectivBook(){
    DetectiveBook detectiveBook = new DetectiveBook(name, count);
    detectiveBook.setNameOfAuthor(nameofauthor);
    return detectiveBook;
    }*/
    @Bean(name = "library")
    public Library Library(){
    return new Library(detectiveBook());
    }

    @Bean(name = "fabric")
    public TheHistoryBook createTheHistoryBook(){
        return TheHistoryBook.getTheHistoryBook();
    }
    

    }

    DetectiveBook:

    package ru;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;

    @Component("detectiveBook")
    public class DetectiveBook implements Book {
    private String name;
    private String nameOfAuthor;
    private int countOfPages;

    DetectiveBook(@Value("TheFirstHuman") String  name, @Value("356") int countOfPages){
        this.name = name;
        this.countOfPages = countOfPages;
    }
    @Autowired
    public void setNameOfAuthor(@Value("Piter Beilish") String nameOfAuthor){
        this.nameOfAuthor = nameOfAuthor;
    }
    
    @Override
    public String dropTheBook() {
        return "Название книги: " + name + ", автор книги: " + nameOfAuthor
                + ", кол-во страниц: " + countOfPages;
    }
    @Override
    public String IwantThisBook(){
        return "Название книги: " + name + ", автор книги: " + nameOfAuthor;
    }
    

    }

    Yes, of course, I can point it in the xml file and it's much easier, but it's the kind of annotations I need.



  • Because you have class. DetectiveBook annotated @Component Spring, automatically creates bin in the context (in addition to those bands that you hand-wrapped in SpringConfig)

    At the same time, the spring has to create a copy using the designer, which means the parameters for the argument. He can't find them in the context and you have to use annotations with values.

    If you get it. @Componentit'll fix this situation.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2