随笔-314  评论-209  文章-0  trackbacks-0

研究了很久新出的 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!";
    }

}

posted on 2009-06-25 15:06 xzc 阅读(311) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: