Question quite similar to this is answered here and here
And they suggest to use scanBasePackages
in the annotation @SpringBootApplication
and it works to find all beans required for auto wiring.
Problem Description
However, in one of the service CategoryServiceImpl
there is an autowired
dao categoryDao
which has @PersistenceContext
entity manager.
And my spring boot application is unable to find EntityManagerFactory
which is the point where my application is breaking.
It is a very straight forward application( I am trying to learn). I have two simple projects.
First one is the rest controller and second one is the JPA project to persist entities.
FIRST PROJECT: REST CONTROLLER PROJECT SETUP
Application.java (entry point)
@SpringBootApplication(scanBasePackages = {
"com.example.resourcecontroller",
"com.example.service",
"com.example.dao",
"javax" })
public class Application {
}
pom.xml
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
SECOND PROJECT: JPA SPRING HIBERNATE
pom.xml
<groupId>com.example</groupId>
<artifactId>spring-hibernate-jpa-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-hibernate-jpa-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
Service class
@Configuration
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.example.dao"),
@ComponentScan("com.example.service") })
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryDao categoryDao;
}
DAO
@Repository
public class CategoryDaoImpl implements CategoryDao {
@PersistenceContext
private EntityManager em;
}
**Application Config **
@Configuration
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.example.dao"),
@ComponentScan("com.example.service") })
public class AppConfig {
@Bean
public LocalEntityManagerFactoryBean geEntityManagerFactoryBean() {
LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
factoryBean.setPersistenceUnitName("LOCAL_PERSISTENCE");
return factoryBean;
}
}