简体   繁体   中英

how to get object from a class which annotated

I have a class lets say Test as below

@Component
public class Test implements TestInterface

@Autowired 
private interface1 i1;

There is a class C1

@Component 
public class C1 implements interface1 

and I am calling this class as below

@Bean
public void xyx(){

new Test();

when I debug then I am getting i1 as null.

Please advise what I am doing wrong

Note:. I have to use class Test inside xyx() because its a pseudo code and there lots of codes inside xyx()

You can Inject the Interface as parameter into your Bean method and then use the constructor of Test to set the field. With that you also get the advantage to set i1 final :D

@Bean
public Test xyx(YourInterface interface){
    Test test = new Test(interface);
    // do stuff 
    return Test
}

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