Unable to Autowire through constructor












0















I am trying to Autowire a class through constructor.



@Component
public class Test<T extends Something>{

@Autowired
public Test(Class<T> entity)
doSomething(enity);
}
...


When I run the code I keep get the error message



Parameter 0 of constructor in com.test.Test required a bean of type 'java.lang.Class' that could not be found.
Action:
Consider defining a bean of type 'java.lang.Class' in your configuration.


Could someone please tell me where I am going wrong here. Thanks.










share|improve this question























  • The error is telling you exactly what is wrong... There needs to be an instance of a class Class but I doubt that is something that is going to happen.

    – M. Deinum
    Nov 13 '18 at 19:09











  • The argument of the constructor should be a concrete class.

    – LunaticJape
    Nov 13 '18 at 19:32











  • What are you trying to accomplish with this? I cannot think of any valid use case where it makes sense to wire in a class instance. I'm eager/anxious to learn, though.

    – Makoto
    Nov 13 '18 at 23:27
















0















I am trying to Autowire a class through constructor.



@Component
public class Test<T extends Something>{

@Autowired
public Test(Class<T> entity)
doSomething(enity);
}
...


When I run the code I keep get the error message



Parameter 0 of constructor in com.test.Test required a bean of type 'java.lang.Class' that could not be found.
Action:
Consider defining a bean of type 'java.lang.Class' in your configuration.


Could someone please tell me where I am going wrong here. Thanks.










share|improve this question























  • The error is telling you exactly what is wrong... There needs to be an instance of a class Class but I doubt that is something that is going to happen.

    – M. Deinum
    Nov 13 '18 at 19:09











  • The argument of the constructor should be a concrete class.

    – LunaticJape
    Nov 13 '18 at 19:32











  • What are you trying to accomplish with this? I cannot think of any valid use case where it makes sense to wire in a class instance. I'm eager/anxious to learn, though.

    – Makoto
    Nov 13 '18 at 23:27














0












0








0








I am trying to Autowire a class through constructor.



@Component
public class Test<T extends Something>{

@Autowired
public Test(Class<T> entity)
doSomething(enity);
}
...


When I run the code I keep get the error message



Parameter 0 of constructor in com.test.Test required a bean of type 'java.lang.Class' that could not be found.
Action:
Consider defining a bean of type 'java.lang.Class' in your configuration.


Could someone please tell me where I am going wrong here. Thanks.










share|improve this question














I am trying to Autowire a class through constructor.



@Component
public class Test<T extends Something>{

@Autowired
public Test(Class<T> entity)
doSomething(enity);
}
...


When I run the code I keep get the error message



Parameter 0 of constructor in com.test.Test required a bean of type 'java.lang.Class' that could not be found.
Action:
Consider defining a bean of type 'java.lang.Class' in your configuration.


Could someone please tell me where I am going wrong here. Thanks.







java spring spring-mvc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 19:05









Sesha SwarupSesha Swarup

122




122













  • The error is telling you exactly what is wrong... There needs to be an instance of a class Class but I doubt that is something that is going to happen.

    – M. Deinum
    Nov 13 '18 at 19:09











  • The argument of the constructor should be a concrete class.

    – LunaticJape
    Nov 13 '18 at 19:32











  • What are you trying to accomplish with this? I cannot think of any valid use case where it makes sense to wire in a class instance. I'm eager/anxious to learn, though.

    – Makoto
    Nov 13 '18 at 23:27



















  • The error is telling you exactly what is wrong... There needs to be an instance of a class Class but I doubt that is something that is going to happen.

    – M. Deinum
    Nov 13 '18 at 19:09











  • The argument of the constructor should be a concrete class.

    – LunaticJape
    Nov 13 '18 at 19:32











  • What are you trying to accomplish with this? I cannot think of any valid use case where it makes sense to wire in a class instance. I'm eager/anxious to learn, though.

    – Makoto
    Nov 13 '18 at 23:27

















The error is telling you exactly what is wrong... There needs to be an instance of a class Class but I doubt that is something that is going to happen.

– M. Deinum
Nov 13 '18 at 19:09





