Spring 2.5 注解开发的简单例子(@Service)

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

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

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

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

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

  下面是代码的运行结果:

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

  代码:

  beans.xml

<?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/>
   <context:component-scan base-package="org.example"/>
</beans>

  测试类:

import org.example.IMan;
import org.example.SimpleMan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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());
  }
}

  自动探测和注入bean的类:

package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class SimpleMan {
  // 自动注入名称为 Man 的 Bean
  @Autowired(required = false)
  @Qualifier("chineseMan")
  //@Qualifier("usMan")
  private IMan man;  
  /**
   * @return the man
   */
  public IMan getMan() {
    return man;
  }
  /**
   * @param man the man to set
   */
  public void setMan(IMan man) {
    this.man = man;
  }
  public String hello() {
    System.out.println("man.sayHello()=" + man.sayHello());
    return "SimpleMan said: Hi";
  }
}
  
一个接口和两个实现类:
package org.example;
/**
* 抽象的人接口.
* @author BeanSoft
* @version 1.0
*/
public interface IMan {
  /**
   * 打招呼的抽象定义.
   * @return 招呼的内容字符串
   */
  public String sayHello();
}
  
package org.example;
import org.springframework.stereotype.Service;
/**
* 中国人的实现.
* @author BeanSoft
*/
@Service
public class ChineseMan implements IMan {
  public String sayHello() {
    return "抽你丫的";
  }
}
package org.example;
import org.springframework.stereotype.Service;
/**
* @author BeanSoft
* 美国大兵
*/
@Service("usMan")
// 这里定义了一个 id 为 usMan 的 Bean, 标注里面的属性是 bean 的 id
public class EnglishMan implements IMan {
  public String sayHello() {
    return this + " said: Fuck you!";
  }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值