Spring2.5 の@Resource

Spring2.5 のメモを書くのは情けないが、仕事で使わなくてはならないので、不本意でも書きます。

@Resource や、@Autowired を書いてsetterの記述を無くする例。

bean生成のコンフィグXMLで、<context:annotation-config/>を書くことである。

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
">http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <context:annotation-config/>

  <bean id="FooImpl" class="jp.test.service.FooImpl" />

  <bean id="footarget" class="org.springframework.aop.framework.ProxyFactoryBean" >
    <property name="proxyInterfaces">
       <value>jp.test.service.Foo</value>
    </property>
    <property name="target">
       <ref local="FooImpl"/>
    </property>
  </bean>
  <bean id="user" class="jp.test.data.User">
    <property name="name">
       <value>Uranus</value>
    </property>
  </bean>

</beans>
----------------------------------------------

import javax.annotation.Resource;
import jp.test.data.User;

public class FooImpl implements Foo{
   @Resource private User user;

   @Override
   public String action(){
      return this.user.getName();
   }
}
------------------------------------
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

  BeanFactory contxt = new ClassPathXmlApplicationContext("config.xml");

  Foo foo = (Foo)contxt.getBean("footarget");

  String res = foo.action();

  System.out.println(res);