程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从Spring MVC XML文件移动到javaconfig。我的数据库XML文件真的让我迷茫了大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决从Spring MVC XML文件移动到javaconfig。我的数据库XML文件真的让我迷茫了?

开发过程中遇到从Spring MVC XML文件移动到javaconfig。我的数据库XML文件真的让我迷茫了的问题如何解决?下面主要结合日常开发的经验,给出你关于从Spring MVC XML文件移动到javaconfig。我的数据库XML文件真的让我迷茫了的解决方法建议,希望对你解决从Spring MVC XML文件移动到javaconfig。我的数据库XML文件真的让我迷茫了有所启发或帮助;

对于

<tx:Annotation-driven transaction-manager="hibernatetransactionManager" />

注释你的配置类,WebMVCConfig

@EnabletransactionManagement

对于

<context:component-scan base-package="org.uftwf" />

将Package String添加到您的@ComponentScan字段basePackages

对于

<context:property-placeholder LOCATIOn="classpath:app.propertIEs" />

注释您的Configuration类

@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() throws 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;
}

解决方法

我从Spring MVC XML文件移动到javaconfig。我的数据库XML文件真的让我迷茫了。我不知道如何使Hibernate4工作以及我的JBoss
JNDI数据源工作。有人可以告诉我如何使javaconfig类像这种XML一样工作。

这是我的database.xml:

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_276_11845@a/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLscheR_276_11845@a-instance" xmlns:context="http://www.springframework.org/scheR_276_11845@a/context"
    xmlns:tx="http://www.springframework.org/scheR_276_11845@a/tx" xmlns:jdbc="http://www.springframework.org/scheR_276_11845@a/jdbc"
    xmlns:jee="http://www.springframework.org/scheR_276_11845@a/jee"

    xsi:scheR_276_11845@aLOCATIOn="http://www.springframework.org/scheR_276_11845@a/beans
                            http://www.springframework.org/scheR_276_11845@a/beans/spring-beans.xsd
                            http://www.springframework.org/scheR_276_11845@a/context
                            http://www.springframework.org/scheR_276_11845@a/context/spring-context-3.1.xsd
                            http://www.springframework.org/scheR_276_11845@a/tx
                            http://www.springframework.org/scheR_276_11845@a/tx/spring-tx-3.0.xsd
                            http://www.springframework.org/scheR_276_11845@a/jdbc
                            http://www.springframework.org/scheR_276_11845@a/jdbc/spring-jdbc-3.0.xsd
                            http://www.springframework.org/scheR_276_11845@a/jee
                            http://www.springframework.org/scheR_276_11845@a/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>

这是我的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}")
    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;
    }


}

我认为我缺少的部分如下,但请多检查我的课

<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" />

大佬总结

以上是大佬教程为你收集整理的从Spring MVC XML文件移动到javaconfig。我的数据库XML文件真的让我迷茫了全部内容,希望文章能够帮你解决从Spring MVC XML文件移动到javaconfig。我的数据库XML文件真的让我迷茫了所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。