powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / Spring 3 Контексты
5 сообщений из 5, страница 1 из 1
Spring 3 Контексты
    #37829127
scymaks
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Возник у меня следующий вопрос...

Вот есть у меня spring-servlet.xml в нем описано что есть какие-то бины (scope = singleton)....

в web.xml есть вот такой вот код:

Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>



Когда я запускаю приложение, то в логах вижу, что создается два инстанса бинов. Почему?
...
Рейтинг: 0 / 0
Spring 3 Контексты
    #37829163
svenom
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
А зачем вы используете org.springframework.web.context.ContextLoaderListener?
И как вы понимаете, что создается по несколько инстансов бинов? Вы видите в логе, что контекст разворачивается дважды?
...
Рейтинг: 0 / 0
Spring 3 Контексты
    #37829339
Олег Артемов
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>


Имхо, вот здесь проблема. Создай 2 разных файла - один для рутового контекста, один для Servlet-а (сервлету пропиши его контекст и все). Вот как у меня настроено:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:services.xml</param-value>
  </context-param>

  <servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
...
Рейтинг: 0 / 0
Spring 3 Контексты
    #37830033
scymaks
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Олег Артемов,

я правильно понял, что в services.xml описаны всякие бины, которые нужны именно для приложения?
А в mvc.xml, видимо, что-то типа такого:

Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
    
    <context:component-scan base-package="ru.package" />
    <mvc:annotation-driven />

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



?
...
Рейтинг: 0 / 0
Spring 3 Контексты
    #37830116
Олег Артемов
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Вот что у меня описано:
services.xml

Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
  <?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:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

  <context:annotation-config/>

  <context:component-scan base-package="com.pmstation" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
  </context:component-scan>

  <task:annotation-driven executor="tasksExecutor" scheduler="taskScheduler"/>
  <task:executor id="tasksExecutor" pool-size="5"/>
  <task:scheduler id="taskScheduler" pool-size="10"/>
</beans>



mvc.xml
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
  <?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-3.1.xsd
           http://www.springframework.org/schema/context
		   http://www.springframework.org/schema/context/spring-context-3.1.xsd
		   http://www.springframework.org/schema/mvc
		   http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

	<mvc:annotation-driven >
       <mvc:message-converters register-defaults="true">
           <bean class="com.pmstation.common.spring.jsonp.JsonpHttpMessageConverter"/>
       </mvc:message-converters>
    </mvc:annotation-driven>

	<context:annotation-config />


	<context:component-scan base-package="com.pmstation" use-default-filters="false">
	   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
       <context:include-filter type="annotation" expression="com.pmstation.common.spring.stereotype.ViewService"/>
       <context:include-filter type="annotation" expression="com.pmstation.common.spring.stereotype.ViewValidator"/>
	</context:component-scan>

    <bean name="jstlViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
        <property name="order" value="1"/>
    </bean>


</beans>
...
Рейтинг: 0 / 0
5 сообщений из 5, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / Spring 3 Контексты
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]