cancel
Showing results for 
Search instead for 
Did you mean: 

spring dependency injection issue : NullPointerException

pmarreddy
Champ in-the-making
Champ in-the-making
hi,

we are exploring alfreco for a while, its a great product, we are considering to provide content services which would have been very difficult with out alfresco.

i have some issues in trying to use spring dependency injection, it would be great if somebody can figure out the solution.

<bean id="initTest" class="org.alfresco.example.inittest" >
<property name="authenticationService">
<ref bean="authenticationService" />
</property>
</bean>
</beans>



i have defined this in application-context.xml in repository and used it in the following way.


package org.alfresco.example;

import org.alfresco.service.cmr.security.AuthenticationService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class inittest {

private AuthenticationService authenticationService;

public void setAuthenticationService(AuthenticationService authenticationService)
{
System.out.println("hi");
this.authenticationService = authenticationService;
}

public void login() throws Exception
{


ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");

authenticationService.authenticate("admin", "admin".toCharArray());
}
}

i am getting

Exception in thread "main" java.lang.NullPointerException
at org.alfresco.example.inittest.login(test1.java:23)
at org.alfresco.example.loggie.main(loggie.java:14)


if i am using getbean it works fine.

adv thanks prasanth
3 REPLIES 3

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

First try putting the loading of the app context into a static member, rather than in the method ….



public static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");


… now the calling code should look something like this …



inittest obj = inittest.applicationContext.getBean("initTest");
obj.login();


Hope this helps,
Roy

pmarreddy
Champ in-the-making
Champ in-the-making
yup it works, thanks.


i just want to conform, does it  work for all situations, i  looked at alfresco code and no where application context is defined in a static way.

ad thanks prasanth.

davidc
Star Contributor
Star Contributor
The initialisation of the Application context depends on the environment you're executing in.  For example, the Web Client uses web.xml to initialise a WebApplicationContext.  Take a look in web.xml (within the source bundle at /projects/web-client/source/web/web-inf) and search for ContextLoaderListener and contextConfigLocation.