小编Ben*_*980的帖子

@Inject字段始终为null

我尝试@Inject一个字段(它是一个jar模块,空的beans.xml存在于META-INF下),如下所示:

IDataProvider接口

public interface IDataProvider {
  void test();
}
Run Code Online (Sandbox Code Playgroud)

DataProvider实现导入javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class DataProvider implements IDataProvider {
  private int i;

  public DataProvider() {
    i = 42;
  }

  @Override
  public void test() {

  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试注入DataProvider的类

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class DataController {

  @Inject
  private IDataProvider dataProvider;

  private int k;

  public DataController() {
     k = 42;
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我在Wildfly上运行它,注入的dataProvider总是为null(DataController构造函数中的Breakpoint).

在每个教程上都是这样做的,所以我认为这应该有效.唯一的区别是两个类都应该是@ApplicationScoped

我正在使用Wildfly 8.2Final,Gradle,IntelliJ.我的gradle.build看起来像这样:

apply plugin: 'java'

repositories {
  mavenCentral()
  jcenter()
}

dependencies {
  compile group:'javax', name:'javaee-web-api', version:'7.+'
  compile group:'org.jboss.ejb3', name:'jboss-ejb3-ext-api', …
Run Code Online (Sandbox Code Playgroud)

java cdi

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

Const functor returning reference of T is prohibiting assignment

Why is it not possible to assign a value to a reference returned by a const functor?

#include <vector>

template<typename T>
class Test {
public:
  T & operator()(size_t index) const {
    return A[index];
  }

private:
  std::vector<T> A{1,2,3};
};

int main() {
  const Test<double> test1;
  const Test<double> test2;
  Test<double> test;

  test(0) = test1(0) + test2(1);
}
Run Code Online (Sandbox Code Playgroud)

I get the compiling errors below but I don't understand why the const of the functor is prohibiting the assignment because the functor itself isn't …

c++

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

标签 统计

c++ ×1

cdi ×1

java ×1