Spring-dispatcher-servlet.xml到Java Config

时间:2016-02-08 02:16:20

标签: java spring hibernate spring-mvc spring-data

您好我正在尝试将我的Xml配置转换为Java Config。我现在真的输了。

这是 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>

以下是我为 Java Config 提出的建议。

@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;

    }
}

非常感谢您的帮助。

0 个答案:

没有答案