我的应用程序使用XML base spring配置,我希望将此配置移动到@Configuration类,因为Spring 3.0以后支持这些注释。
例如,这是我在XML中的bean,
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.mycom.AnnotatedEntity</value>
</list>
</property>
</bean>
我试过了,
@Configuration
public class Config{
@Value("${jdbc.driverClassName}")
private Driver driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DBDao dao(){
return new DaoImpl(sessionFactory());
}
@Bean
public SessionFactory sessionFactory(){
LocalSessionFactoryBean localSessionFactory = new LocalSessionFactoryBean();
Class<?>[] annotatedClasses = null;
annotatedClasses.getClass();
localSessionFactory.setDataSource(dataSource());
localSessionFactory.setAnnotatedClasses(annotatedClasses);
return (SessionFactory) localSessionFactory;
}
@Bean
public SimpleDriverDataSource dataSource(){
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriver(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
问题是如何设置&#34; annotatedClasses&#34;和&#34; hibernateProperties&#34;配置类中的属性?
答案 0 :(得分:0)
带注释的类采用var args。像这样:
localSessionFactory.setAnnotatedClasses(SomeEntity.class, AnotherEntity.class);
设置hibernate属性:
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
properties.put("hibernate.show_sql", false);
sessionFactory.setHibernateProperties(properties);
旁注:不要将LocalSessionFactoryBean
投射到SessionFactory
。简单地回复:
localSessionFactory.getObject();