从Spring MVC XML文件转移到javaconfig。我真的迷失在我的数据库XML文件中 [英] Moving from Spring MVC XML files to javaconfig. I am really at a lost with my database XML file
问题描述
这是我的database.xml:
?xml version =1.0encoding =UTF-8?>
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :context =http://www.springframework.org/schema/context
xmlns:tx =http://www.springframework.org/schema/txxmlns:jdbc =http:// www.springframework.org/schema/jdbc
xmlns:jee =http://www.springframework.org/schema/jee
xsi:schemaLocation =http:// www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http:// www .springframework.org / schema / tx / spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework。组织/架构/ JEE /弹簧JEE-3.0.xsd>
< context:property-placeholder location =classpath:app.properties/>
< context:component-scan base-package =org.uftwf/>
< tx:annotation-driven transaction-manager =hibernateTransactionManager/>
< jee:jndi-lookup id =dataSourcejndi-name =java:jboss / datasources / mySQLDB
expected-type =javax.sql.DataSource/> ;
< bean id =sessionFactory
class =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =annotatedClasses>
< list>
< value> org.uftwf.inquiry.model.MemberInquiryInformation< / value>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {hibernate.dialect}< / prop>
< prop key =hibernate.show_sql> $ {hibernate.show_sql}< / prop>
< prop key =hibernate.use_sql_comments> $ {hibernate.use_sql_comments}< / prop>
< prop key =format_sql> $ {format_sql}< / prop>
< /道具>
< / property>
< / bean>
< bean id =hibernateTransactionManager
class =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
< / beans>
这是我的javaconfig类:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {org.uftwf.inquiry})
@ImportResource(/ WEB-INF / spring /root-config.xml)
public class WebMVCConfig extends WebMvcConfigurerAdapter {
private static final String MESSAGE_SOURCE =/ WEB-INF / classes / messages;
private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);
@Value($ {jdbc.driverClassName})
private String driverClassName;
@Value($ {jdbc.url})
私有字符串url;
@Value($ {jdbc.username})
私人字符串用户名;
@Value($ {jdbc.password})
私人字符串密码;
@Value($ {hibernate.dialect})
private String hibernateDialect;
@Value($ {hibernate.show_sql})
private String hibernateShowSql;
@Value($ {hibernate.hbm2ddl.auto})
private String hibernateHbm2ddlAuto;
@Bean
public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
{
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource(application.properties));
ppc.setIgnoreUnresolvablePlaceholders(true);
返回ppc;
@Bean()
public DataSource getDataSource()
{
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
public LocalSessionFactoryBean sessionFactory()
{
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setDataSource(getDataSource());
factoryBean.setHibernateProperties(getHibernateProperties());
factoryBean.setPackagesToScan(org.uftwf.inquiry.model);
返回factoryBean;
$Be
公共属性getHibernateProperties()
{
属性hibernateProperties = new Properties();
hibernateProperties.setProperty(hibernate.dialect,hibernateDialect);
//hibernateProperties.setProperty(\"hibernate.show_sql,true);
//hibernateProperties.setProperty(\"hibernate.format_sql,true);
hibernateProperties.setProperty(hibernate.hbm2ddl.auto,update);
hibernateProperties.setProperty(javax.persistence.validation.mode,none);
//审计历史标志
hibernateProperties.setProperty(org.hibernate.envers.store_data_at_delete,true);
hibernateProperties.setProperty(org.hibernate.envers.global_with_modified_flag,true);
返回hibernateProperties;
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory)
{
HibernateTransactionManager htm = new HibernateTransactionManager();
htm.setSessionFactory(sessionFactory);
return htm;
@Bean
public ViewResolver解析器(){
UrlBasedViewResolver url = new UrlBasedViewResolver();
url.setPrefix(/ WEB-INF / view /);
url.setViewClass(JstlView.class);
url.setSuffix(。jsp);
返回网址;
$ b @Bean(name =messageSource)
public MessageSource configureMessageSource(){
logger.debug(设置消息源 );
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename(MESSAGE_SOURCE);
messageSource.setCacheSeconds(5);
messageSource.setDefaultEncoding(UTF-8);
返回messageSource;
}
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver lr = new SessionLocaleResolver();
lr.setDefaultLocale(Locale.ENGLISH);
返回lr;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
logger.debug(设置资源处理程序);
registry.addResourceHandler(/ resources /)。addResourceLocations(/ resources / **);
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
logger.debug(configureDefaultServletHandling);
configurer.enable();
$ b @Override
public void addInterceptors(final InterceptorRegistry registry){
registry.addInterceptor(new LocaleChangeInterceptor());
$ b $Bean
public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){
SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
属性映射= new Properties();
mappings.put(org.springframework.web.servlet.PageNotFound,p404);
mappings.put(org.springframework.dao.DataAccessException,dataAccessFailure);
mappings.put(org.springframework.transaction.TransactionException,dataAccessFailure);
b.setExceptionMappings(mappings);
return b;
$Be $ b $ public RequestTrackerConfig requestTrackerConfig()
{
RequestTrackerConfig tr = new RequestTrackerConfig();
tr.setPassword(Waiting#$);
tr.setUrl(https://uftwfrt01-dev.uftmasterad.org/REST/1.0);
tr.setUser(root);
return tr;
}
}
我认为我缺少的部分是以下内容,但请仔细检查我的课程
< context:property-placeholder location =classpath:app。属性/>
< context:component-scan base-package =org.uftwf/>
< tx:annotation-driven transaction-manager =hibernateTransactionManager/>
< jee:jndi-lookup id =dataSourcejndi-name =java:jboss / datasources / mySQLDB
expected-type =javax.sql.DataSource/> ;
对于
< tx:annotation-driven transaction-manager =hibernateTransactionManager/>
注释您的Configuration类, WebMVCConfig
,
@EnableTransactionManagement
$ b
< context:component-scan base-package =org.uftwf/> $ b For
将包字符串添加到 @ComponentScan
字段 basePackages
For
< context:property-placeholder location =classpath:app.properties/>
使用
<$ p $注释您的配置类p> @PropertySource(value =classpath:app.properties)
并使您的 PropertyPlaceholderConfigurer
@Bean
方法 static
对于
< jee:jndi-lookup id =dataSource jndi-name =java:jboss / datasources / mySQLDB
expected-type =javax.sql.DataSource/>
@Bean
public DataSource dataSource()抛出Exception {
Context ctx = new InitialContext();
return(DataSource)ctx.lookup(java:jboss / datasources / mySQLDB);
}
与自动装配会话工厂不同,只需调用 @Bean
方法
@Bean
public HibernateTransactionManager transactionManager()
{
HibernateTransactionManager htm = new HibernateTransactionManager();
htm.setSessionFactory(sessionFactory());
return htm;
}
I Moving from Spring MVC XML files to javaconfig. I am really at a lost with my database XML file. I don't know how to get Hibernate4 working and my JBoss JNDI Datasource working. Can someone please tell me how to make the javaconfig class work like this XML..
Here is my database.xml:
?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:property-placeholder location="classpath:app.properties" />
<context:component-scan base-package="org.uftwf" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/mySQLDB"
expected-type="javax.sql.DataSource" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>org.uftwf.inquiry.model.MemberInquiryInformation</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="format_sql">${format_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Here is my javaconfig class:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages= {"org.uftwf.inquiry"})
@ImportResource("/WEB-INF/spring/root-config.xml")
public class WebMVCConfig extends WebMvcConfigurerAdapter {
private static final String MESSAGE_SOURCE = "/WEB-INF/classes/messages";
private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${hibernate.dialect}")
private String hibernateDialect;
@Value("${hibernate.show_sql}")
private String hibernateShowSql;
@Value("${hibernate.hbm2ddl.auto}")
private String hibernateHbm2ddlAuto;
@Bean
public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
{
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("application.properties"));
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}
@Bean()
public DataSource getDataSource()
{
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
@Bean
public LocalSessionFactoryBean sessionFactory()
{
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setDataSource(getDataSource());
factoryBean.setHibernateProperties(getHibernateProperties());
factoryBean.setPackagesToScan("org.uftwf.inquiry.model");
return factoryBean;
}
@Bean
public Properties getHibernateProperties()
{
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", hibernateDialect);
//hibernateProperties.setProperty("hibernate.show_sql", "true");
//hibernateProperties.setProperty("hibernate.format_sql", "true");
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update");
hibernateProperties.setProperty("javax.persistence.validation.mode", "none");
//Audit History flags
hibernateProperties.setProperty("org.hibernate.envers.store_data_at_delete", "true");
hibernateProperties.setProperty("org.hibernate.envers.global_with_modified_flag", "true");
return hibernateProperties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory)
{
HibernateTransactionManager htm = new HibernateTransactionManager();
htm.setSessionFactory(sessionFactory);
return htm;
}
@Bean
public ViewResolver resolver() {
UrlBasedViewResolver url = new UrlBasedViewResolver();
url.setPrefix("/WEB-INF/view/");
url.setViewClass(JstlView.class);
url.setSuffix(".jsp");
return url;
}
@Bean(name = "messageSource")
public MessageSource configureMessageSource() {
logger.debug("setting up message source");
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename(MESSAGE_SOURCE);
messageSource.setCacheSeconds(5);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver lr = new SessionLocaleResolver();
lr.setDefaultLocale(Locale.ENGLISH);
return lr;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
logger.debug("setting up resource handlers");
registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
logger.debug("configureDefaultServletHandling");
configurer.enable();
}
@Override
public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(new LocaleChangeInterceptor());
}
@Bean
public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
Properties mappings = new Properties();
mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
mappings.put("org.springframework.dao.DataAccessException", "dataAccessFailure");
mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
b.setExceptionMappings(mappings);
return b;
}
@Bean
public RequestTrackerConfig requestTrackerConfig()
{
RequestTrackerConfig tr = new RequestTrackerConfig();
tr.setPassword("Waiting#$");
tr.setUrl("https://uftwfrt01-dev.uftmasterad.org/REST/1.0");
tr.setUser("root");
return tr;
}
}
I think the parts I am missing are the following but please overcheck my class
<context:property-placeholder location="classpath:app.properties" />
<context:component-scan base-package="org.uftwf" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/mySQLDB"
expected-type="javax.sql.DataSource" />
For
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
annotate your Configuration class, WebMVCConfig
, with
@EnableTransactionManagement
For
<context:component-scan base-package="org.uftwf" />
Add the package String to your @ComponentScan
field basePackages
For
<context:property-placeholder location="classpath:app.properties" />
annotate your Configuration class with
@PropertySource(value = "classpath:app.properties")
and make your PropertyPlaceholderConfigurer
@Bean
method static
.
For
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/mySQLDB"
expected-type="javax.sql.DataSource" />
@Bean
public DataSource dataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:jboss/datasources/mySQLDB");
}
Instead of autowiring your session factory, just call your @Bean
method
@Bean
public HibernateTransactionManager transactionManager()
{
HibernateTransactionManager htm = new HibernateTransactionManager();
htm.setSessionFactory(sessionFactory());
return htm;
}
这篇关于从Spring MVC XML文件转移到javaconfig。我真的迷失在我的数据库XML文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!