Spring-dispatcher-servlet.xml到Java配置

Spring-dispatcher-servlet.xml到Java配置,java,spring,hibernate,spring-mvc,spring-data,Java,Spring,Hibernate,Spring Mvc,Spring Data,嗨,我正在尝试将Xml配置转换为Java配置。我现在真的迷路了 下面是spring dispatcher servlet.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" x

嗨,我正在尝试将Xml配置转换为Java配置。我现在真的迷路了

下面是spring dispatcher servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- package of spring controller -->
    <context:component-scan base-package="com.test" />
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="cacheSeconds" value="0" />
    </bean>

    <mvc:resources mapping="/Resources/**" location="/Resources/"
        cache-period="31556926" />

    <bean id="viewResolver1" class="org.springframework.web.servlet.view.XmlViewResolver">
        <property name="location" value="/WEB-INF/excel-view.xml" />
    </bean>

    <bean id="viewResolver2"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- setting maximum upload size -->
        <property name="maxUploadSize" value="100000" />
    </bean>

    <!-- Read test.properties -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:test.properties</value>
        </property>
    </bean>

    <bean id="entityManagerFactoryBean"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- This makes /META-INF/persistence.xml is no longer necessary -->
        <property name="packagesToScan" value="com.beo.model" />
        <!-- JpaVendorAdapter implementation for Hibernate EntityManager. Exposes 
            Hibernate's persistence provider and EntityManager extension interface -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
                <!-- <prop key="hibernate.show_sql">true</prop> -->
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
        <!-- Hikari Config -->
        <property name="dataSourceClassName" value="${hikari.dataSourceClassName}" />
        <property name="maximumPoolSize" value="${hikari.maximumPoolSize}" />
        <property name="maxLifetime" value="${hikari.maxLifetime}" />
        <property name="idleTimeout" value="${hikari.idleTimeout}" />

        <property name="dataSourceProperties">
            <props>
                <prop key="url">${jdbc.url}</prop>
                <prop key="user">${jdbc.username}</prop>
                <prop key="password">${jdbc.password}</prop>
            </props>
        </property>
    </bean>


    <!-- This transaction manager is appropriate for applications that use a 
        single JPA EntityManagerFactory for transactional data access. JTA (usually 
        through JtaTransactionManager) is necessary for accessing multiple transactional 
        resources within the same transaction. -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
    </bean>

    <!-- responsible for registering the necessary Spring components that power 
        annotation-driven transaction management; such as when @Transactional methods 
        are invoked -->
    <tx:annotation-driven />
</beans>

/WEB-INF/jsp/
.jsp
类路径:test.properties
org.hibernate.dialogue.PostgreSqlDialogue
${jdbc.url}
${jdbc.username}
${jdbc.password}
下面是我为Java配置准备的内容

@Configuration
@EnableWebMvc
@ComponentScan("com.test")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/Resources/**")
                .addResourceLocations("/Resources/").setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public ViewResolver setupViewResolver(ContentNegotiationManager manager) {
        List<ViewResolver> resolvers = new ArrayList<ViewResolver>();


        InternalResourceViewResolver InternalResourceResolver = new InternalResourceViewResolver();
        InternalResourceResolver.setPrefix("/WEB-INF/jsp/");
        InternalResourceResolver.setSuffix(".jsp");
        resolvers.add(InternalResourceResolver);

        ContentNegotiatingViewResolver resolver2 = new ContentNegotiatingViewResolver();
        resolver2.setViewResolvers(resolvers);
        resolver2.setContentNegotiationManager(manager);
        return resolver2;

    }
}
@配置
@EnableWebMvc
@组件扫描(“com.test”)
公共类WebConfig扩展了WebMVCConfigureAdapter{
@凌驾
public void addResourceHandlers(最终ResourceHandlerRegistry注册表){
registry.addResourceHandler(“/Resources/**”)
.addResourceLocations(“/Resources/”).setCachePeriod(31556926);
}
@凌驾
公共无效配置DefaultServletHandling(
DefaultServletHandlerConfigurer(配置器){
configurer.enable();
}
@豆子
公共视图解析程序设置视图解析程序(ContentNegotiationManager){
列表解析程序=新的ArrayList();
InternalResourceViewResolver InternalResourceResolver=新的InternalResourceViewResolver();
setPrefix(“/WEB-INF/jsp/”);
setSuffix(“.jsp”);
添加(InternalResourceResolver);
contentnegotingviewresolver 2=新contentnegotingviewresolver();
解析器2.SetViewResolver(解析器);
Resolve2.setContentNegotiationManager(经理);
返回解析程序2;
}
}

非常感谢您的帮助。

您没有说,您有什么问题。您有什么问题吗?实际上我的主要问题是,如何将Excelviewresolver添加到ContentNegotiatingViewResolver?您的配置中没有
Excelviewresolver
。你是说
xmlviewsolver
?哦,是的。很抱歉我想丢弃xml文件并将其转换为bean,然后将其添加到viewsolver.excel-view.xml仅包含