相关疑难解决方法(0)

Spring:为什么我们自动连接接口而不是实现的类?

interface IA
{
  public void someFunction();
}

@Resource(name="b")
class B implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someBfunc()
  {
     //doing b things
  }
}

@Resource(name="c")
class C implements IA
{
  public void someFunction()
  {
    //busy code block
  }
  public void someCfunc()
  {
     //doing C things
  }
}

class MyRunner
{

  @Autowire
  @Qualifier("b") 
  IA worker;

  worker.someFunction();
}
Run Code Online (Sandbox Code Playgroud)

谁可以给我解释一下这个.

  • spring如何知道要使用哪种多态类型.
  • 我需要@Qualifier还是@Resource
  • 为什么我们自动连接接口而不是实现的类?

java spring dependency-injection

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

我应该在哪里放置@Transactional注释:在接口定义或实现类?

代码中标题的问题:

@Transactional (readonly = true)
public interface FooService {
   void doSmth ();
}


public class FooServiceImpl implements FooService {
   ...
}
Run Code Online (Sandbox Code Playgroud)

VS

public interface FooService {
   void doSmth ();
}

@Transactional (readonly = true)
public class FooServiceImpl implements FooService {
   ...
}
Run Code Online (Sandbox Code Playgroud)

java spring annotations coding-style

82
推荐指数
3
解决办法
3万
查看次数

注入点有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)

我是 Spring Boot 的新手,在编写文件上传 API 时出现以下错误:

Error:Description:
Field fileStorageService in com.primesolutions.fileupload.controller.FileController required a bean of type 'com.primesolutions.fileupload.service.FileStorageService' that could not be found.
The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.primesolutions.fileupload.service.FileStorageService' in your configuration.*
Run Code Online (Sandbox Code Playgroud)

控制器类:

public class FileController 
{
    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileStorageService fileStorageService;

    @PostMapping("/uploadFile")
    public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
        String fileName = fileStorageService.storeFile(file);

        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/downloadFile/")
                .path(fileName)
                .toUriString();

        return new UploadFileResponse(fileName, fileDownloadUri, …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

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