Search in sources :

Example 46 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.

the class LazyAutowiredAnnotationBeanPostProcessorTests method testLazyOptionalResourceInjectionWithNonExistingTarget.

@Test
public void testLazyOptionalResourceInjectionWithNonExistingTarget() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition bd = new RootBeanDefinition(OptionalFieldResourceInjectionBean.class);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", bd);
    OptionalFieldResourceInjectionBean bean = (OptionalFieldResourceInjectionBean) bf.getBean("annotatedBean");
    assertNotNull(bean.getTestBean());
    try {
        bean.getTestBean().getName();
        fail("Should have thrown NoSuchBeanDefinitionException");
    } catch (NoSuchBeanDefinitionException ex) {
    // expected;
    }
}
Also used : AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 47 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.

the class CommonAnnotationBeanPostProcessorTests method testExtendedResourceInjectionWithOverriding.

@Test
public void testExtendedResourceInjectionWithOverriding() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerResolvableDependency(BeanFactory.class, bf);
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("tb", "testBean3");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(bf);
    RootBeanDefinition annotatedBd = new RootBeanDefinition(ExtendedResourceInjectionBean.class);
    TestBean tb5 = new TestBean();
    annotatedBd.getPropertyValues().add("testBean2", tb5);
    bf.registerBeanDefinition("annotatedBean", annotatedBd);
    bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(NamedResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    TestBean tb2 = new TestBean();
    bf.registerSingleton("testBean2", tb2);
    TestBean tb3 = new TestBean();
    bf.registerSingleton("testBean3", tb3);
    TestBean tb4 = new TestBean();
    bf.registerSingleton("testBean4", tb4);
    NestedTestBean tb6 = new NestedTestBean();
    bf.registerSingleton("xy", tb6);
    ExtendedResourceInjectionBean bean = (ExtendedResourceInjectionBean) bf.getBean("annotatedBean");
    assertTrue(bean.initCalled);
    assertTrue(bean.init2Called);
    assertSame(tb, bean.getTestBean());
    assertSame(tb5, bean.getTestBean2());
    assertSame(tb4, bean.getTestBean3());
    assertSame(tb3, bean.getTestBean4());
    assertSame(tb6, bean.testBean5);
    assertSame(tb6, bean.testBean6);
    assertSame(bf, bean.beanFactory);
    try {
        bf.getBean("annotatedBean2");
    } catch (BeanCreationException ex) {
        assertTrue(ex.getRootCause() instanceof NoSuchBeanDefinitionException);
        NoSuchBeanDefinitionException innerEx = (NoSuchBeanDefinitionException) ex.getRootCause();
        assertEquals("testBean9", innerEx.getBeanName());
    }
    bf.destroySingletons();
    assertTrue(bean.destroyCalled);
    assertTrue(bean.destroy2Called);
}
Also used : PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) BeanCreationException(org.springframework.beans.factory.BeanCreationException) ITestBean(org.springframework.tests.sample.beans.ITestBean) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Properties(java.util.Properties) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 48 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.

the class TestBean method getBeanByTypeRaisesNoSuchBeanDefinitionException.

@Test
public void getBeanByTypeRaisesNoSuchBeanDefinitionException() {
    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
    // attempt to retrieve a bean that does not exist
    Class<?> targetType = Pattern.class;
    try {
        context.getBean(targetType);
        fail("Should have thrown NoSuchBeanDefinitionException");
    } catch (NoSuchBeanDefinitionException ex) {
        assertThat(ex.getMessage(), containsString(format("No qualifying bean of type '%s'", targetType.getName())));
    }
}
Also used : Pattern(java.util.regex.Pattern) ApplicationContext(org.springframework.context.ApplicationContext) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Test(org.junit.Test)

Example 49 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.

the class FactoryBeanAccessTests method factoryBeanAccess.

