Spring引导应用程序中的自动装配字段为空

时间:2015-12-05 11:31:09

标签: java spring spring-boot spring-data

我正在实施一个简单的春季启动应用程序:

Content上课:

Entity
@Table(name = "content")
public class Content implements Serializable {

public Content() {
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;

@NotNull
private String title;

//getter/setter and toString()
}

ContentRepository界面:

@Repository
public interface ContentRepository extends CrudRepository<Content, Long>{
}

RepositoryConfiguration

@Configuration
@EnableJpaRepositories({"com.tarameshgroup.derakht.repository"})
@EntityScan("com.tarameshgroup.derakht.domain")
@ComponentScan("com.tarameshgroup.derakht")
public class RepositoryConfiguration {
}

Application课程进行测试:

@SpringBootApplication
@EnableAutoConfiguration
@Import({RepositoryConfiguration.class})
public class Application {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(Application.class);

    @Autowired
    static ContentRepository contentRepository; // Why is null?

    public static void main(String[] args) {
        logger.info("Running application...");
        SpringApplication.run(Application.class);

        System.out.println("contentRepository: " + contentRepository); //why contentRepository is null?
    }

1 个答案:

答案 0 :(得分:1)

您将ContentRepository定义为静态字段。要么使它不是静态的,要么使用这个技巧@autowired in static classes