Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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 @带@Autowired的Bean ResourceProcessor_Java_Spring Boot_Spring Rest - Fatal编程技术网

Java @带@Autowired的Bean ResourceProcessor

Java @带@Autowired的Bean ResourceProcessor,java,spring-boot,spring-rest,Java,Spring Boot,Spring Rest,在使用Spring Data REST和HATEOAS的Spring Boot 1.5.10应用程序中,我有一个ResourceProcessorbean,带有@Autowired服务,如: @Bean public ResourceProcessor<Resource<Order>> orderResourceProcessor() { return new ResourceProcessor<Resource<Order>>() {

在使用Spring Data REST和HATEOAS的Spring Boot 1.5.10应用程序中,我有一个
ResourceProcessor
bean,带有
@Autowired
服务,如:

@Bean
public ResourceProcessor<Resource<Order>> orderResourceProcessor() {
    return new ResourceProcessor<Resource<Order>>() {
        @Autowired
        private OrderHandler orderHandler;

        @Override
        public Resource<Order> process(Resource<Order> resource) {
            Order order = resource.getContent();
            Payment payment = orderHandler.payment(order);

            resource.add(makeLink(payment));

            return resource;
        }

        private Link makelink(Payment payment) {
            return new Link(/*...*/);
        }
    };
}
@Bean
公共资源处理器orderResourceProcessor(){
返回新的ResourceProcessor(){
@自动连线
私有OrderHandler;
@凌驾
公共资源流程(资源){
订单=resource.getContent();
付款付款=orderHandler.Payment(订单);
添加(makeLink(付款));
返回资源;
}
专用链接makelink(付款){
返回新链接(/*…*/);
}
};
}
不幸的是,当添加
@Autowired
服务时,资源处理器bean不再被触发;i、 例如,
OrderHandler
被注释掉时,资源处理器按其应该的方式运行


ResourceProcessor
能否使用
@Autowired
服务;如果是这样的话,构造它的正确方法是什么?

我想您可以将orderHandler自动连接到外部类。以您的方式,当您自己创建ResourceProcessor实例时,它将不起作用

@Autowired
private OrderHandler orderHandler;

@Bean
public ResourceProcessor<Resource<Order>> orderResourceProcessor() {
   return new ResourceProcessor<Resource<Order>>() {


    @Override
    public Resource<Order> process(Resource<Order> resource) {
        Order order = resource.getContent();
        Payment payment = orderHandler.payment(order);

        resource.add(makeLink(payment));

        return resource;
    }

    private Link makelink(Payment payment) {
        return new Link(/*...*/);
    }
};
}
@Autowired
私有OrderHandler;
@豆子
公共资源处理器orderResourceProcessor(){
返回新的ResourceProcessor(){
@凌驾
公共资源流程(资源){
订单=resource.getContent();
付款付款=orderHandler.Payment(订单);
添加(makeLink(付款));
返回资源;
}
专用链接makelink(付款){
返回新链接(/*…*/);
}
};
}

我想您可以将orderHandler自动连接到外部类。以您的方式,当您自己创建ResourceProcessor实例时,它将不起作用

@Autowired
private OrderHandler orderHandler;

@Bean
public ResourceProcessor<Resource<Order>> orderResourceProcessor() {
   return new ResourceProcessor<Resource<Order>>() {


    @Override
    public Resource<Order> process(Resource<Order> resource) {
        Order order = resource.getContent();
        Payment payment = orderHandler.payment(order);

        resource.add(makeLink(payment));

        return resource;
    }

    private Link makelink(Payment payment) {
        return new Link(/*...*/);
    }
};
}
@Autowired
私有OrderHandler;
@豆子
公共资源处理器orderResourceProcessor(){
返回新的ResourceProcessor(){
@凌驾
公共资源流程(资源){
订单=resource.getContent();
付款付款=orderHandler.Payment(订单);
添加(makeLink(付款));
返回资源;
}
专用链接makelink(付款){
返回新链接(/*…*/);
}
};
}
您应该对本部分内容感兴趣:

@Configuration类中的@Bean方法

通常,@Bean方法在@Configuration类中声明。 在这种情况下,bean方法可以引用 通过直接调用同一类。这确保了引用 beans是强类型和可导航的。这样的所谓 “bean间引用”保证尊重范围和AOP 语义,就像getBean()查找一样

例如:

 @Bean
 public FooService fooService() {
     return new FooService(fooRepository());
 }

 @Bean
 public FooRepository fooRepository() {
     return new JdbcFooRepository(dataSource());
 }
这意味着您不必使用
@Autowired
@Bean
声明中设置依赖项,而是引用另一个用
@Bean
注释的方法
但是您真的需要设置依赖项来创建bean吗? 完全没有。OrderHandler仅在
process()
调用期间使用

因此,您可以简单地将
OrderHandler
注入到用
@Bean
注释的方法的同一级别,并在匿名类中使用它:

@Autowired
private OrderHandler orderHandler;  // only change

@Bean
public ResourceProcessor<Resource<Order>> orderResourceProcessor() {
    return new ResourceProcessor<Resource<Order>>() {


        @Override
        public Resource<Order> process(Resource<Order> resource) {
            Order order = resource.getContent();
            Payment payment = orderHandler.payment(order);

            resource.add(makeLink(payment));

            return resource;
        }

        private Link makelink(Payment payment) {
            return new Link(/*...*/);
        }
    };
}
@Autowired
私有OrderHandler OrderHandler;//只有改变
@豆子
公共资源处理器orderResourceProcessor(){
返回新的ResourceProcessor(){
@凌驾
公共资源流程(资源){
订单=resource.getContent();
付款付款=orderHandler.Payment(订单);
添加(makeLink(付款));
返回资源;
}
专用链接makelink(付款){
返回新链接(/*…*/);
}
};
}
您应该对本部分内容感兴趣:

@Configuration类中的@Bean方法

通常,@Bean方法在@Configuration类中声明。 在这种情况下,bean方法可以引用 通过直接调用同一类。这确保了引用 beans是强类型和可导航的。这样的所谓 “bean间引用”保证尊重范围和AOP 语义,就像getBean()查找一样

例如:

 @Bean
 public FooService fooService() {
     return new FooService(fooRepository());
 }

 @Bean
 public FooRepository fooRepository() {
     return new JdbcFooRepository(dataSource());
 }
这意味着您不必使用
@Autowired
@Bean
声明中设置依赖项,而是引用另一个用
@Bean
注释的方法
但是您真的需要设置依赖项来创建bean吗? 完全没有。OrderHandler仅在
process()
调用期间使用

因此,您可以简单地将
OrderHandler
注入到用
@Bean
注释的方法的同一级别,并在匿名类中使用它:

@Autowired
private OrderHandler orderHandler;  // only change

@Bean
public ResourceProcessor<Resource<Order>> orderResourceProcessor() {
    return new ResourceProcessor<Resource<Order>>() {


        @Override
        public Resource<Order> process(Resource<Order> resource) {
            Order order = resource.getContent();
            Payment payment = orderHandler.payment(order);

            resource.add(makeLink(payment));

            return resource;
        }

        private Link makelink(Payment payment) {
            return new Link(/*...*/);
        }
    };
}
@Autowired
私有OrderHandler OrderHandler;//只有改变
@豆子
公共资源处理器orderResourceProcessor(){
返回新的ResourceProcessor(){
@凌驾
公共资源流程(资源){
订单=resource.getContent();
付款付款=orderHandler.Payment(订单);
添加(makeLink(付款));
返回资源;
}
专用链接makelink(付款){
返回新链接(/*…*/);
}
};
}

尝试您的建议揭示了一个潜在的问题,即(未显示)循环依赖性…在春天悄无声息地被吞噬-/在任何情况下,您使用的@Autowire都不起作用。你改变了我的回答。请公平。@davidxxx在看到您的答案后,我意识到实现是一个匿名类,这是事实。我修正了我的密码。你到底想让我做什么?把它还原成旧版本?@cool-Change-nothing。你的回答很公平:)你哈