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
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
TestClass.java
Aggregated.javaCode: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; } }
spring-config.xmlCode:package com.spring.ioc; public class Aggregated { }
SpringStart.javaHTML 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>
This example doesn't bring any complexity in comparison with the information mentioned at the 3.11.5. @Resource reference section.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 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?
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.
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.
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.
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.
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?