如何使用纯Java配置

时间:2015-08-26 09:36:14

标签: java hibernate spring-mvc

我很难找到解释和概述如何在Spring 4中使用纯java配置设置hbm2ddl.auto的材料。 我没有使用Spring Boot,因为我想更多地了解Spring基础知识。但是,当然,我想避免应用程序中的所有XML文件。

到目前为止,我能够配置和使用我的应用程序,但我必须手动创建表。我设置hbm2ddl.auto的尝试不会去任何地方。

这就是我所拥有的:

@Configuration

@EnableTransactionManagement 公共类DataSourceConfiguration {

private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";

@Autowired
private Environment env;

@Bean
public DataSource dataSource() throws SQLException {
    System.out.println("--------------");
    System.out.println("Data Source Initialization");
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/myeventmanager");
    dataSource.setUsername("root");
    dataSource.setPassword("******");
    System.out.println("--------------");
    System.out.println("Initialized");
    System.out.println(dataSource.getConnection());
    return dataSource;
}

@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setPackagesToScan(new String[]{"com.ruruapps.domain"});
    Properties props = new Properties();
    props.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
    sessionFactoryBean.setHibernateProperties(props);
    return sessionFactoryBean;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager();
    transactionManager.setSessionFactory(sessionFactory);

    return transactionManager;
}

@Bean
public BeanPostProcessor persistenceTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}

Properties hibernateProperties() {
    return new Properties() {
        {
            setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
            setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
            setProperty("hibernate.globally_quoted_identifiers", "true");
        }
    };
}

}

那么配置这个的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

您可以按如下方式实施。

@Configuration
@EnableTransactionManagement
@ComponentScan({ "xxx.xxx.xxx" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.ws.ppp.pojo" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

@Bean(name = "dataSource")
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;
}

private Properties hibernateProperties() {
    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.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.automaticschema"));
    properties.put("hibernate.search.default.directory_provider",
            environment.getRequiredProperty("hibernate.search.default.directory_provider"));
    properties.put("hibernate.search.default.indexBase",
            environment.getRequiredProperty("hibernate.search.default.indexBase"));

    return properties;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(s);
    return txManager;
}

}

答案 1 :(得分:0)

就像。

            private Properties hibernateProperties() {
                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.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
                properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.automaticschema"));
                properties.put("hibernate.search.default.directory_provider",
                        environment.getRequiredProperty("hibernate.search.default.directory_provider"));
                properties.put("hibernate.search.default.indexBase",
                        environment.getRequiredProperty("hibernate.search.default.indexBase"));

                return properties;
            }

试试这个。