Spring 2.5 标注开发的简单例子 </p>

</span></span>

2007-11-30 刘长炯 </p>

</span></span>

</p>

 

</span></span>

  </p>

</span>

研究了很久新出的 Spring 2.5, 总算大致明白了如何用标注定义 Bean, 但是如何定义和注入类型为 java.lang.String bean 仍然未解决, 希望得到高人帮助. </p>

</span>

总的来看 Java EE 5 的标注开发方式开来是得到了大家的认可了. </p>

</span>

@Service 相当于定义 bean, 自动根据 bean 的类名生成一个首字母小写的 bean </p>

</span>

@Autowired 则是自动注入依赖的类, 它会在类路径中找成员对应的类/接口的实现类, 如果找到多个, 需要用 @Qualifier("chineseMan") 来指定对应的 bean ID. </p>

</span>

一定程度上大大简化了代码的编写, 例如一对一的 bean 映射现在完全不需要写任何额外的 bean 定义了. </p>

</span>

下面是代码的运行结果: </p>

</span>

man.sayHello()=抽你丫的
SimpleMan said: Hi
org.example.EnglishMan@12bcd4b said: Fuck you! </p>

</span>

  </p>

</span>

代码: </p>

</span>

beans.xml </p>

</span>

<?xml version="1.0" encoding="UTF-8"?> </p>

<beans xmlns="http://www.springframework.org/schema/beans" </p>

</span>

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" </p>

</span>

       xmlns:context="http://www.springframework.org/schema/context" </p>

</span>

       xsi:schemaLocation="http://www.springframework.org/schema/beans </p>

</span>

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd </p>

</span>

           http://www.springframework.org/schema/context </p>

</span>

           http://www.springframework.org/schema/context/spring-context-2.5.xsd"> </p>

</span>

     <context:annotation-config/> </p>

</span>

     <context:component-scan base-package="org.example"/> </p>

</span>

     </p>

</span>

</beans> </p>

</span>

测试类: </p>

</span>

import org.example.IMan;
import org.example.SimpleMan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; </p>

</span>

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        SimpleMan dao = (SimpleMan) ctx.getBean("simpleMan");
        System.out.println(dao.hello());
        IMan man = (IMan) ctx.getBean("usMan");
        System.out.println(man.sayHello());
    }
} </p>

</span>

自动探测和注入bean的类: </p>

</span>

package org.example; </p>

</span>

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; </p>

</span>

@Service
public class SimpleMan {
    //
自动注入名称为 Man Bean
    @Autowired(required = false)
    @Qualifier("chineseMan")
    //@Qualifier("usMan")
    private IMan man;    </p>

</span>

    /**
     * @return the man
     */
    public IMan getMan() {
        return man;
    } </p>

</span>

    /**
     * @param man the man to set
     */
    public void setMan(IMan man) {
        this.man = man;
    } </p>

</span>

    public String hello() {
        System.out.println("man.sayHello()=" + man.sayHello());
        return "SimpleMan said: Hi";
    }
} </p>

</span>

  </p>

</span>

一个接口和两个实现类: </p>

</span>

package org.example; </p>

</span>

/**
*
抽象的人接口.
* @author BeanSoft
* @version 1.0
*/
public interface IMan {
    /**
     *
打招呼的抽象定义.
     * @return
招呼的内容字符串
     */
    public String sayHello();
} </p>

</span>

  </p>

</span>

package org.example; </p>

</span>

import org.springframework.stereotype.Service; </p>

</span>

/**
*
中国人的实现.
* @author BeanSoft
*/
@Service
public class ChineseMan implements IMan { </p>

</span>

    public String sayHello() {
        return "
抽你丫的";
    } </p>

</span>

} </p>

</span>

  </p>

</span>

package org.example; </p>

</span>

import org.springframework.stereotype.Service; </p>

</span>

/**
* @author BeanSoft
*
美国大兵
*/
@Service("usMan")
//
这里定义了一个 id usMan Bean, 标注里面的属性是 bean id
public class EnglishMan implements IMan { </p>

</span>

    public String sayHello() {
        return this + " said: Fuck you!";
    } </p>

</span>

} </p>

</span>

</p>

 

</span>

转载请注明:WebLogic Android 博客 » Spring 2.5 标注开发的简单例子