Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Example @Resource

  1. #1
    Join Date
    Dec 2006
    Posts
    26

    Default Example @Resource

    Does anyone have an end to end resource for using the @Resource annotation?

    I can not seem to get it to work and I have looked over all the documentation.

    thanks

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    TestClass.java

    Code:
    package com.spring.ioc;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class TestClass {
    
    	private Aggregated field;
    
    	public Aggregated getField() {
    		return field;
    	}
    
    	@Resource(name = "first")
    	public void setField(Aggregated field) {
    		this.field = field;
    	}
    }
    Aggregated.java
    Code:
    package com.spring.ioc;
    
    public class Aggregated {
    }
    spring-config.xml

    HTML 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:component-scan base-package="com.spring.ioc"/>
    
    	<bean id="first" class="com.spring.ioc.Aggregated"/>
    	<bean id="second" class="com.spring.ioc.Aggregated"/>
    
    </beans>
    SpringStart.java

    Code:
    package com.spring;
    
    import com.spring.ioc.TestClass;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringStart {
    	public static void main(String[] args) throws Exception {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    		TestClass bean = (TestClass)context.getBean("testClass");
    		System.out.println(bean.getField() == context.getBean("first"));
    		System.out.println(bean.getField() == context.getBean("second"));
    	}
    }
    This example doesn't bring any complexity in comparison with the information mentioned at the 3.11.5. @Resource reference section.

  3. #3
    Join Date
    Dec 2006
    Posts
    26

    Default Works great - but am having a serious and puzzeling problem

    This seems to work, I have beans created, injected except that my SpringInit.init() function seems to now be stuck in a recursive loop.

    The program does not now follow through normal execution. I have no while/for loops in my program.

    I am simply calling:

    public static void main(String[] args)
    {
    SpringInit.init();
    System.out.println("I never get here - if I put a breakpoint inside the SpringInit.init() it stops there over and over and over again.");
    }

    any ideas here? Could it be something in the xml config?
    Attached Files Attached Files

  4. #4
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Given example can't be compiled because:
    • SpringInit.init() is instance method, hence, it can't be called like SpringInit.init();
    • MyClassOne and MyClassTwo sources are absent, hence, context is not instantiated;


    So, the problem is not reproducible.

  5. #5
    Join Date
    Dec 2006
    Posts
    26

    Default Problems initializing appcontext on first application - lost three days

    SpringInit.init() is static so it can be called like SpringInit.init(). I can upload MyClassOne.java and MyClassTwo.java but they are simple classes like

    public class MyClassOne
    {
    }

    and that's it.

    For some reason the spring init keeps getting recursively called. I'm not sure what is going on. It may have something to do with configuration.

    If you have any more ideas, please let me know. I have spent nearly three full working days trying to get the simplest thing to work.

  6. #6
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by jgooroo View Post
    SpringInit.init() is static so it can be called like SpringInit.init(). I can upload MyClassOne.java and MyClassTwo.java but they are simple classes like

    public class MyClassOne
    {
    }

    and that's it.

    For some reason the spring init keeps getting recursively called. I'm not sure what is going on. It may have something to do with configuration.

    If you have any more ideas, please let me know. I have spent nearly three full working days trying to get the simplest thing to work.
    Given example doesn't reproduce the problem. Everything works fine.

  7. #7
    Join Date
    Dec 2006
    Posts
    26

    Default Attached Example that shows the problem (unsolveable)

    OK, here is a very simple example that shows that SpringInit.init() (static method) keeps getting called over and over recursively and nothing injected and so on.

    I have no idea why this is happening. It is very puzzeling and my indicate that:
    1. There is a bug in the Spring Framework
    2. That #1 is not likely since Spring Ioc is being used by millions of programmers daily and someone would have seen this before.

    It comes complete with pom.xml files so that it can be run and has notes inside. I'm not sure this one can be solved.
    Attached Files Attached Files

  8. #8
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by jgooroo View Post
    OK, here is a very simple example that shows that SpringInit.init() (static method) keeps getting called over and over recursively and nothing injected and so on.

    I have no idea why this is happening. It is very puzzeling and my indicate that:
    1. There is a bug in the Spring Framework
    2. That #1 is not likely since Spring Ioc is being used by millions of programmers daily and someone would have seen this before.

    It comes complete with pom.xml files so that it can be run and has notes inside. I'm not sure this one can be solved.
    Let's resolve the awful problem by steps.

    The first question is - do you expect MyManagerImpl object to be created during context initialization?

  9. #9
    Join Date
    Dec 2006
    Posts
    26

    Default Example @Resource

    Right now I did not expect it to be created at context initialization. If it is being created by the context then I am unaware of what mechanism it is being created.

    The test is creating the object. It would be nice to have the context instantiate the object but I would need control over when it is actually created. I may want other things to happen before this manager is created and the Spring context initialized.

    thanks for all the help and other insights to resolve the problem would be appreciated.

  10. #10
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Ok, so you don't expect object of MyManagerImpl class to be created during context initialization.

    Let me ask you the purpose of @Component presence at the MyManagerImpl class then?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •