我有春季mvc + jpa项目。当我运行这个项目并转到/service
链接时,我得到了expiton。我尝试使用@Autowired
,但我得到另一个例外,因为使用applicationContext
HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ua.korbo.persistence.repository.PupilRepository] is defined
控制器
@Controller
public class WebController {
private PupilService pupilService;
private PupilRepository pupilRepository;
private ApplicationContext context;
private void setUpField(){
System.out.println("1");
if (context == null){
System.out.println("2");
context = new AnnotationConfigApplicationContext(BeanClass.class);
System.out.println("3");
}
System.out.println("4");
if (pupilService == null){
System.out.println("5");
pupilService = context.getBean(PupilServiceImpl.class);
System.out.println("6");
}
System.out.println("7");
if (pupilRepository == null){
System.out.println("8");
System.out.printf(pupilRepository + " start");
pupilRepository = context.getBean(PupilRepository.class);
System.out.printf(pupilRepository+" end");
System.out.println("9");
}
}
@RequestMapping(value = "/service", method = RequestMethod.GET)
public String printService(Map<String, String> model){
setUpField();
pupilService.findAll();
pupilService.setPupilRepositories(pupilRepository);
pupilService.save(new Pupil("Name", "Last", "Email", "MSG", "Number"));
model.put("key", "This is service!");
return "print";
}
}
来自sout setUpField
1
2
08-Sep-2015 21:30:42.871 INFO [http-nio-8080-exec-7] org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation HHH000204: Processing PersistenceUnitInfo [
name: default
...]
08-Sep-2015 21:30:42.902 INFO [http-nio-8080-exec-7] org.hibernate.dialect.Dialect.<init> HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect
08-Sep-2015 21:30:43.048 INFO [http-nio-8080-exec-7] <unknown>.<unknown> HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
08-Sep-2015 21:30:43.048 INFO [http-nio-8080-exec-7] <unknown>.<unknown> HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@229bbfd5
08-Sep-2015 21:30:43.055 INFO [http-nio-8080-exec-7] <unknown>.<unknown> HHH000228: Running hbm2ddl schema update
08-Sep-2015 21:30:43.066 WARN [http-nio-8080-exec-7] org.hibernate.jpa.internal.EntityManagerFactoryRegistry.addEntityManagerFactory HHH000436: Entity manager factory name (default) is already registered. If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
3
4
5
6
7
8
null start
网络初始化程序
public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { SpringRootConfig.class, PersistenceContext.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { SpringWebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
持久性背景
@Configuration
@EnableJpaRepositories(basePackages = {"persistence.repository"},
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager")
@EnableTransactionManagement
@PropertySource("classpath:app.properties")
public class PersistenceContext {
public PersistenceContext() {
}
/**
* The method that configures the datasource bean
* */
@Resource
private Environment env;
@Bean
DataSource dataSource() {
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
return new HikariDataSource(dataSourceConfig);
}
/**
* The method that configures the entity manager factory
* */
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("entity");
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
/**
* The method that configures the transaction manager
* */
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
存储库
@Repository
public interface PupilRepository extends CrudRepository<Pupil, Long> {
}