Spring boot interaction with Spring MVC + Spring S

2019-08-24 18:04发布

问题:

I have finished my work on Spring-based(Spring-MVC) web application that also uses Spring Security, Hibernate, HSQLDB (and Maven of course). I was running my web app on Jetyy 9.x server integrated in IntelliJ IDEA, but now I need it to be packaged in single executable jar with embedded Jetty server.

People offered me to use Spring boot for embedding jetty purpose. So as I found out (because tried and read some info) Spring boot does not understand my xml configuration files that my app was using for configuring Spring-MVC and Spring Security.

How can I make all spring components interract with each other and what sould I do for that considering to my current (non spring boot project structure)?

The project structure and settings (non spring boot):

Code of configuration files:

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:file:${webroot}/WEB-INF/classes/DBStorage/testdb</property>
        <property name="connection.username">SA</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">5</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup
        <property name="hbm2ddl.auto">create</property>-->


        <!-- Mapping files -->
        <mapping resource="User.hbm.xml"/>
        <mapping resource="Disk.hbm.xml"/>
        <mapping resource="Takenitem.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

dispatcher-servlet.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing
         infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving
        up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <context:component-scan base-package="com.dvdexchange" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources
        in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

spring-database.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- HSQLDB data source -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:file:${webroot}/WEB-INF/classes/DBStorage/testdb" />
        <property name="username" value="SA" />
        <property name="password" value="" />
    </bean>

    <!-- Hibernate session factory -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>User.hbm.xml</value>
                <value>Disk.hbm.xml</value>
                <value>Takenitem.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQL5Dialect
                </prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="userDAO" class="com.dvdexchange.dao.impl.UserDAOHibernateImpl"/>

    <bean id="myUserServiceImpl" class="com.dvdexchange.service.impl.UserServiceImpl">
        <property name="userDAO" ref="userDAO" />
    </bean>

    <!-- MUST have transaction manager, using aop and aspects  -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="userServicePointCut"
                      expression="execution(* com.dvdexchange.service.*ServiceImpl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointCut" />
    </aop:config>

</beans>

spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <!-- enable use-expressions -->
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/diskLib**" access="hasRole('ROLE_USER')"/>
        <intercept-url pattern="/borrowDisk**" access="hasRole('ROLE_USER')"/>
        <intercept-url pattern="/takenDisks**" access="hasRole('ROLE_USER')"/>
        <intercept-url pattern="/givenDisks**" access="hasRole('ROLE_USER')"/>

        <!-- access denied page -->
        <access-denied-handler error-page="/403"/>

        <form-login
                login-page="/login"
                default-target-url="/diskLib"
                authentication-failure-url="/login?error"
                username-parameter="username"
                password-parameter="password"/>
        <logout logout-success-url="/login?logout"/>
        <!-- enable csrf protection -->
        <csrf/>
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref="myUserServiceImpl">
            <password-encoder hash="md5"/>
        </authentication-provider>
    </authentication-manager>

</beans:beans