简体   繁体   中英

How to Create multiple Dependent beans for a class Without using XML File?

I have created a Spring Project With 2 Model classes, one dependent on other With @Autowired.

How can I Inject fully created Son Object in the John Class.

Currently I am getting Null Value for the other class.

package com.example.demo;

import org.springframework.stereotype.Component;

@Component
public class Son {

    private String sonAge;

    public String getNewsName() {
        return sonAge;
    }

    public void setNewsName(String sonAge) {
        this.sonAge = sonAge;
    }

    @Override
    public String toString() {
        return "Son [sonAge=" + sonAge + "]";
    }

}


    package com.example.demo;

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

    @Component
    public class John {

        private Son son;

        public Son getSon() {
            return son;
        }
        @Autowired
        public void setSon(Son son) {
            this.son = son;
        }
        public John() {

        }
        @Override
        public String toString() {
            return "John [son=" + son + "]";
        }


}

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class NewTestDependApplication {



    public static void main(String[] args) {
        SpringApplication.run(NewTestDependApplication.class, args);

          ApplicationContext context = new AnnotationConfigApplicationContext(NewTestDependApplication.class); 
          John john = (John)context.getBean("john");
         System.out.println(john);


    }

}

The Output I am getting is "John [son=Son [sonAge=null]]" sonAge is coming null, how to create a son object and inject it.

I want to create 10 Different object of John with 10 Different object of Son, all will have a different value. I don't want to use XML or @Bean Method to Create the Object.

I am new to the Concept so cut me some slack.

If you want to give Son an age you could set during initialization of the field: private String sonAge = "thousand"; . Other than that I don't see any other option if @Bean is not liked from your side.

If you want to have multiple instances of these you will need to use some Qualifiers .

Here an example:

@Configuration
public class SonConfig {

    @Bean
    @Qualifier("son1")
    public Son son1() {
        // Set what ever you like here
        final Son son = new Son();
        return son;
    }
}

Which then could be used in John using the qualifier:

@Component
public class John {
    ...

    @Autowired
    @Qualifier("son1")
    public void setSon(final Son son) {
        this.son = son;
    }
    ...
}

If you are not only doing this for learning purpose I'd suggest using some kind of persistence solution or something similar. Hard to say without knowing any use case but using dependency injection for models usually does not really add up.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM