org.springframework.beans.factory.CannotLoadBeanClassException Java Examples

The following examples show how to use org.springframework.beans.factory.CannotLoadBeanClassException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * When using a BeanFactory. singletons are of course not pre-instantiated.
 * So rubbish class names in bean defs must now not be 'resolved' when the
 * bean def is being parsed, 'cos everything on a bean def is now lazy, but
 * must rather only be picked up when the bean is instantiated.
 */
@Test
public void testClassNotFoundWithDefaultBeanClassLoader() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CLASS_NOT_FOUND_CONTEXT);
	// cool, no errors, so the rubbish class name in the bean def was not resolved
	try {
		// let's resolve the bean definition; must blow up
		factory.getBean("classNotFound");
		fail("Must have thrown a CannotLoadBeanClassException");
	}
	catch (CannotLoadBeanClassException ex) {
		assertTrue(ex.getResourceDescription().contains("classNotFound.xml"));
		assertTrue(ex.getCause() instanceof ClassNotFoundException);
	}
}
 
Example #2
Source File: ThemesService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the theme service
 * 
 * @return the theme service
 * @throws ThemeNotAvailableException
 *             If the theme is not available
 */
private static IThemeService getThemeService( ) throws ThemeNotAvailableException
{
    IThemeService themeService = null;

    if ( !isAvailable( ) )
    {
        throw new ThemeNotAvailableException( );
    }

    try
    {
        themeService = SpringContextService.getBean( BEAN_THEME_SERVICE );
    }
    catch( BeanDefinitionStoreException | NoSuchBeanDefinitionException | CannotLoadBeanClassException e )
    {
        throw new ThemeNotAvailableException( );
    }

    return themeService;
}
 
Example #3
Source File: XmlBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * When using a BeanFactory. singletons are of course not pre-instantiated.
 * So rubbish class names in bean defs must now not be 'resolved' when the
 * bean def is being parsed, 'cos everything on a bean def is now lazy, but
 * must rather only be picked up when the bean is instantiated.
 */
@Test
public void testClassNotFoundWithDefaultBeanClassLoader() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CLASS_NOT_FOUND_CONTEXT);
	// cool, no errors, so the rubbish class name in the bean def was not resolved
	try {
		// let's resolve the bean definition; must blow up
		factory.getBean("classNotFound");
		fail("Must have thrown a CannotLoadBeanClassException");
	}
	catch (CannotLoadBeanClassException ex) {
		assertTrue(ex.getResourceDescription().contains("classNotFound.xml"));
		assertTrue(ex.getCause() instanceof ClassNotFoundException);
	}
}
 
Example #4
Source File: XmlBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * When using a BeanFactory. singletons are of course not pre-instantiated.
 * So rubbish class names in bean defs must now not be 'resolved' when the
 * bean def is being parsed, 'cos everything on a bean def is now lazy, but
 * must rather only be picked up when the bean is instantiated.
 */
@Test
public void testClassNotFoundWithDefaultBeanClassLoader() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(CLASS_NOT_FOUND_CONTEXT);
	// cool, no errors, so the rubbish class name in the bean def was not resolved
	try {
		// let's resolve the bean definition; must blow up
		factory.getBean("classNotFound");
		fail("Must have thrown a CannotLoadBeanClassException");
	}
	catch (CannotLoadBeanClassException ex) {
		assertTrue(ex.getResourceDescription().indexOf("classNotFound.xml") != -1);
		assertTrue(ex.getCause() instanceof ClassNotFoundException);
	}
}
 
Example #5
Source File: AbstractJaxWsServiceExporter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Publish all {@link javax.jws.WebService} annotated beans in the
 * containing BeanFactory.
 * @see #publishEndpoint
 */
