스프링 구성을 이해하려고합니다. 나는 두 기사를 읽기 : "응용 프로그램 컨텍스트"와 "웹 응용 프로그램 컨텍스트"스프링의 최적 구성 3.2.3
- http://www.onjava.com/pub/a/onjava/2006/03/22/advanced-spring-configuration.html?page=1
- http://syntx.io/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/
다음은이 개 설정 파일을 가지고하는 것이 좋습니다.
/WEB-INF/applicationContext.xml을을 사용하면 다음을 할 수 있습니다 : 당신이 이제까지 스프링 MVC 프레임 워크를 사용하여 웹 응용 프로그램을 개발하기 위해 노력하는 경우
, 당신은 사용해야 두 개의 구성 파일이 있다는 것을 알고 빈을 구성하거나 을 사용하여 응용 프로그램의 컨텍스트를 표시하십시오. 이 곳에서 비즈니스 로직 빈, 리소스 및 웹 계층과 직접 관련이없는 다른 모든 콩 을 정의하는 곳입니다.
/WEB-INF/[servlet-name]-servlet.xml은 웹 계층에게 볼 리졸버, 컨트롤러, 유효성 검사기, 그리고 당신은 MVC 프레임 워크에 필요한 다른 모든 콩을 구성하는 데 사용됩니다. [servlet-name]은 web.xml 배치 서술자에 정의 된 Spring의 디스패처 서블릿 이름 을 참조합니다.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Application Context --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml, /WEB-INF/spring-security.xml</param-value> </context-param> <!-- Spring MVC --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring Security --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Fin Spring Security --> </web-app>
이 내 applicationContext.xml이다 :
이에 따르면, 나는 다음과 같이 내 web.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:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- Look in tom cats context -->
<jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/rhcimax"/>
<!-- Hibernate Session Factory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>com.blah.baseProject</value>
</array>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
그리고 이것은 내 MVC-디스패처 - 서블릿이다. 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:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- Enable @Controller annotation support -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Map simple view name such as "test" into /WEB-INF/views/test.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
<context:component-scan base-package="com.blah.baseProject"/>
</beans>
이 구성이 거의 불분명한지 알고 싶습니다. rect. 이 구성의 실행은하지만 난이 예외가 있기 때문에 applicationContext.xml가 호출되지 것을 느낄 :
org.hibernate.HibernateException: No Session found for current thread
내 의도가 봄에 좋은 관행을 유지하고 올바른 구성을 배우는 것입니다.
는 "중간 계층 사이에 명확한 분리를 유지하는 것이 좋습니다 등 (보통의 ApplicationContext에 정의되어있다)와 같은 관련 구성 요소를 웹 - 비즈니스 로직 구성 요소와 데이터 액세스 클래스 을 같은 서비스 컨트롤러 및보기 확인자 (즉, 은 Dispatcher 서블릿 당 WebApplicationContext에 정의되어 있습니다.) 내 경험에
그는 대단히 감사합니다 !! 내가 언급 한 것처럼 서블릿 구성에서 컨트롤러에 대해서만 구성 요소 스캔을 선언했습니다. 이제는 모든 것이 잘 작동합니다 !! 나는 예외를 얻지 않는다 :). 마지막으로 한 가지 : application-context.xml과 application-context-db.xml을 공유 할 수 있습니까? – kiduxa
완료. 업데이트 된 답변보기 – yname