相关疑难解决方法(0)

Spring IoC/DI 中接口使用 @Component 注解进行注解。可能是什么原因?

有时接口会使用@Component 注解进行注解。那么我的明显推理是实现此类接口的类也将被视为组件。但如果我是对的,情况就不是这样了。

那么接口上@Component注解的目的是什么呢?

java spring dependency-injection inversion-of-control spring-ioc

9
推荐指数
1
解决办法
2万
查看次数

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

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

如何在使用自动装配时实现这一点。示例代码如下。

界面- IMessage

package com.example.demo.services;

public interface IMessage {
        String message(String name);
}
Run Code Online (Sandbox Code Playgroud)

实现-
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 ;
    }
}
Run Code Online (Sandbox Code Playgroud)

主服务

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 …
Run Code Online (Sandbox Code Playgroud)

java spring dependency-injection inversion-of-control autowired

5
推荐指数
1
解决办法
1万
查看次数