gpt4 book ai didi

java - 如何在使用spring自动布线时传递构造函数参数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:17 25 4
gpt4 key购买 nike

我们的项目使用 spring DI/IoC,所以我使用 Autowiring 来注入(inject) bean。该程序需要在实例化期间将参数传递给对象。并且参数在运行时是已知的(而不是在编译时)。

如何在使用 Autowiring 时实现这一点。示例代码如下。

界面 - IMessage

package com.example.demo.services;

public interface IMessage {
String message(String name);
}

实现 -
SayHelloService

package com.example.demo.services;

import org.springframework.stereotype.Service;

@Service
public class SayHelloService implements IMessage {

String id;

public SayHelloService(String id) {
super();
this.id = id;
}

@Override
public String message(String name) {
return "Hello Dear User - " + name + ". Greeter Id: " + id ;
}
}

主服务

package com.example.demo.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class MasterService implements IMessage {

String creationTime;

MasterService() {
System.out.println("ms... default constructor");
creationTime = Long.toString(System.currentTimeMillis());
}

//classic java way of creating service
IMessage sayHelloServiceClassicWay = new SayHelloService(creationTime);

//how to achieve above using spring auto wiring. Below code does not exactly do same.
@Autowired
@Qualifier("sayHelloService")
IMessage sayHelloServiceAutoWired;

@Override
public String message(String name) {
return name.toString();
}
}

现在上面程序中(在MasterService中)如何替换

IMessage sayHelloServiceClassicWay = new SayHelloService(creationTime);

使用 spring 等效代码。

最佳答案

Spring 不是这样工作的。

您的两个 bean 在执行和实例化方面过于耦合:创建第一个时,它是在构造期间创建的,第二个在运行时在参数构造函数中将生成的值传递给它。

即使使用依赖注入(inject)顺序(@DependsOn@Order 或两个 @Configuration 哪个依赖另一个)也无法解决您的问题,因为运行时生成的值 < strong>那不是依赖项。

作为解决方法,在 IMessage 接口(interface)中提供一个方法来赋值 once creationTime 可能是可以接受的。
SayHelloService 可能看起来像:

package com.example.demo.services;

import org.springframework.stereotype.Service;

@Service
public class SayHelloService implements IMessage {

String id;

public SayHelloService(String id) {
super();
this.id = id;
}

@Override
public void setId(String id){
// you can add this check to enforce the immutability of id
if (this.id != null){//exception handling}
this.id = id;
}

@Override
public String message(String name) {
return "Hello Dear User - " + name + ". Greeter Id: " + id ;
}
}

您可以这样更改 MasterService :

private IMessage sayHelloServiceAutoWired;

@Autowired
MasterService( @Qualifier("sayHelloService")
IMessage sayHelloServiceAutoWired) {
System.out.println("ms... default constructor");
creationTime = Long.toString(System.currentTimeMillis());
this.sayHelloServiceAutoWired = sayHelloServiceAutoWired;
this.sayHelloServiceAutoWired.setId(creationTime);
}

PS: Autowiring 构造函数不是强制性的,但它没有 API 来设置类的依赖关系更清晰。您也可以使用二传手。

关于java - 如何在使用spring自动布线时传递构造函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46639069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com