The error is telling you exactly what is wrong... There needs to be an instance of a class Class but I doubt that is something that is going to happen.

– M. Deinum
Nov 13 '18 at 19:09













The argument of the constructor should be a concrete class.

– LunaticJape
Nov 13 '18 at 19:32





The argument of the constructor should be a concrete class.

– LunaticJape
Nov 13 '18 at 19:32













What are you trying to accomplish with this? I cannot think of any valid use case where it makes sense to wire in a class instance. I'm eager/anxious to learn, though.

– Makoto
Nov 13 '18 at 23:27





What are you trying to accomplish with this? I cannot think of any valid use case where it makes sense to wire in a class instance. I'm eager/anxious to learn, though.

– Makoto
Nov 13 '18 at 23:27












1 Answer
1






active

oldest

votes


















0














It says you that it could not find Class class marked as Bean etc
So you need to have a class wanted to be injected declared as @Bean ,@Component etc.



Here is an example:



@Configuration
public class Config {

@Bean
public<T> Class<T> tClass(){
return (some class to be returned);// you need to generify or pass some type of class which you want
}

}


//Here is injecting with no problems
@Component
public class Test<T> {

private Class<T> tClass;

@Autowired
public Test(Class<T> tClass) {
this.tClass = tClass;
}
}


Some how its better do define such architecture which would be better in this case:



  public interface Foo<T> {

Class<T> getClassFromType();
}

@Component
public class FooIntegerImpl implements Foo<Integer>{


@Override
public Class<Integer> getClassFromType() {
return Integer.class;
}
}

@Component
public class FooStringImpl implements Foo<String>{

@Override
public Class<String> getClassFromType() {
return String.class;
}
}

@Component
public class Test {

private List<Foo> foo;

@Autowired
public Test(List<Foo> foo) {
this.foo = foo;
}

}


For such purposes for example you can define generic API which would be common for all cases , actually you can define AbstractCrudOperations and define crud things when someone need to inherit it would define type of object which need to be in and will have some methods defined



Actually in your case i dont know the logic what you want to implement but basic error is that Class could not be found as bean



