我正在尝试使用javaConfig将数据库连接到我的vaadin-app。但我的布局没有注入JpaRepository。
HotelCategoryServiceImpl.java
package com.gptravel.service.category;
import com.gptravel.dao.HotelCategoryRepository;
import com.gptravel.entity.HotelCategory;
import com.gptravel.service.hotel.HotelServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.logging.Logger;
@Service("hotelCategoryService")
@Transactional
public class HotelCategoryServiceImpl implements HotelCategoryService {
private static final Logger LOGGER =
Logger.getLogger(HotelServiceImpl.class.getName());
@PersistenceContext
private EntityManager em;
@Autowired
private HotelCategoryRepository hotelCategoryRepo;
@Override
public HotelCategory create(HotelCategory hotelCategory) {
HotelCategory savehotelCategory =
hotelCategoryRepo.saveAndFlush(hotelCategory);
return savehotelCategory;
}
@Override
public void delete(long id) {
hotelCategoryRepo.delete(id);
}
@Override
public HotelCategory getById(long id) {
return hotelCategoryRepo.findOne(id);
}
@Override
public HotelCategory update(HotelCategory hotelCategory) {
return hotelCategoryRepo.saveAndFlush(hotelCategory);
}
//get all categories
@Override
@Transactional(readOnly = true)
public List<HotelCategory> findAll() {
return hotelCategoryRepo.findAll();
}
}
HotelCategoryService.java
package com.gptravel.service.category;
import com.gptravel.entity.HotelCategory;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("hotelCategoryService")
public interface HotelCategoryService {
HotelCategory create(HotelCategory hotelCategory);
void delete(long id);
HotelCategory getById(long id);
HotelCategory update(HotelCategory hotelCategory);
List<HotelCategory> findAll();
}
存储库:
import com.gptravel.entity.HotelCategory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface HotelCategoryRepository extends
JpaRepository<HotelCategory, Long> {
List<HotelCategory> findAllBy(Pageable pageable);
}
Jpa Config.java
@Configuration
@EnableTransactionManagement
@ComponentScan("com.gptravel")
@PropertySource(value = { "classpath:application.properties" })
@EnableJpaRepositories(basePackages="com.gptravel.dao")
public class HibernateConfig {
@Resource
private Environment environment;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
public EntityManagerFactory entityManagerFactory() throws NamingException {
HibernateJpaVendorAdapter vendorAdapter = new
HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factoryBean = new
LocalContainerEntityManagerFactoryBean();
factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.setPackagesToScan("com.gptravel.entity");
factoryBean.setDataSource(dataSource());
factoryBean.setPersistenceProviderClass
(HibernatePersistenceProvider.class);
factoryBean.setJpaProperties(jpaProperties());
factoryBean.setPersistenceUnitName("demo_hotels");
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
}
private Properties jpaProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect",
environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql",
environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.hbm2ddl.auto",
environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
}
@Bean
public PlatformTransactionManager transactionManager() throws
NamingException {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
@Bean
public SpringLiquibase liquibase() {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:db/db.changelog.xml");
liquibase.setDataSource(dataSource());
return liquibase;
}
}
我的应用程序有简单的测试连接数据库,成功结束 TestJpaConfig.java
@Configuration
@EnableTransactionManagement
@ComponentScan("com.gptravel")
public class TestDataBaseConfig {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";
private static final String PROPERTY_NAME_DATABASE_URL = "jdbc:mysql://localhost:3306/demo_hotels";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "demo";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "demo";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "org.hibernate.dialect.MySQLDialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "true";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "com.gptravel.entity";
private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "update";
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
entityManagerFactoryBean.setPackagesToScan(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN);
entityManagerFactoryBean.setJpaProperties(hibernateProp());
return entityManagerFactoryBean;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(PROPERTY_NAME_DATABASE_DRIVER);
dataSource.setUrl(PROPERTY_NAME_DATABASE_URL);
dataSource.setUsername(PROPERTY_NAME_DATABASE_USERNAME);
dataSource.setPassword(PROPERTY_NAME_DATABASE_PASSWORD);
return dataSource;
}
private Properties hibernateProp() {
Properties properties = new Properties();
properties.put("hibernate.dialect", PROPERTY_NAME_HIBERNATE_DIALECT);
properties.put("hibernate.show_sql", PROPERTY_NAME_HIBERNATE_SHOW_SQL);
properties.put("hibernate.hbm2ddl.auto", PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO);
return properties;
}
}
TestClass.java
@DirtiesContext
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestDataBaseConfig.class)
@WebAppConfiguration
public class HotelServiceImplTest {
@Resource
private EntityManagerFactory emf;
protected EntityManager em;
@Resource
private HotelRepository hotelCategoryService;
@Before
public void setUp() throws Exception {
em = emf.createEntityManager();
}
@Test
public void testSaveHotelCategory() throws Exception {
hotelCategoryService.findAll();
List<Hotel> testList = hotelCategoryService.findAll();
System.out.println("Size - " + testList.size());
}
}
可能是什么问题?
更新#1
我添加了@Service
,@Component
这样的注释,但应用仍然无效。
链接到我的回购:VaadinJPAHotelApp
答案 0 :(得分:0)
您可能需要在HotelCategoryServiceImpl类中更改此内容。
@Service("HotelCategoryServiceImpl")
@Transactional
public class HotelCategoryServiceImpl implements HotelCategoryService {
private static final Logger LOGGER =
浏览以下代码以供参考。 HotelCategoryRestCall
答案 1 :(得分:0)
并非findAll
返回null
,但HotelCategoryService
为null
。
您有一个HotelForm
的实例,但是您没有获得任何与Spring相关的例外情况,尽管@Autowired
字段没有自动装配。这意味着这个实例不是由Spring创建的,而是由其他东西创建的(Vaadin可能?)。
应该由Spring注入依赖项的实例必须由Spring创建。因此,请检查您如何创建实例。简单地将Spring注释打到它上面就不够了,因为很明显,现在你并不是要求Spring为你创建一个实例,而是另外一些,它可能不会对Spring注释产生任何影响。
也许这个tutorial which shows how to integrate Vaadin with Spring可以提供一些帮助
并且请:以后不要使用图像来传递代码