1

I am implementing a simple spring boot app:

Content class:

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 interface:

@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 class for testing:

@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?
    }
CVV
  • 451
  • 2
  • 7
  • 21
  • Where is the class that implements interface ContentRepository. AS when you autowire object of implemented class is created. As I can't see any class implementing it. THat's why the error – Naruto Dec 05 '15 at 11:39
  • @Naruto There is no need for implementing class in `spring boot ` applications. – CVV Dec 05 '15 at 11:43

1 Answers1

1

You define ContentRepository as static field. either make it non static or use this trick @autowired in static classes

Community
  • 1
  • 1
Rahim Dastar
  • 1,259
  • 1
  • 9
  • 15