简体   繁体   English

Spring依赖注入到具有构造函数的类

[英]Spring dependency injection to a class with a constructor

I'm kind of new to the whole spring dependency injection.我对整个 spring 依赖注入有点陌生。 There is one thing that I don't understand and hopefully someone could clear that up for me...有一件事我不明白,希望有人可以为我澄清......

I have a service:我有一个服务:

@Service
public class Service {...}

And another class:另一个类:

public class A{
   @Autowired
   private Service service;
   ...
}

In this case, Service will be injected into class A as expected.在这种情况下,Service 将按预期注入到 A 类中。 As far as I know, you cannot use a constructor in class A, otherwise the injection will not work.据我所知,A 类中不能使用构造函数,否则注入将不起作用。 Is there a way to use a constructor in class A and inject Service at the same time?有没有办法在 A 类中使用构造函数并同时注入 Service? ie: IE:

public class A{
       @Autowired
       private Service service;

       private String a;
       private int b;
       public A(String a,int b){
        this.a = a;
        this.b = b;
       }
    }

In this case I'm getting that service is null, anyway to work that out?在这种情况下,我得到该服务为空,无论如何要解决这个问题? I want to be able to create an object from type A using "new" with the Service injected to it, is that possible?我希望能够使用注入了服务的“new”从类型 A 创建一个对象,这可能吗?

Thanks.谢谢。

The important bit is that both of your classes should be Spring beans.重要的一点是你的两个类都应该是 Spring bean。

This in turn means that all the instantiation will be done by Spring container.这反过来意味着所有实例化都将由 Spring 容器完成。 To learn how to properly handle constructor-based dependency injection, check this bit of ref doc要了解如何正确处理基于构造函数的依赖注入,请查看参考文档的这一点

If whysoever, you can't make a class A a Spring bean, than the recommended way is to annotate it with @Configurable which will autowire dependencies on creation time.如果无论如何,您不能将A类设为 Spring bean,那么推荐的方法是使用@Configurable对其进行注释,这将在创建时自动装配依赖项。

Note that for this, you'll need to enable aspects.请注意,为此,您需要启用方面。 I did a quick search and found this blog that seems to do a good job explaining the details我做了一个快速搜索,发现这个博客似乎很好地解释了细节

There is.有。 You should manually create the instance of your service class in a Spring configuration class (annotated with @Configuration ) and annotate the method that creates the instance with @Service , instead of the class.您应该在 Spring 配置类(用@Configuration注释)中手动创建服务类的实例,并用@Service而不是类来注释创建实例的方法。

So, as an example, your application might have a Spring configuration that looks like this:因此,作为示例,您的应用程序可能具有如下所示的 Spring 配置:

@Configuration
public class MyApplicationConfig {
    @Bean
    public Service myService() {
        return new Service("hello", "world", 42); // ctor args as an example
    }
}

And your service class:还有你的服务类:

// need fully qualified class because your class has the same name
@org.springframework.stereotype.Service
public class Service {
    public Service(String prefix, String suffix, int number) {
        // Whatever
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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