下面列出了怎么用org.springframework.beans.factory.config.CustomScopeConfigurer的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testExpectedSpringBehaviour() throws NoSuchFieldException, IllegalAccessException {
Field scopesField = CustomScopeConfigurer.class.getDeclaredField("scopes");
scopesField.setAccessible(true);
Map<String, org.springframework.beans.factory.config.Scope> scopes = (Map<String, org.springframework.beans.factory.config.Scope>) scopesField.get(this.testScopeConfigurer);
TestConfig.TestScope testScope = (TestConfig.TestScope) scopes.get("test");
BeanWithTwoDestructionCallbacks bean = this.applicationContext.getBean(BeanWithTwoDestructionCallbacks.class);
assertThat(bean.destroyCalled).isFalse();
assertThat(bean.preDestroyCalled).isFalse();
assertThat(testScope.getBeans()).hasSize(1);
assertThat(testScope.getCallbacks()).hasSize(1);
//Only one destruction callback per bean
List<Runnable> destructionCallbacks = testScope.getCallbacks().get("beanWithTwoDestructionCallbacks");
assertThat(destructionCallbacks).hasSize(1);
//The one destruction callback calls all destroy-methods
destructionCallbacks.get(0).run();
assertThat(bean.preDestroyCalled).isTrue();
assertThat(bean.destroyCalled).isTrue();
}
@Bean
@ConditionalOnProperty(value = "joinfaces.view-scope.enabled", havingValue = "true", matchIfMissing = true)
public static CustomScopeConfigurer viewScopeConfigurer() {
CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();
customScopeConfigurer.addScope(ViewScope.SCOPE_VIEW, new ViewScope());
return customScopeConfigurer;
}
@Bean
public static CustomScopeConfigurer testScopeConfigurer() {
CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();
customScopeConfigurer.addScope("test", new TestScope());
return customScopeConfigurer;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public static CustomScopeConfigurer viewScopeConfigurer() {
CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();
Map scopes = new HashMap();
scopes.put("view", new ViewScope());
customScopeConfigurer.setScopes(scopes);
return customScopeConfigurer;
}
@Bean(name = "customScopeConfigurer")
public static CustomScopeConfigurer createClientScope(final ClientSession clientSession) {
Assert.requireNonNull(clientSession, "clientSession");
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope(ClientScopeImpl.CLIENT_SCOPE, new TestClientScope(clientSession));
return configurer;
}
/**
* Declare the "tenant" scope.
*/
@Bean
public static CustomScopeConfigurer customScopeConfigurer (TenantScope tenantScope) {
CustomScopeConfigurer configurer = new CustomScopeConfigurer ();
configurer.addScope("tenant", tenantScope);
return configurer;
}
@SuppressWarnings("serial")
@Bean public org.springframework.beans.factory.config.CustomScopeConfigurer customScopeConfigurer(){
CustomScopeConfigurer csc = new CustomScopeConfigurer();
csc.setScopes(new HashMap<String, Object>() {{
put("view", new com.liferay.faces.demos.spring.ViewScope());
}});
return csc;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public static CustomScopeConfigurer viewScopeConfigurer() {
CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();
Map scopes = new HashMap();
scopes.put("view", new ViewScope());
customScopeConfigurer.setScopes(scopes);
return customScopeConfigurer;
}
@Bean
public static CustomScopeConfigurer customScopeConfigurer() {
Map<String, Object> scopes = new HashMap<>();
scopes.put("view", new ViewScope());
CustomScopeConfigurer bean = new CustomScopeConfigurer();
bean.setScopes(scopes);
return bean;
}
@Bean
public static CustomScopeConfigurer webSocketScopeConfigurer() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope("websocket", new SimpSessionScope());
return configurer;
}
@Override
public BeanDefinition parse(Element element, ParserContext context) {
Object source = context.extractSource(element);
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
context.pushContainingComponent(compDefinition);
Element channelElem = DomUtils.getChildElementByTagName(element, "client-inbound-channel");
RuntimeBeanReference inChannel = getMessageChannel("clientInboundChannel", channelElem, context, source);
channelElem = DomUtils.getChildElementByTagName(element, "client-outbound-channel");
RuntimeBeanReference outChannel = getMessageChannel("clientOutboundChannel", channelElem, context, source);
channelElem = DomUtils.getChildElementByTagName(element, "broker-channel");
RuntimeBeanReference brokerChannel = getMessageChannel("brokerChannel", channelElem, context, source);
RuntimeBeanReference userRegistry = registerUserRegistry(element, context, source);
Object userDestHandler = registerUserDestHandler(element, userRegistry, inChannel, brokerChannel, context, source);
RuntimeBeanReference converter = registerMessageConverter(element, context, source);
RuntimeBeanReference template = registerMessagingTemplate(element, brokerChannel, converter, context, source);
registerAnnotationMethodMessageHandler(element, inChannel, outChannel,converter, template, context, source);
RootBeanDefinition broker = registerMessageBroker(element, inChannel, outChannel, brokerChannel,
userDestHandler, template, userRegistry, context, source);
// WebSocket and sub-protocol handling
ManagedMap<String, Object> urlMap = registerHandlerMapping(element, context, source);
RuntimeBeanReference stompHandler = registerStompHandler(element, inChannel, outChannel, context, source);
for (Element endpointElem : DomUtils.getChildElementsByTagName(element, "stomp-endpoint")) {
RuntimeBeanReference requestHandler = registerRequestHandler(endpointElem, stompHandler, context, source);
String pathAttribute = endpointElem.getAttribute("path");
Assert.hasText(pathAttribute, "Invalid <stomp-endpoint> (no path mapping)");
for (String path : StringUtils.tokenizeToStringArray(pathAttribute, ",")) {
path = path.trim();
Assert.hasText(path, () -> "Invalid <stomp-endpoint> path attribute: " + pathAttribute);
if (DomUtils.getChildElementByTagName(endpointElem, "sockjs") != null) {
path = (path.endsWith("/") ? path + "**" : path + "/**");
}
urlMap.put(path, requestHandler);
}
}
Map<String, Object> scopeMap = Collections.singletonMap("websocket", new SimpSessionScope());
RootBeanDefinition scopeConfigurer = new RootBeanDefinition(CustomScopeConfigurer.class);
scopeConfigurer.getPropertyValues().add("scopes", scopeMap);
registerBeanDefByName("webSocketScopeConfigurer", scopeConfigurer, context, source);
registerWebSocketMessageBrokerStats(broker, inChannel, outChannel, context, source);
context.popAndRegisterContainingComponent();
return null;
}
@Bean
public static CustomScopeConfigurer webSocketScopeConfigurer() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope("websocket", new SimpSessionScope());
return configurer;
}
@Override
public BeanDefinition parse(Element element, ParserContext context) {
Object source = context.extractSource(element);
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
context.pushContainingComponent(compDefinition);
Element channelElem = DomUtils.getChildElementByTagName(element, "client-inbound-channel");
RuntimeBeanReference inChannel = getMessageChannel("clientInboundChannel", channelElem, context, source);
channelElem = DomUtils.getChildElementByTagName(element, "client-outbound-channel");
RuntimeBeanReference outChannel = getMessageChannel("clientOutboundChannel", channelElem, context, source);
channelElem = DomUtils.getChildElementByTagName(element, "broker-channel");
RuntimeBeanReference brokerChannel = getMessageChannel("brokerChannel", channelElem, context, source);
RuntimeBeanReference userRegistry = registerUserRegistry(element, context, source);
Object userDestHandler = registerUserDestHandler(element, userRegistry, inChannel, brokerChannel, context, source);
RuntimeBeanReference converter = registerMessageConverter(element, context, source);
RuntimeBeanReference template = registerMessagingTemplate(element, brokerChannel, converter, context, source);
registerAnnotationMethodMessageHandler(element, inChannel, outChannel,converter, template, context, source);
RootBeanDefinition broker = registerMessageBroker(element, inChannel, outChannel, brokerChannel,
userDestHandler, template, userRegistry, context, source);
// WebSocket and sub-protocol handling
ManagedMap<String, Object> urlMap = registerHandlerMapping(element, context, source);
RuntimeBeanReference stompHandler = registerStompHandler(element, inChannel, outChannel, context, source);
for (Element endpointElem : DomUtils.getChildElementsByTagName(element, "stomp-endpoint")) {
RuntimeBeanReference requestHandler = registerRequestHandler(endpointElem, stompHandler, context, source);
String pathAttribute = endpointElem.getAttribute("path");
Assert.hasText(pathAttribute, "Invalid <stomp-endpoint> (no path mapping)");
for (String path : StringUtils.tokenizeToStringArray(pathAttribute, ",")) {
path = path.trim();
Assert.hasText(path, () -> "Invalid <stomp-endpoint> path attribute: " + pathAttribute);
if (DomUtils.getChildElementByTagName(endpointElem, "sockjs") != null) {
path = (path.endsWith("/") ? path + "**" : path + "/**");
}
urlMap.put(path, requestHandler);
}
}
Map<String, Object> scopeMap = Collections.singletonMap("websocket", new SimpSessionScope());
RootBeanDefinition scopeConfigurer = new RootBeanDefinition(CustomScopeConfigurer.class);
scopeConfigurer.getPropertyValues().add("scopes", scopeMap);
registerBeanDefByName("webSocketScopeConfigurer", scopeConfigurer, context, source);
registerWebSocketMessageBrokerStats(broker, inChannel, outChannel, context, source);
context.popAndRegisterContainingComponent();
return null;
}
@Bean
static CustomScopeConfigurer customScopeConfigurer() {
final CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.setScopes(Collections.singletonMap(VertxContextScope.NAME, new VertxContextScope()));
return configurer;
}
@Bean
public static CustomScopeConfigurer webSocketScopeConfigurer() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope("websocket", new SimpSessionScope());
return configurer;
}
@Override
public BeanDefinition parse(Element element, ParserContext context) {
Object source = context.extractSource(element);
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
context.pushContainingComponent(compDefinition);
Element channelElem = DomUtils.getChildElementByTagName(element, "client-inbound-channel");
RuntimeBeanReference inChannel = getMessageChannel("clientInboundChannel", channelElem, context, source);
channelElem = DomUtils.getChildElementByTagName(element, "client-outbound-channel");
RuntimeBeanReference outChannel = getMessageChannel("clientOutboundChannel", channelElem, context, source);
channelElem = DomUtils.getChildElementByTagName(element, "broker-channel");
RuntimeBeanReference brokerChannel = getMessageChannel("brokerChannel", channelElem, context, source);
RuntimeBeanReference userRegistry = registerUserRegistry(element, context, source);
Object userDestHandler = registerUserDestHandler(element, userRegistry, inChannel, brokerChannel, context, source);
RuntimeBeanReference converter = registerMessageConverter(element, context, source);
RuntimeBeanReference template = registerMessagingTemplate(element, brokerChannel, converter, context, source);
registerAnnotationMethodMessageHandler(element, inChannel, outChannel,converter, template, context, source);
RootBeanDefinition broker = registerMessageBroker(element, inChannel, outChannel, brokerChannel,
userDestHandler, template, userRegistry, context, source);
// WebSocket and sub-protocol handling
ManagedMap<String, Object> urlMap = registerHandlerMapping(element, context, source);
RuntimeBeanReference stompHandler = registerStompHandler(element, inChannel, outChannel, context, source);
for (Element endpointElem : DomUtils.getChildElementsByTagName(element, "stomp-endpoint")) {
RuntimeBeanReference requestHandler = registerRequestHandler(endpointElem, stompHandler, context, source);
String pathAttribute = endpointElem.getAttribute("path");
Assert.state(StringUtils.hasText(pathAttribute), "Invalid <stomp-endpoint> (no path mapping)");
List<String> paths = Arrays.asList(StringUtils.tokenizeToStringArray(pathAttribute, ","));
for (String path : paths) {
path = path.trim();
Assert.state(StringUtils.hasText(path), "Invalid <stomp-endpoint> path attribute: " + pathAttribute);
if (DomUtils.getChildElementByTagName(endpointElem, "sockjs") != null) {
path = path.endsWith("/") ? path + "**" : path + "/**";
}
urlMap.put(path, requestHandler);
}
}
Map<String, Object> scopeMap = Collections.<String, Object>singletonMap("websocket", new SimpSessionScope());
RootBeanDefinition scopeConfigurer = new RootBeanDefinition(CustomScopeConfigurer.class);
scopeConfigurer.getPropertyValues().add("scopes", scopeMap);
registerBeanDefByName("webSocketScopeConfigurer", scopeConfigurer, context, source);
registerWebSocketMessageBrokerStats(broker, inChannel, outChannel, context, source);
context.popAndRegisterContainingComponent();
return null;
}
@Bean(name = "customScopeConfigurer")
public static CustomScopeConfigurer createClientScope() {
final CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope(ClientScopeImpl.CLIENT_SCOPE, new ClientScopeImpl());
return configurer;
}
@Bean
public CustomScopeConfigurer customScopeConfigurer() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope("session", new SimpleThreadScope());
return configurer;
}