Wyswietla mi sie bład i nie potrafie znalezc bledu w programie, moglby ktos pomoc i wytlumaczyc dlaczego tak jest?
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'dao.IBookdao' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:347) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1103) at main.SpringJpaApplication.main(SpringJpaApplication.java:18)
moj kod:
``package config;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Component;
@Configuration
public class JpaConfig {
@Bean
public LocalContainerEntityManagerFactoryBean createEMF(JpaVendorAdapter adapter, DataSource ds) {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
Map<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.url", "jdbc:mysql://localhost:3306/mojabaza");
properties.put("javax.persistence.jdbc.user", "root");
properties.put("javax.persistence.jdbc.password", "password");
properties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");
properties.put("javax.persistence.schema-generation.database.action", "drop-and-create");
emf.setPersistenceUnitName("abc");
emf.setDataSource(ds);
emf.setJpaPropertyMap(properties);
emf.setJpaVendorAdapter(adapter);
emf.setPackagesToScan("model");
return emf;
}
@Bean
public JpaVendorAdapter createVendorAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.MYSQL);
adapter.setShowSql(true);
return adapter;
}
@Bean
public DataSource createDS() {
BasicDataSource ds = new BasicDataSource();
ds.setUrl("jdbc:mysql://localhost:3306/mojabaza");
ds.setUsername("root");
ds.setPassword("password");
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setInitialSize(5);
return ds;
}
}
package dao;
import model.Book;
public interface IBookdao {
public void save(Book book);
public Book get(Long id);
public void cleanUp();
}
package main;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import dao.Bookdao;
import dao.IBookdao;
import model.Book;
@Configuration()
@ComponentScan("config")
public class SpringJpaApplication {
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(SpringJpaApplication.class);
IBookdao abookDao = ctx.getBean(IBookdao.class);
//zapisujemy
Book book = new Book("1234567890468", "Spring is so cool", "Javastart");
abookDao.save(book);
//odczytujemy
Book bookGet = abookDao.get(1L);
System.out.println(bookGet);
Thread.sleep(5000);
ctx.close();
}
}
package dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import model.Book;
@Repository
public class Bookdao implements IBookdao{
private EntityManagerFactory emFactory;
private EntityManager entityManager;
public Bookdao() {
emFactory = Persistence.createEntityManagerFactory("abc");
entityManager = emFactory.createEntityManager();
}
public void save(Book book) {
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(book);
tx.commit();
}
public Book get(Long id) {
Book book = entityManager.find(Book.class, id);
return book;
}
public void cleanUp() {
entityManager.close();
emFactory.close();
}
}
package model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String isbn;
private String title;
private String author;
Book() {}
public Book(String isbn, String title, String author) {
this.isbn = isbn;
this.title = title;
this.author = author;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [id=" + id + ", isbn="
+ isbn + ", title=" + title + ", author=" + author + "]";
}
}
``