I think this is helpful for you






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',
    autoActivateHeartbeat: false,
    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%2f53287895%2funable-to-autowire-through-constructor%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It says you that it could not find Class class marked as Bean etc
    So you need to have a class wanted to be injected declared as @Bean ,@Component etc.



    Here is an example:



    @Configuration
    public class Config {

    @Bean
    public<T> Class<T> tClass(){
    return (some class to be returned);// you need to generify or pass some type of class which you want
    }

    }


    //Here is injecting with no problems
    @Component
    public class Test<T> {

    private Class<T> tClass;

    @Autowired
    public Test(Class<T> tClass) {
    this.tClass = tClass;
    }
    }


    Some how its better do define such architecture which would be better in this case:



      public interface Foo<T> {

    Class<T> getClassFromType();
    }

    @Component
    public class FooIntegerImpl implements Foo<Integer>{


    @Override
    public Class<Integer> getClassFromType() {
    return Integer.class;
    }
    }

    @Component
    public class FooStringImpl implements Foo<String>{

    @Override
    public Class<String> getClassFromType() {
    return String.class;
    }
    }

    @Component
    public class Test {

    private List<Foo> foo;

    @Autowired
    public Test(List<Foo> foo) {
    this.foo = foo;
    }

    }


    For such purposes for example you can define generic API which would be common for all cases , actually you can define AbstractCrudOperations and define crud things when someone need to inherit it would define type of object which need to be in and will have some methods defined



    Actually in your case i dont know the logic what you want to implement but basic error is that Class could not be found as bean



    I think this is helpful for you






    share|improve this answer




























      0














      It says you that it could not find Class class marked as Bean etc
      So you need to have a class wanted to be injected declared as @Bean ,@Component etc.



      Here is an example:



      @Configuration
      public class Config {

      @Bean
      public<T> Class<T> tClass(){
      return (some class to be returned);// you need to generify or pass some type of class which you want
      }

      }


      //Here is injecting with no problems
      @Component
      public class Test<T> {

      private Class<T> tClass;

      @Autowired
      public Test(Class<T> tClass) {
      this.tClass = tClass;
      }
      }


      Some how its better do define such architecture which would be better in this case:



        public interface Foo<T> {

      Class<T> getClassFromType();
      }

      @Component
      public class FooIntegerImpl implements Foo<Integer>{


      @Override
      public Class<Integer> getClassFromType() {
      return Integer.class;
      }
      }

      @Component
      public class FooStringImpl implements Foo<String>{

      @Override
      public Class<String> getClassFromType() {
      return String.class;
      }
      }

      @Component
      public class Test {

      private List<Foo> foo;

      @Autowired
      public Test(List<Foo> foo) {
      this.foo = foo;
      }

      }


      For such purposes for example you can define generic API which would be common for all cases , actually you can define AbstractCrudOperations and define crud things when someone need to inherit it would define type of object which need to be in and will have some methods defined



      Actually in your case i dont know the logic what you want to implement but basic error is that Class could not be found as bean



      I think this is helpful for you






      share|improve this answer


























        0












        0








        0







        It says you that it could not find Class class marked as Bean etc
        So you need to have a class wanted to be injected declared as @Bean ,@Component etc.



        Here is an example:



        @Configuration
        public class Config {

        @Bean
        public<T> Class<T> tClass(){
        return (some class to be returned);// you need to generify or pass some type of class which you want
        }

        }


        //Here is injecting with no problems
        @Component
        public class Test<T> {

        private Class<T> tClass;

        @Autowired
        public Test(Class<T> tClass) {
        this.tClass = tClass;
        }
        }


        Some how its better do define such architecture which would be better in this case:



          public interface Foo<T> {

        Class<T> getClassFromType();
        }

        @Component
        public class FooIntegerImpl implements Foo<Integer>{


        @Override
        public Class<Integer> getClassFromType() {
        return Integer.class;
        }
        }

        @Component
        public class FooStringImpl implements Foo<String>{

        @Override
        public Class<String> getClassFromType() {
        return String.class;
        }
        }

        @Component
        public class Test {

        private List<Foo> foo;

        @Autowired
        public Test(List<Foo> foo) {
        this.foo = foo;
        }

        }


        For such purposes for example you can define generic API which would be common for all cases , actually you can define AbstractCrudOperations and define crud things when someone need to inherit it would define type of object which need to be in and will have some methods defined



        Actually in your case i dont know the logic what you want to implement but basic error is that Class could not be found as bean



        I think this is helpful for you






        share|improve this answer













        It says you that it could not find Class class marked as Bean etc
        So you need to have a class wanted to be injected declared as @Bean ,@Component etc.



        Here is an example:



        @Configuration
        public class Config {

        @Bean
        public<T> Class<T> tClass(){
        return (some class to be returned);// you need to generify or pass some type of class which you want
        }

        }


        //Here is injecting with no problems
        @Component
        public class Test<T> {

        private Class<T> tClass;

        @Autowired
        public Test(Class<T> tClass) {
        this.tClass = tClass;
        }
        }


        Some how its better do define such architecture which would be better in this case:



          public interface Foo<T> {

        Class<T> getClassFromType();
        }

        @Component
        public class FooIntegerImpl implements Foo<Integer>{


        @Override
        public Class<Integer> getClassFromType() {
        return Integer.class;
        }
        }

        @Component
        public class FooStringImpl implements Foo<String>{

        @Override
        public Class<String> getClassFromType() {
        return String.class;
        }
        }

        @Component
        public class Test {

        private List<Foo> foo;

        @Autowired
        public Test(List<Foo> foo) {
        this.foo = foo;
        }

        }


        For such purposes for example you can define generic API which would be common for all cases , actually you can define AbstractCrudOperations and define crud things when someone need to inherit it would define type of object which need to be in and will have some methods defined



        Actually in your case i dont know the logic what you want to implement but basic error is that Class could not be found as bean



        I think this is helpful for you







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 23:24









        Mykhailo MoskuraMykhailo Moskura

        838113




        838113






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287895%2funable-to-autowire-through-constructor%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python