How to avoid beans which are not required in Spring during component scan?











up vote
-1
down vote

favorite












In my project we have around 30 beans in a package. I want to load only a Details bean from that package using component scan and how to avoid other beans loading ? In the below example i am interested in only on Details bean other beans not required to load? But the below one is not working?



Example :

com.spring.test
Student.java
Details.java
Contacts.java

@Configuration
@ComponentScan(basePackages={,"com.spring.test"},
includeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.Details"), excludeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.*^((?!Details).)*$"))
public class AppConfig {


}









share|improve this question




















  • 1




    Don't put 30 unrelated beans in the same package then.
    – Boris the Spider
    16 hours ago










  • it is already defined in my project i should not move to another package
    – user3094331
    16 hours ago










  • Then don't scan and just create an @Bean method for it. Also the fact that those Student and Contacts classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
    – M. Deinum
    14 hours ago

















up vote
-1
down vote

favorite












In my project we have around 30 beans in a package. I want to load only a Details bean from that package using component scan and how to avoid other beans loading ? In the below example i am interested in only on Details bean other beans not required to load? But the below one is not working?



Example :

com.spring.test
Student.java
Details.java
Contacts.java

@Configuration
@ComponentScan(basePackages={,"com.spring.test"},
includeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.Details"), excludeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.*^((?!Details).)*$"))
public class AppConfig {


}









share|improve this question




















  • 1




    Don't put 30 unrelated beans in the same package then.
    – Boris the Spider
    16 hours ago










  • it is already defined in my project i should not move to another package
    – user3094331
    16 hours ago










  • Then don't scan and just create an @Bean method for it. Also the fact that those Student and Contacts classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
    – M. Deinum
    14 hours ago















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











In my project we have around 30 beans in a package. I want to load only a Details bean from that package using component scan and how to avoid other beans loading ? In the below example i am interested in only on Details bean other beans not required to load? But the below one is not working?



Example :

com.spring.test
Student.java
Details.java
Contacts.java

@Configuration
@ComponentScan(basePackages={,"com.spring.test"},
includeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.Details"), excludeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.*^((?!Details).)*$"))
public class AppConfig {


}









share|improve this question















In my project we have around 30 beans in a package. I want to load only a Details bean from that package using component scan and how to avoid other beans loading ? In the below example i am interested in only on Details bean other beans not required to load? But the below one is not working?



Example :

com.spring.test
Student.java
Details.java
Contacts.java

@Configuration
@ComponentScan(basePackages={,"com.spring.test"},
includeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.Details"), excludeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.*^((?!Details).)*$"))
public class AppConfig {


}






java regex spring spring-mvc spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 16 hours ago

























asked 18 hours ago









user3094331

1009




1009








  • 1




    Don't put 30 unrelated beans in the same package then.
    – Boris the Spider
    16 hours ago










  • it is already defined in my project i should not move to another package
    – user3094331
    16 hours ago










  • Then don't scan and just create an @Bean method for it. Also the fact that those Student and Contacts classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
    – M. Deinum
    14 hours ago
















  • 1




    Don't put 30 unrelated beans in the same package then.
    – Boris the Spider
    16 hours ago










  • it is already defined in my project i should not move to another package
    – user3094331
    16 hours ago










  • Then don't scan and just create an @Bean method for it. Also the fact that those Student and Contacts classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
    – M. Deinum
    14 hours ago










1




1




Don't put 30 unrelated beans in the same package then.
– Boris the Spider
16 hours ago




Don't put 30 unrelated beans in the same package then.
– Boris the Spider
16 hours ago












it is already defined in my project i should not move to another package
– user3094331
16 hours ago




it is already defined in my project i should not move to another package
– user3094331
16 hours ago












Then don't scan and just create an @Bean method for it. Also the fact that those Student and Contacts classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
– M. Deinum
14 hours ago






Then don't scan and just create an @Bean method for it. Also the fact that those Student and Contacts classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
– M. Deinum
14 hours ago














2 Answers
2






active

oldest

votes

















up vote
0
down vote













Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.






share|improve this answer








New contributor




Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    0
    down vote













    Your Java config file should be marked with @Configuration annotation. After then your Spring application loads configuration from this file.



    See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236602%2fhow-to-avoid-beans-which-are-not-required-in-spring-during-component-scan%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.






      share|improve this answer








      New contributor




      Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        up vote
        0
        down vote













        Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.






        share|improve this answer








        New contributor




        Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















          up vote
          0
          down vote










          up vote
          0
          down vote









          Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.






          share|improve this answer








          New contributor




          Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.







          share|improve this answer








          New contributor




          Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 17 hours ago









          Ajish Thankachan

          411




          411




          New contributor




          Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Ajish Thankachan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.
























              up vote
              0
              down vote













              Your Java config file should be marked with @Configuration annotation. After then your Spring application loads configuration from this file.



              See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html






              share|improve this answer

























                up vote
                0
                down vote













                Your Java config file should be marked with @Configuration annotation. After then your Spring application loads configuration from this file.



                See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Your Java config file should be marked with @Configuration annotation. After then your Spring application loads configuration from this file.



                  See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html






                  share|improve this answer












                  Your Java config file should be marked with @Configuration annotation. After then your Spring application loads configuration from this file.



                  See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 17 hours ago









                  h0nzan

                  1812213




                  1812213






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236602%2fhow-to-avoid-beans-which-are-not-required-in-spring-during-component-scan%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      Popular posts from this blog

                      Full-time equivalent

                      Bicuculline

                      さくらももこ