cancel
Showing results for 
Search instead for 
Did you mean: 

Sping ApplicationContext

dominict
Champ in-the-making
Champ in-the-making
Has anyone had problems obtaining a custom bean from a Web Service:

This works:

        ApplicationContext appCtx = null;
        HttpServletRequest request = (HttpServletRequest)MessageContext.getCurrentContext().getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
        ServletContext sc = request.getSession().getServletContext();
        WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        appCtx = wc;
        CheckinService checkinSrv = (CheckinService)ctx.getBean("CustomCheckinService");

However, trying to obtain the bean using ClassPathXmlApplicationContext fails in various ways.

If I load the custom bean definition file itself I get the folllowing error:

code:

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("alfresco/extension/custom-content-services-context.xml");
CheckinService checkinSrv = (CheckinService)ctx.getBean("CustomCheckinService");

error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomCheckinService' defined in class path resource [alfresco/extension/custom-content-services-context.xml]: Can't resolve reference to bean 'ServiceRegistry' while setting property 'constructor argument'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ServiceRegistry' is defined


OR

Code:

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("alfresco/application-context.xml");
        CheckinService checkinSrv = (CheckinService)ctx.getBean("CustomCheckinService");

Error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ftsIndexerTrigger' defined in class path resource [alfresco/scheduled-jobs-context.xml]: Initialization of bean failed; nested exception is org.quartz.ObjectAlreadyExistsException: Unable to store Job with name: 'ftsIndexerJobDetail' and group: 'DEFAULT', because one already exists with this identification.


All wiki examples (on wiki.alfresco.com) show obtaining a custom bean via the ClassPathXmlApplicationContent (http://wiki.alfresco.com/wiki/Introducing_the_Alfresco_Java_Content_Repository_API), but it doesn't work (as shown above).  What is the correct way of obtaining a custom bean?

Thanks Dom
1 REPLY 1

derek
Star Contributor
Star Contributor
Hi,

The one that works is the one to use.  When you create an ApplicationContext in Spring, it initializes all the beans again, thereby bringing up a second instance of the Alfresco repository within the same VM - bad.

And you can't just load a single xml file as a new ApplicationContext as it doesn't have access to all the other beans.

You have to get the context from the application - it should have pulled your custom xml files in.

Regards