cancel
Showing results for 
Search instead for 
Did you mean: 

spring dependency injection issue

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)
2 REPLIES 2

andy
Champ on-the-rise
Champ on-the-rise
Hi

If you are running a test you need to build the ApplicationContext once and only once, This can be done using BaseSpring test or your own test that builds a static context.

In your example, you get an application context everytime you login in and not untill you call login.

If you are wiring into the alfresco repository in the configuration files then you never need to make an ApplicationContext - that is done as you start tomcat or JBoss.

Regards

Andy

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

actually myproblem is it is giving me nullpointer excpetion if i am using spring dependency injection as specified above. if i am using get bean it is working fine.

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


adv thanks prasanth