Spring 3.0注解开发的简单例子(@Service)
2011-01-12 17:50
525 查看
研究了很久新出的 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!";
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nini1109/archive/2009/06/17/4277588.aspx
总的来看 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!";
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nini1109/archive/2009/06/17/4277588.aspx
相关文章推荐
- Spring开发 - 通过实现ApplicationContextAware在Servlet中调用注解的Service
- Spring注解开发 作用区别@Component、@Repository、@Service、@Controller
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
- struts2 整合 Spring开发,简单数据添加小例子
- Eclipse + MyEclipse整合Struts+Spring+Hibernate简单例子开发
- Spring第六讲学习笔记,使用注解开发。Repository,Service,Controller
- spring使用注解的简单例子
- spring注解开发中常用注解以及简单配置
- 一个简单的Spring定时器例子 注解方式
- 5、Cocos2dx 3.0小游戏开发的例子寻找测试三个简单的介绍和总结
- 【Spring】使用@Service注解写的一个小例子
- 使用注解springmvc+spring+mybatis一个简单ajax例子
- Spring3.0学习-注解模式下的Spring3+hibernate3+struts2-事务管理(xml模式和注解模式的事务注入+@Repository@Service@Transactional)
- spring注解装配bean简单例子
- spring注解 -----最简单的注解与自动装配例子
- Spring开发 - 通过实现ApplicationContextAware在Servlet中调用注解的Service
- Swagger(一) SpringBoot整合Swagger2简单的例子
- 使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)
- 使用Multiplayer Networking做一个简单的多人游戏例子-3/3(Unity3D开发之二十七)
- gxt CURD ,数据库增删改查简单例子(Spring + Hibernate)