Announcement Announcement Module
Collapse
No announcement yet.
Bean Factory is not set for BeanConfigurerSupport Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean Factory is not set for BeanConfigurerSupport

    In my configuration file I have:

    Code:
    	<context:spring-configured/>
    	
    	<context:annotation-config/>
    	
    	<context:load-time-weaver/>
    
            <bean id="authService" class="server.services.AuthServiceImpl"/>
    And I have this class (I used (at) here instead of the symbol because the forum thinks I'm trying to write a e-mail address...):

    Code:
    (at)Configurable
    public class LoginCommand implements Command {
    		
    	String name;
    	String password;
    	
    	
    	String sessionId;
    	
    	(at)Autowired
    	private transient AuthService authService;
    
            ...
    }
    When the command is deserialized I've get this message:

    Code:
    DEBUG org.springframework.beans.factory.wiring.BeanConfigurerSupport - BeanFactory has not been set on BeanConfigurerSupport: Make sure this configurer runs in a Spring container. Unable to configure bean of type [common.commands.LoginCommand]. Proceeding without injection.
    The result is that authSerivce remains null.

  • #2
    It seems that you didn't provided necessary support for @Configurable to be applied. Check corresponding reference section about that - 6.8. Using AspectJ with Spring applications

    Comment


    • #3
      I tryed this also:

      Code:
      <bean id="loginCommand" class="commom.commands.LoginCommand"
        depends-on="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"
        scope="prototype">
        <property name="authService" ref="authService" />
      </bean>
      together with

      Code:
      (at)Configurable("loginCommand")
      public class LoginCommand implements Command {
      I can't figure it out

      Comment


      • #4
        Am I right assuming that you want the service to be autowired when you create LoginCommand instance via new operator?

        Comment


        • #5
          I wanted to have authService available when the LoginCommand object is deserialized.
          But I guess is the same advice that is running for the readResolve() method.

          Comment


          • #6
            Here is a complete small example that shows autowiring being processed on deserialized objects. Does it represent the same use-case as yours?

            TestClass.java
            Code:
            package com.spring.ioc;
            
            import java.io.Serializable;
            
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.beans.factory.annotation.Configurable;
            
            @Configurable
            public class TestClass implements Serializable {
            
            	private static final long serialVersionUID = 1L;
            
            	private Aggregated field;
            
            	public Aggregated getField() {
            		return field;
            	}
            
            	@Autowired
            	public void setField(Aggregated field) {
            		this.field = field;
            	}
            }
            Aggregated.java
            Code:
            package com.spring.ioc;
            
            public class Aggregated {
            }
            aop.xml
            HTML Code:
            <!DOCTYPE aspectj PUBLIC
                    "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
            <aspectj>
            
                <weaver>
                    <include within="com.spring.ioc.*"/>
                </weaver>
            
                <aspects>
            	    <weaver options="-showWeaveInfo"/>
                </aspects>
            
              </aspectj>
            spring-config.xml
            Code:
            <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:aop="http://www.springframework.org/schema/aop"
                   xmlns:tx="http://www.springframework.org/schema/tx"
                   xmlns:context="http://www.springframework.org/schema/context"
                   xmlns:util="http://www.springframework.org/schema/util"
                   xsi:schemaLocation="
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
            
            	<context:annotation-config/>
            	<context:spring-configured/>
            	<context:load-time-weaver/>
            
            	<bean id="aggregated" class="com.spring.ioc.Aggregated"/>
            
            </beans>
            SpringStart.java
            Code:
            package com.spring;
            
            import java.io.*;
            
            import com.spring.ioc.TestClass;
            import org.springframework.context.ApplicationContext;
            import org.springframework.context.support.ClassPathXmlApplicationContext;
            
            public class SpringStart {
            	public static void main(String[] args) throws Exception {
            		store();
            //		load();
            	}
            
            	private static void store() throws Exception {
            		TestClass bean = new TestClass();
            		FileOutputStream fOut = new FileOutputStream(System.getProperty("user.dir") + "/dat");
            		ObjectOutputStream oOut = new ObjectOutputStream(fOut);
            		oOut.writeObject(bean);
            		oOut.close();
            	}
            
            	private static void load() throws Exception {
            		ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            		ObjectInputStream oIn = new ObjectInputStream(new FileInputStream(System.getProperty("user.dir") + "/dat"));
            		TestClass bean = (TestClass)oIn.readObject();
            		oIn.close();
            		System.out.println(bean.getField());
            	}
            }
            Usage:
            1. Run the SpringStart where store() call is uncommented and load() call is commented at the main() method (serialize the object);
            2. Run the SpringStart where load() call is uncommented and store() call is commented at the main() method (deserialize the object);
            Output shows that the property is successfully woven.

            Comment


            • #7
              I tryed to do the same but it did not work...
              I've got this logged:

              Code:
              [AppClassLoader@1f12c4e] weaveinfo Extending interface set for type 'common.commands.LoginCommand' (LoginCommand.java) to include 'org.springframework.beans.factory.aspectj.ConfigurableObject' (AnnotationBeanConfigurerAspect.aj)
              [AppClassLoader@1f12c4e] weaveinfo Extending interface set for type 'common.commands.LoginCommand' (LoginCommand.java) to include 'org.springframework.beans.factory.aspectj.AbstractInterfaceDrivenDependencyInjectionAspect$ConfigurableDeserializationSupport' (AnnotationBeanConfigurerAspect.aj)
              [AppClassLoader@1f12c4e] weaveinfo Type 'common.commands.LoginCommand' (LoginCommand.java) has intertyped method from 'org.springframework.beans.factory.aspectj.AbstractInterfaceDrivenDependencyInjectionAspect' (AbstractInterfaceDrivenDependencyInjectionAspect.aj:'java.lang.Object org.springframework.beans.factory.aspectj.AbstractInterfaceDrivenDependencyInjectionAspect$ConfigurableDeserializationSupport.readResolve()')
              [AppClassLoader@1f12c4e] weaveinfo Join point 'initialization(void org.springframework.beans.factory.aspectj.ConfigurableObject.<init>())' in Type 'common.commands.LoginCommand' (LoginCommand.java:29) advised by before advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (AbstractDependencyInjectionAspect.aj:78) [with runtime test]
              [AppClassLoader@1f12c4e] weaveinfo Join point 'initialization(void org.springframework.beans.factory.aspectj.ConfigurableObject.<init>())' in Type 'common.commands.LoginCommand' (LoginCommand.java:29) advised by afterReturning advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (AbstractDependencyInjectionAspect.aj:87) [with runtime test]
              [AppClassLoader@1f12c4e] weaveinfo Join point 'initialization(void common.commands.LoginCommand.<init>(java.lang.String, java.lang.String))' in Type 'common.commands.LoginCommand' (LoginCommand.java:29) advised by afterReturning advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (AbstractDependencyInjectionAspect.aj:87) [with runtime test]
              At LoginCommand.java:29 I have the constructor of the class.

              Comment


              • #8
                There is no LoginCommand class usage at my example. Have you tried to start the exact example I posted? If you try it and it works you can analyze the difference between example and your classes and point out the problem reason.

                Comment


                • #9
                  Thanks for the example!

                  It worked, and then my code worked also...

                  I was using
                  Code:
                  -javaagent:path/to/aspectjweaver.jar
                  instead of
                  Code:
                  -javaagent:path/to/spring-agent.jar
                  Why wasn't that good? I'm kind of new at Spring + AOP

                  Comment


                  • #10
                    Originally posted by bogdand2 View Post
                    Thanks for the example!

                    It worked, and then my code worked also...

                    I was using
                    Code:
                    -javaagent:path/to/aspectjweaver.jar
                    instead of
                    Code:
                    -javaagent:path/to/spring-agent.jar
                    Why wasn't that good? I'm kind of new at Spring + AOP

                    You should understand the basic load-time weaving principles. That relates not only to the Spring+AspectJ but to the every used technology. You get an example of The Law of Leaky Abstractions otherwise.
                    • first of all, there is no magic either at Spring or AOP or AspectJ - that's the main principle
                    • java offers standard facility that allows you to extend class loading mechanism - java.lang.instrument. AspectJ just uses that facility to instrument the classes being loaded;
                    • Spring contribution here is that it provides agent that is able to apply the AOP rules to the loaded classes. That agent is located exactly at the spring-agent.jar;

                    Comment


                    • #11
                      Thank you!

                      Comment

                      Working...
                      X