Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 服务bean在Spring Rest控制器中未正确自动连接_Java_Spring_Spring Mvc_Spring Restcontroller - Fatal编程技术网

Java 服务bean在Spring Rest控制器中未正确自动连接

Java 服务bean在Spring Rest控制器中未正确自动连接,java,spring,spring-mvc,spring-restcontroller,Java,Spring,Spring Mvc,Spring Restcontroller,我有一个springrest应用程序,它有一个rest控制器,如下所示 @RestController public class IngestorController { @Autowired private IngestorService ingestorService; @RequestMapping( value = "/ingestor/createAndDeploy/{ingestorName}", method = RequestMethod.POST )

我有一个springrest应用程序,它有一个
rest控制器
,如下所示

@RestController
public class IngestorController
{
    @Autowired
    private IngestorService ingestorService;

    @RequestMapping( value = "/ingestor/createAndDeploy/{ingestorName}", method = RequestMethod.POST )
    public void createAndDeploy( @PathVariable String ingestorName )
    {
        ingestorService.createAndDeploy( ingestorName );
    }

}
@Service
public class IngestorService
{
    @Autowired
    private IngestorCommandBuilder ingestorCommandBuilder;

    private String uri;
    private DeployTemplate deployTemplate;

    public void init() throws URISyntaxException
    {
        deployTemplate = new DeployTemplate( new URI( uri ) );
    }

    @Transactional
    public void createAndDeploy( Ingestor ingestor )
    {
         //.....
     }

}
简单地说,我有一个
服务Bean
,如下所示

@RestController
public class IngestorController
{
    @Autowired
    private IngestorService ingestorService;

    @RequestMapping( value = "/ingestor/createAndDeploy/{ingestorName}", method = RequestMethod.POST )
    public void createAndDeploy( @PathVariable String ingestorName )
    {
        ingestorService.createAndDeploy( ingestorName );
    }

}
@Service
public class IngestorService
{
    @Autowired
    private IngestorCommandBuilder ingestorCommandBuilder;

    private String uri;
    private DeployTemplate deployTemplate;

    public void init() throws URISyntaxException
    {
        deployTemplate = new DeployTemplate( new URI( uri ) );
    }

    @Transactional
    public void createAndDeploy( Ingestor ingestor )
    {
         //.....
     }

}
我有如下所示的
Spring配置

<bean id="ingestorCommandBuilder" class="org.amaze.server.ingestor.IngestorCommandBuilder" />

<bean id="ingestorService" class="org.amaze.server.service.IngestorService" init-method="init">
    <property name="uri" value="http://localhost:15217" />
</bean>

<bean id="ingestorController" class="org.amaze.server.controller.IngestorController"/>

每当我尝试启动应用程序上下文时,应用程序上下文就会启动,并点击InjectorService中的init方法,deployTemplate对象也会为服务bean初始化

但是这个bean不是为InjectorController自动连接的。当我从postman访问rest端点时,服务bean的deployTemplate属性为null。。分配给控制器中InjectorService变量的对象是不同的对象,而不是为init方法调用的对象

我尝试将服务bean设置为单例(即使默认范围是单例),但dint不起作用


我无法找出我所犯的错误。。如果您使用基于注释的配置,您基本上不需要在应用程序上下文xml文件中描述所有bean。注释是自动连线服务所需的全部内容

要正确定义init方法,请使用
@PostConstruct
注释。属性可以很容易地移动到externat.Properties文件中,并使用
@Value
注释注入到代码中


或者,将
@Qualifier
@Autowired

一起使用。当您使用基于注释的DI时,您不需要在XML中定义bean

@PostConstruct可用于替换xml配置的init方法

只用

    <context:component-scan base-package="..."/> <mvc:annotation-driven/>

首先确保您有:


在Spring配置中。现在,您有几个备选方案:


要扫描组件,或



请包括您的完整Spring配置,以便更轻松地分析您的问题。

为什么您有
声明,并用
@Service
注释该类?您是否使用Hi感谢您的回复。。。是的,我正在使用上下文组件扫描。。。感谢您的建议,但由于某些原因,我无法使用批注配置。。我只需要通过xml配置来实现这一点……您的代码已经使用了spring注释,比如
@Service
@Autowired