Conexão testada OK.
Porém, estou com problema na injeção de dependência com autowired
@Autowired(required=true) private ConvidadoRepository repository;
Já tentai anotação @Repository na interface, o @Component na classe ConvidadoController e nada funcionou.
O que poderia ser?
Mensagem de erro:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'convidadoController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.com.alura.repository.ConvidadoRepository br.com.alura.conf.ConvidadoController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [br.com.alura.repository.ConvidadoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.8.RELEASE.jar:4.2.8.RELEASE]
Os códigos:
public class Testando {
public static void main(String[] args) throws SQLException {
Connection conn = createConnection();
System.out.println("Banco: " + conn.getSchema() + "/" + conn.getCatalog());
SpringApplication.run(Configuracao.class, args);
}
}
------------------
@Controller
public class ConvidadoController {
@Autowired(required=true)
private ConvidadoRepository repository;
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("listaconvidados")
public String listaConvidados(Model model){
try {
Iterable<Convidado> convidados = repository.findAll();
model.addAttribute("convidados", convidados);
return "listaconvidados";
} catch (Exception e) {
System.out.println("Erro ao carregar a lista: " + e.getMessage());
e.printStackTrace();
return null;
}
}
}
------------------
public interface ConvidadoRepository extends CrudRepository<Convidado, Long>{
}
------------------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.alura.listavip</groupId>
<artifactId>listavip</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>listavip</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
</dependencies>
</project>