我有一个服务类,我想用构造函数参数的不同传入值动态初始化它:
@Service
public class SomeServiceImpl implements SomeService {
private final SomeProperties someProperties;
private final String url;
private final String password;
private final Logger log = LoggerFactory.getLogger(SomeServiceImpl.class);
@Autowired
public SomeServiceImpl(SomeProperties someProperties,
@Autowired(required = false) String url,
@Autowired(required = false) String password) {
this.someProperties = someProperties;
this.url = url;
this.password = password;
}
是否可以在运行时@Service使用自己提供的@Autowired(required = false)参数(在本例中为自己的 url 和密码)在另一个 spring 组件类中对其进行初始化?这段代码会是什么样子?
隔江千里
收到一只叮咚
相关分类