public void publishEndpoints() {
	Set<String> beanNames = new LinkedHashSet<String>(this.beanFactory.getBeanDefinitionCount());
	beanNames.addAll(Arrays.asList(this.beanFactory.getBeanDefinitionNames()));
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		beanNames.addAll(Arrays.asList(((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()));
	}
	for (String beanName : beanNames) {
		try {
			Class<?> type = this.beanFactory.getType(beanName);
			if (type != null && !type.isInterface()) {
				WebService wsAnnotation = type.getAnnotation(WebService.class);
				WebServiceProvider wsProviderAnnotation = type.getAnnotation(WebServiceProvider.class);
				if (wsAnnotation != null || wsProviderAnnotation != null) {
					Endpoint endpoint = createEndpoint(this.beanFactory.getBean(beanName));
					if (this.endpointProperties != null) {
						endpoint.setProperties(this.endpointProperties);
					}
					if (this.executor != null) {
						endpoint.setExecutor(this.executor);
					}
					if (wsAnnotation != null) {
						publishEndpoint(endpoint, wsAnnotation);
					}
					else {
						publishEndpoint(endpoint, wsProviderAnnotation);
					}
					this.publishedEndpoints.add(endpoint);
				}
			}
		}
		catch (CannotLoadBeanClassException ex) {
			// ignore beans where the class is not resolvable
		}
	}
}
 
Example #6
Source File: CaptchaSecurityService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Default constructor.
 *
 * Gets the captchaValidator from the captcha plugin. If the validator is missing, sets available to false;
 */
public CaptchaSecurityService( )
{
    try
    {
        // first check if captchaValidator bean is available in the jcaptcha plugin context
        _captchaService = SpringContextService.getBean( "captcha.captchaService" );
        _bAvailable = _captchaService != null;
    }
    catch( CannotLoadBeanClassException | NoSuchBeanDefinitionException | BeanDefinitionStoreException e )
    {
        _bAvailable = false;
    }
}
 
Example #7
Source File: WorkflowService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Private constructor
 */
private WorkflowService( )
{
    try
    {
        _service = SpringContextService
                .getBean( fr.paris.lutece.plugins.workflowcore.service.workflow.WorkflowService.BEAN_SERVICE );
        _provider = SpringContextService.getBean( BEAN_WORKFLOW_PROVIDER );
        _bServiceAvailable = ( _service != null ) && ( _provider != null );
    }
    catch ( CannotLoadBeanClassException | NoSuchBeanDefinitionException | BeanDefinitionStoreException e )
    {
        _bServiceAvailable = false;
    }
}
 
Example #8
Source File: SponsoredLinksSearchService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Default constructor.
 *
 * Gets the sponsoredLinksService from the sponsoredlinks plugin If the service is missing, sets available to false
 */
public SponsoredLinksSearchService( )
{
    try
    {
        // first check if the sponsoredlinks service bean is available
        _sponsoredLinksService = SpringContextService.getBean( "sponsoredlinks.sponsoredLinksService" );
        _bAvailable = _sponsoredLinksService != null;
    }
    catch( BeanDefinitionStoreException | NoSuchBeanDefinitionException | CannotLoadBeanClassException e )
    {
        _bAvailable = false;
    }
}
 
Example #9
Source File: RegularExpressionService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Private constructor
 */
private RegularExpressionService( )
{
    try
    {
        _service = SpringContextService.getBean( "regularExpressionService" );
        _bServiceAvailable = _service != null;
    }
    catch( CannotLoadBeanClassException | NoSuchBeanDefinitionException | BeanDefinitionStoreException e )
    {
        _bServiceAvailable = false;
    }
}
 
Example #10
Source File: AbstractJaxWsServiceExporter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Publish all {@link javax.jws.WebService} annotated beans in the
 * containing BeanFactory.
 * @see #publishEndpoint
 */
public void publishEndpoints() {
	Set<String> beanNames = new LinkedHashSet<String>(this.beanFactory.getBeanDefinitionCount());
	beanNames.addAll(Arrays.asList(this.beanFactory.getBeanDefinitionNames()));
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		beanNames.addAll(Arrays.asList(((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()));
	}
	for (String beanName : beanNames) {
		try {
			Class<?> type = this.beanFactory.getType(beanName);
			if (type != null && !type.isInterface()) {
				WebService wsAnnotation = type.getAnnotation(WebService.class);
				WebServiceProvider wsProviderAnnotation = type.getAnnotation(WebServiceProvider.class);
				if (wsAnnotation != null || wsProviderAnnotation != null) {
					Endpoint endpoint = createEndpoint(this.beanFactory.getBean(beanName));
					if (this.endpointProperties != null) {
						endpoint.setProperties(this.endpointProperties);
					}
					if (this.executor != null) {
						endpoint.setExecutor(this.executor);
					}
					if (wsAnnotation != null) {
						publishEndpoint(endpoint, wsAnnotation);
					}
					else {
						publishEndpoint(endpoint, wsProviderAnnotation);
					}
					this.publishedEndpoints.add(endpoint);
				}
			}
		}
		catch (CannotLoadBeanClassException ex) {
			// ignore beans where the class is not resolvable
		}
	}
}
 
Example #11
Source File: ClassPathXmlApplicationContextTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextWithInvalidLazyClass() {
	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(INVALID_CLASS_CONTEXT, getClass());
	assertTrue(ctx.containsBean("someMessageSource"));
	try {
		ctx.getBean("someMessageSource");
		fail("Should have thrown CannotLoadBeanClassException");
	}
	catch (CannotLoadBeanClassException ex) {
		assertTrue(ex.contains(ClassNotFoundException.class));
	}
	ctx.close();
}
 
Example #12
Source File: MBeanExporter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the actual autodetection process, delegating to an
 * {@code AutodetectCallback} instance to vote on the inclusion of a
 * given bean.
 * @param callback the {@code AutodetectCallback} to use when deciding
 * whether to include a bean or not
 */
private void autodetect(AutodetectCallback callback) {
	Set<String> beanNames = new LinkedHashSet<String>(this.beanFactory.getBeanDefinitionCount());
	beanNames.addAll(Arrays.asList(this.beanFactory.getBeanDefinitionNames()));
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		beanNames.addAll(Arrays.asList(((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()));
	}
	for (String beanName : beanNames) {
		if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
			try {
				Class<?> beanClass = this.beanFactory.getType(beanName);
				if (beanClass != null && callback.include(beanClass, beanName)) {
					boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
					Object beanInstance = (!lazyInit ? this.beanFactory.getBean(beanName) : null);
					if (!ScopedProxyUtils.isScopedTarget(beanName) && !this.beans.containsValue(beanName) &&
							(beanInstance == null ||
									!CollectionUtils.containsInstance(this.beans.values(), beanInstance))) {
						// Not already registered for JMX exposure.
						this.beans.put(beanName, (beanInstance != null ? beanInstance : beanName));
						if (logger.isInfoEnabled()) {
							logger.info("Bean with name '" + beanName + "' has been autodetected for JMX exposure");
						}
					}
					else {
						if (logger.isDebugEnabled()) {
							logger.debug("Bean with name '" + beanName + "' is already registered for JMX exposure");
						}
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (this.allowEagerInit) {
					throw ex;
				}
				// otherwise ignore beans where the class is not resolvable
			}
		}
	}
}
 
Example #13
Source File: MBeanExporter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs the actual autodetection process, delegating to an
 * {@code AutodetectCallback} instance to vote on the inclusion of a
 * given bean.
 * @param callback the {@code AutodetectCallback} to use when deciding
 * whether to include a bean or not
 */
private void autodetect(AutodetectCallback callback) {
	Set<String> beanNames = new LinkedHashSet<String>(this.beanFactory.getBeanDefinitionCount());
	beanNames.addAll(Arrays.asList(this.beanFactory.getBeanDefinitionNames()));
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		beanNames.addAll(Arrays.asList(((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()));
	}
	for (String beanName : beanNames) {
		if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
			try {
				Class<?> beanClass = this.beanFactory.getType(beanName);
				if (beanClass != null && callback.include(beanClass, beanName)) {
					boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
					Object beanInstance = (!lazyInit ? this.beanFactory.getBean(beanName) : null);
					if (!ScopedProxyUtils.isScopedTarget(beanName) && !this.beans.containsValue(beanName) &&
							(beanInstance == null ||
									!CollectionUtils.containsInstance(this.beans.values(), beanInstance))) {
						// Not already registered for JMX exposure.
						this.beans.put(beanName, (beanInstance != null ? beanInstance : beanName));
						if (logger.isInfoEnabled()) {
							logger.info("Bean with name '" + beanName + "' has been autodetected for JMX exposure");
						}
					}
					else {
						if (logger.isDebugEnabled()) {
							logger.debug("Bean with name '" + beanName + "' is already registered for JMX exposure");
						}
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (this.allowEagerInit) {
					throw ex;
				}
				// otherwise ignore beans where the class is not resolvable
			}
		}
	}
}
 
Example #14
Source File: AbstractJaxWsServiceExporter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Publish all {@link javax.jws.WebService} annotated beans in the
 * containing BeanFactory.
 * @see #publishEndpoint
 */
public void publishEndpoints() {
	Assert.state(this.beanFactory != null, "No BeanFactory set");

	Set<String> beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount());
	Collections.addAll(beanNames, this.beanFactory.getBeanDefinitionNames());
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		Collections.addAll(beanNames, ((ConfigurableBeanFactory) this.beanFactory).getSingletonNames());
	}

	for (String beanName : beanNames) {
		try {
			Class<?> type = this.beanFactory.getType(beanName);
			if (type != null && !type.isInterface()) {
				WebService wsAnnotation = type.getAnnotation(WebService.class);
				WebServiceProvider wsProviderAnnotation = type.getAnnotation(WebServiceProvider.class);
				if (wsAnnotation != null || wsProviderAnnotation != null) {
					Endpoint endpoint = createEndpoint(this.beanFactory.getBean(beanName));
					if (this.endpointProperties != null) {
						endpoint.setProperties(this.endpointProperties);
					}
					if (this.executor != null) {
						endpoint.setExecutor(this.executor);
					}
					if (wsAnnotation != null) {
						publishEndpoint(endpoint, wsAnnotation);
					}
					else {
						publishEndpoint(endpoint, wsProviderAnnotation);
					}
					this.publishedEndpoints.add(endpoint);
				}
			}
		}
		catch (CannotLoadBeanClassException ex) {
			// ignore beans where the class is not resolvable
		}
	}
}
 
Example #15
Source File: ClassPathXmlApplicationContextTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testContextWithInvalidLazyClass() {
	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(INVALID_CLASS_CONTEXT, getClass());
	assertTrue(ctx.containsBean("someMessageSource"));
	try {
		ctx.getBean("someMessageSource");
		fail("Should have thrown CannotLoadBeanClassException");
	}
	catch (CannotLoadBeanClassException ex) {
		assertTrue(ex.contains(ClassNotFoundException.class));
	}
	ctx.close();
}
 
Example #16
Source File: AbstractJaxWsServiceExporter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Publish all {@link javax.jws.WebService} annotated beans in the
 * containing BeanFactory.
 * @see #publishEndpoint
 */
public void publishEndpoints() {
	Assert.state(this.beanFactory != null, "No BeanFactory set");

	Set<String> beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount());
	Collections.addAll(beanNames, this.beanFactory.getBeanDefinitionNames());
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		Collections.addAll(beanNames, ((ConfigurableBeanFactory) this.beanFactory).getSingletonNames());
	}

	for (String beanName : beanNames) {
		try {
			Class<?> type = this.beanFactory.getType(beanName);
			if (type != null && !type.isInterface()) {
				WebService wsAnnotation = type.getAnnotation(WebService.class);
				WebServiceProvider wsProviderAnnotation = type.getAnnotation(WebServiceProvider.class);
				if (wsAnnotation != null || wsProviderAnnotation != null) {
					Endpoint endpoint = createEndpoint(this.beanFactory.getBean(beanName));
					if (this.endpointProperties != null) {
						endpoint.setProperties(this.endpointProperties);
					}
					if (this.executor != null) {
						endpoint.setExecutor(this.executor);
					}
					if (wsAnnotation != null) {
						publishEndpoint(endpoint, wsAnnotation);
					}
					else {
						publishEndpoint(endpoint, wsProviderAnnotation);
					}
					this.publishedEndpoints.add(endpoint);
				}
			}
		}
		catch (CannotLoadBeanClassException ex) {
			// ignore beans where the class is not resolvable
		}
	}
}
 
Example #17
Source File: ClassPathXmlApplicationContextTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextWithInvalidLazyClass() {
	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(INVALID_CLASS_CONTEXT, getClass());
	assertTrue(ctx.containsBean("someMessageSource"));
	try {
		ctx.getBean("someMessageSource");
		fail("Should have thrown CannotLoadBeanClassException");
	}
	catch (CannotLoadBeanClassException ex) {
		assertTrue(ex.contains(ClassNotFoundException.class));
	}
	ctx.close();
}
 
Example #18
Source File: MBeanExporter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Performs the actual autodetection process, delegating to an
 * {@code AutodetectCallback} instance to vote on the inclusion of a
 * given bean.
 * @param callback the {@code AutodetectCallback} to use when deciding
 * whether to include a bean or not
 */
private void autodetect(Map<String, Object> beans, AutodetectCallback callback) {
	Assert.state(this.beanFactory != null, "No BeanFactory set");
	Set<String> beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount());
	Collections.addAll(beanNames, this.beanFactory.getBeanDefinitionNames());
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		Collections.addAll(beanNames, ((ConfigurableBeanFactory) this.beanFactory).getSingletonNames());
	}

	for (String beanName : beanNames) {
		if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
			try {
				Class<?> beanClass = this.beanFactory.getType(beanName);
				if (beanClass != null && callback.include(beanClass, beanName)) {
					boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
					Object beanInstance = null;
					if (!lazyInit) {
						beanInstance = this.beanFactory.getBean(beanName);
						if (!beanClass.isInstance(beanInstance)) {
							continue;
						}
					}
					if (!ScopedProxyUtils.isScopedTarget(beanName) && !beans.containsValue(beanName) &&
							(beanInstance == null ||
									!CollectionUtils.containsInstance(beans.values(), beanInstance))) {
						// Not already registered for JMX exposure.
						beans.put(beanName, (beanInstance != null ? beanInstance : beanName));
						if (logger.isDebugEnabled()) {
							logger.debug("Bean with name '" + beanName + "' has been autodetected for JMX exposure");
						}
					}
					else {
						if (logger.isTraceEnabled()) {
							logger.trace("Bean with name '" + beanName + "' is already registered for JMX exposure");
						}
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (this.allowEagerInit) {
					throw ex;
				}
				// otherwise ignore beans where the class is not resolvable
			}
		}
	}
}
 
Example #19
Source File: MBeanExporter.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Performs the actual autodetection process, delegating to an
 * {@code AutodetectCallback} instance to vote on the inclusion of a
 * given bean.
 * @param callback the {@code AutodetectCallback} to use when deciding
 * whether to include a bean or not
 */
private void autodetect(Map<String, Object> beans, AutodetectCallback callback) {
	Assert.state(this.beanFactory != null, "No BeanFactory set");
	Set<String> beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount());
	Collections.addAll(beanNames, this.beanFactory.getBeanDefinitionNames());
	if (this.beanFactory instanceof ConfigurableBeanFactory) {
		Collections.addAll(beanNames, ((ConfigurableBeanFactory) this.beanFactory).getSingletonNames());
	}

	for (String beanName : beanNames) {
		if (!isExcluded(beanName) && !isBeanDefinitionAbstract(this.beanFactory, beanName)) {
			try {
				Class<?> beanClass = this.beanFactory.getType(beanName);
				if (beanClass != null && callback.include(beanClass, beanName)) {
					boolean lazyInit = isBeanDefinitionLazyInit(this.beanFactory, beanName);
					Object beanInstance = null;
					if (!lazyInit) {
						beanInstance = this.beanFactory.getBean(beanName);
						if (!beanClass.isInstance(beanInstance)) {
							continue;
						}
					}
					if (!ScopedProxyUtils.isScopedTarget(beanName) && !beans.containsValue(beanName) &&
							(beanInstance == null ||
									!CollectionUtils.containsInstance(beans.values(), beanInstance))) {
						// Not already registered for JMX exposure.
						beans.put(beanName, (beanInstance != null ? beanInstance : beanName));
						if (logger.isDebugEnabled()) {
							logger.debug("Bean with name '" + beanName + "' has been autodetected for JMX exposure");
						}
					}
					else {
						if (logger.isTraceEnabled()) {
							logger.trace("Bean with name '" + beanName + "' is already registered for JMX exposure");
						}
					}
				}
			}
			catch (CannotLoadBeanClassException ex) {
				if (this.allowEagerInit) {
					throw ex;
				}
				// otherwise ignore beans where the class is not resolvable
			}
		}
	}
}