@Test
public void factoryBeanAccess() {
    // SPR9511
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(new SimpleBeanResolver());
    Expression expr = new SpelExpressionParser().parseRaw("@car.colour");
    assertEquals("red", expr.getValue(context));
    expr = new SpelExpressionParser().parseRaw("&car.class.name");
    assertEquals(CarFactoryBean.class.getName(), expr.getValue(context));
    expr = new SpelExpressionParser().parseRaw("@boat.colour");
    assertEquals("blue", expr.getValue(context));
    expr = new SpelExpressionParser().parseRaw("&boat.class.name");
    try {
        assertEquals(Boat.class.getName(), expr.getValue(context));
        fail("Expected BeanIsNotAFactoryException");
    } catch (BeanIsNotAFactoryException binafe) {
    // success
    }
    // No such bean
    try {
        expr = new SpelExpressionParser().parseRaw("@truck");
        assertEquals("red", expr.getValue(context));
        fail("Expected NoSuchBeanDefinitionException");
    } catch (NoSuchBeanDefinitionException nsbde) {
    // success
    }
    // No such factory bean
    try {
        expr = new SpelExpressionParser().parseRaw("&truck");
        assertEquals(CarFactoryBean.class.getName(), expr.getValue(context));
        fail("Expected NoSuchBeanDefinitionException");
    } catch (NoSuchBeanDefinitionException nsbde) {
    // success
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) CarFactoryBean(org.springframework.context.expression.FactoryBeanAccessTests.SimpleBeanResolver.CarFactoryBean) BeanIsNotAFactoryException(org.springframework.beans.factory.BeanIsNotAFactoryException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Boat(org.springframework.context.expression.FactoryBeanAccessTests.SimpleBeanResolver.Boat) Test(org.junit.Test)

Example 50 with NoSuchBeanDefinitionException

use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.

the class TestValidator method simpleBroker.

@Test
@SuppressWarnings("unchecked")
public void simpleBroker() throws Exception {
    loadBeanDefinitions("websocket-config-broker-simple.xml");
    HandlerMapping hm = this.appContext.getBean(HandlerMapping.class);
    assertThat(hm, Matchers.instanceOf(SimpleUrlHandlerMapping.class));
    SimpleUrlHandlerMapping suhm = (SimpleUrlHandlerMapping) hm;
    assertThat(suhm.getUrlMap().keySet(), Matchers.hasSize(4));
    assertThat(suhm.getUrlMap().values(), Matchers.hasSize(4));
    HttpRequestHandler httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/foo");
    assertNotNull(httpRequestHandler);
    assertThat(httpRequestHandler, Matchers.instanceOf(WebSocketHttpRequestHandler.class));
    WebSocketHttpRequestHandler wsHttpRequestHandler = (WebSocketHttpRequestHandler) httpRequestHandler;
    HandshakeHandler handshakeHandler = wsHttpRequestHandler.getHandshakeHandler();
    assertNotNull(handshakeHandler);
    assertTrue(handshakeHandler instanceof TestHandshakeHandler);
    List<HandshakeInterceptor> interceptors = wsHttpRequestHandler.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class), instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
    WebSocketSession session = new TestWebSocketSession("id");
    wsHttpRequestHandler.getWebSocketHandler().afterConnectionEstablished(session);
    assertEquals(true, session.getAttributes().get("decorated"));
    WebSocketHandler wsHandler = wsHttpRequestHandler.getWebSocketHandler();
    assertThat(wsHandler, Matchers.instanceOf(ExceptionWebSocketHandlerDecorator.class));
    wsHandler = ((ExceptionWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler, Matchers.instanceOf(LoggingWebSocketHandlerDecorator.class));
    wsHandler = ((LoggingWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler, Matchers.instanceOf(TestWebSocketHandlerDecorator.class));
    wsHandler = ((TestWebSocketHandlerDecorator) wsHandler).getDelegate();
    assertThat(wsHandler, Matchers.instanceOf(SubProtocolWebSocketHandler.class));
    assertSame(wsHandler, this.appContext.getBean(MessageBrokerBeanDefinitionParser.WEB_SOCKET_HANDLER_BEAN_NAME));
    SubProtocolWebSocketHandler subProtocolWsHandler = (SubProtocolWebSocketHandler) wsHandler;
    assertEquals(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"), subProtocolWsHandler.getSubProtocols());
    assertEquals(25 * 1000, subProtocolWsHandler.getSendTimeLimit());
    assertEquals(1024 * 1024, subProtocolWsHandler.getSendBufferSizeLimit());
    Map<String, SubProtocolHandler> handlerMap = subProtocolWsHandler.getProtocolHandlerMap();
    StompSubProtocolHandler stompHandler = (StompSubProtocolHandler) handlerMap.get("v12.stomp");
    assertNotNull(stompHandler);
    assertEquals(128 * 1024, stompHandler.getMessageSizeLimit());
    assertNotNull(stompHandler.getErrorHandler());
    assertEquals(TestStompErrorHandler.class, stompHandler.getErrorHandler().getClass());
    assertNotNull(new DirectFieldAccessor(stompHandler).getPropertyValue("eventPublisher"));
    httpRequestHandler = (HttpRequestHandler) suhm.getUrlMap().get("/test/**");
    assertNotNull(httpRequestHandler);
    assertThat(httpRequestHandler, Matchers.instanceOf(SockJsHttpRequestHandler.class));
    SockJsHttpRequestHandler sockJsHttpRequestHandler = (SockJsHttpRequestHandler) httpRequestHandler;
    wsHandler = unwrapWebSocketHandler(sockJsHttpRequestHandler.getWebSocketHandler());
    assertNotNull(wsHandler);
    assertThat(wsHandler, Matchers.instanceOf(SubProtocolWebSocketHandler.class));
    assertNotNull(sockJsHttpRequestHandler.getSockJsService());
    assertThat(sockJsHttpRequestHandler.getSockJsService(), Matchers.instanceOf(DefaultSockJsService.class));
    DefaultSockJsService defaultSockJsService = (DefaultSockJsService) sockJsHttpRequestHandler.getSockJsService();
    WebSocketTransportHandler wsTransportHandler = (WebSocketTransportHandler) defaultSockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
    assertNotNull(wsTransportHandler.getHandshakeHandler());
    assertThat(wsTransportHandler.getHandshakeHandler(), Matchers.instanceOf(TestHandshakeHandler.class));
    assertFalse(defaultSockJsService.shouldSuppressCors());
    ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler) defaultSockJsService.getTaskScheduler();
    assertEquals(Runtime.getRuntime().availableProcessors(), scheduler.getScheduledThreadPoolExecutor().getCorePoolSize());
    assertTrue(scheduler.getScheduledThreadPoolExecutor().getRemoveOnCancelPolicy());
    interceptors = defaultSockJsService.getHandshakeInterceptors();
    assertThat(interceptors, contains(instanceOf(FooTestInterceptor.class), instanceOf(BarTestInterceptor.class), instanceOf(OriginHandshakeInterceptor.class)));
    assertTrue(defaultSockJsService.getAllowedOrigins().contains("http://mydomain3.com"));
    assertTrue(defaultSockJsService.getAllowedOrigins().contains("http://mydomain4.com"));
    SimpUserRegistry userRegistry = this.appContext.getBean(SimpUserRegistry.class);
    assertNotNull(userRegistry);
    assertEquals(DefaultSimpUserRegistry.class, userRegistry.getClass());
    UserDestinationResolver userDestResolver = this.appContext.getBean(UserDestinationResolver.class);
    assertNotNull(userDestResolver);
    assertThat(userDestResolver, Matchers.instanceOf(DefaultUserDestinationResolver.class));
    DefaultUserDestinationResolver defaultUserDestResolver = (DefaultUserDestinationResolver) userDestResolver;
    assertEquals("/personal/", defaultUserDestResolver.getDestinationPrefix());
    UserDestinationMessageHandler userDestHandler = this.appContext.getBean(UserDestinationMessageHandler.class);
    assertNotNull(userDestHandler);
    SimpleBrokerMessageHandler brokerMessageHandler = this.appContext.getBean(SimpleBrokerMessageHandler.class);
    assertNotNull(brokerMessageHandler);
    Collection<String> prefixes = brokerMessageHandler.getDestinationPrefixes();
    assertEquals(Arrays.asList("/topic", "/queue"), new ArrayList<>(prefixes));
    assertNotNull(brokerMessageHandler.getTaskScheduler());
    assertArrayEquals(new long[] { 15000, 15000 }, brokerMessageHandler.getHeartbeatValue());
    List<Class<? extends MessageHandler>> subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, SimpleBrokerMessageHandler.class);
    testChannel("clientInboundChannel", subscriberTypes, 2);
    testExecutor("clientInboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Collections.singletonList(SubProtocolWebSocketHandler.class);
    testChannel("clientOutboundChannel", subscriberTypes, 1);
    testExecutor("clientOutboundChannel", Runtime.getRuntime().availableProcessors() * 2, Integer.MAX_VALUE, 60);
    subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(SimpleBrokerMessageHandler.class, UserDestinationMessageHandler.class);
    testChannel("brokerChannel", subscriberTypes, 1);
    try {
        this.appContext.getBean("brokerChannelExecutor", ThreadPoolTaskExecutor.class);
        fail("expected exception");
    } catch (NoSuchBeanDefinitionException ex) {
    // expected
    }
    assertNotNull(this.appContext.getBean("webSocketScopeConfigurer", CustomScopeConfigurer.class));
    DirectFieldAccessor subscriptionRegistryAccessor = new DirectFieldAccessor(brokerMessageHandler.getSubscriptionRegistry());
    String pathSeparator = (String) new DirectFieldAccessor(subscriptionRegistryAccessor.getPropertyValue("pathMatcher")).getPropertyValue("pathSeparator");
    assertEquals(".", pathSeparator);
}
Also used : ExceptionWebSocketHandlerDecorator(org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator) LoggingWebSocketHandlerDecorator(org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpUserRegistry(org.springframework.messaging.simp.user.SimpUserRegistry) DefaultSimpUserRegistry(org.springframework.web.socket.messaging.DefaultSimpUserRegistry) HandshakeInterceptor(org.springframework.web.socket.server.HandshakeInterceptor) OriginHandshakeInterceptor(org.springframework.web.socket.server.support.OriginHandshakeInterceptor) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) WebSocketSession(org.springframework.web.socket.WebSocketSession) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) HandshakeHandler(org.springframework.web.socket.server.HandshakeHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) HandlerMapping(org.springframework.web.servlet.HandlerMapping) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) UserDestinationResolver(org.springframework.messaging.simp.user.UserDestinationResolver) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) CustomScopeConfigurer(org.springframework.beans.factory.config.CustomScopeConfigurer) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) SubProtocolHandler(org.springframework.web.socket.messaging.SubProtocolHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) DefaultUserDestinationResolver(org.springframework.messaging.simp.user.DefaultUserDestinationResolver) Test(org.junit.Test)

Aggregations

NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)71 Test (org.junit.Test)27 BeanCreationException (org.springframework.beans.factory.BeanCreationException)18 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)15 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)14 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 HashMap (java.util.HashMap)8 EventBus (org.apache.cloudstack.framework.events.EventBus)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 SimpleDateFormat (java.text.SimpleDateFormat)7 Date (java.util.Date)7 EventBusException (org.apache.cloudstack.framework.events.EventBusException)7 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)7 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)6 Method (java.lang.reflect.Method)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)5 ApplicationContext (org.springframework.context.ApplicationContext)5 ArrayList (java.util.ArrayList)4 Account (com.cloud.user.Account)3