나는 특정 서비스를 주입하는 컨트롤러와 필터를 가지고 있습니다. 해당 서비스에서 특정 정보를 저장하려고하는 해시 맵이 있습니다. 내가 겪고있는 문제는 해당 서비스의 단일 인스턴스가 만들어져 내 컨트롤러와 필터에 주입되어지도의 두 인스턴스가있는 것처럼 보입니다. 왜 그랬는지 나는 상실했다. 아무리 맵을 인스턴스화 (또는 삽입)해도 동작은 동일합니다.스프링 싱글 톤 @ 자동 전화 서비스 및 공유 상태
서비스의 두 인스턴스가 생성되어 컨트롤러와 필터에 각각 하나씩 삽입되어 주입된다는 점이 밝혀졌습니다. 왜 이런 일이 일어나고 어떻게 해결해야하는지 명확하지 않습니다.
는 다음 코드의 추출물 : 여기
@Controller
public MyController {
@Autowired
private MyService myService;
someEndpoint() {
....
myService.putData(key, value);
.....
}
}
public class MyFilter extends GenericFilterBean {
@Autowired
private MyService myService;
public void doFilter(...) {
//this is where I have a problem.
// the reference myService.myMap seems to be pointing to a different instance
// than the service.myMap in the controller which doesn't make any sense to me
// the filter obviously intercepts all requests so I would expect that after that particular
// endpoint is accessed the data will be there for subsequent requests
myService.getData(..);
}
.....
}
@Service
public class MyService {
private Map <String,String> myMap = new HashMap <String,String>();
public String getData(String key) {
return myMap.get(key);
}
public void putData(String key, String value){
myMap.put(key,value);
}
}
이 앱 config.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:tx="http://www.springframework.org/schema/tx"
xmlns:security="http://www.springframework.org/schema/security"
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/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<context:component-scan base-package="com.mycompany.myPackage"/>
<context:annotation-config />
.......
<security:http
........
........
<security:custom-filter ref="myFilter" position="FORM_LOGIN_FILTER" />
....
...........
<bean class="com.mycompany.filters.MyFilter" id="myFilter"/>
과의 web.xml의 추출물
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher Servlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<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>
도움을 주시면 대단히 감사하겠습니다.
몇 개의 컨텍스트가 있습니까? web.xml을 게시 할 수 있습니까? 또한 문맥 구성? – grid