Spring 注解详细介绍(一)
2011-09-27 13:29
459 查看
使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护。(偶喜欢这个功能,因为偶对xml不感冒)。 以下是一个例子:
先编写接口Man:
publicinterface Man {
public String sayHello();
}
然后写Man的实现类Chinese和American:
@Service
public class Chinese implements Man{
public String sayHello() {
return "I am Chinese!";
}
}
@Service
public class American implements Man{
public String sayHello() {
return "I am American!";
}
}
@Service注释表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,American实例化为american,如果需要自己改名字则:@Service("你自己改的bean名")。
beans.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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-scanbase-package="testspring.main"/>
</beans>
在spring的配置文件里面只需要加上<context:annotation-config/>和<context:component-scanbase-package="需要实现注入的类所在包"/>,可以使用base-package="*"表示全部的类。
编写主类测试:
@Service
public class Main{
@Autowired
@Qualifier("chinese")
private Man man;
public static void main(String[] args) {
// TODO code application logic here
ApplicationContext ctx = newClassPathXmlApplicationContext("beans.xml");
Main main = (Main) ctx.getBean("main");
System.out.println(main.getMan().sayHello());
}
public Man getMan() {
return man;
}
}
在Man接口前面标上@Autowired和@Qualifier注释使得Man接口可以被容器注入,当Man接口存在两个实现类的时候必须指定其中一个来注入,使用实现类首字母小写的字符串来注入。否则可以省略,只写@Autowired
先编写接口Man:
publicinterface Man {
public String sayHello();
}
然后写Man的实现类Chinese和American:
@Service
public class Chinese implements Man{
public String sayHello() {
return "I am Chinese!";
}
}
@Service
public class American implements Man{
public String sayHello() {
return "I am American!";
}
}
@Service注释表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,American实例化为american,如果需要自己改名字则:@Service("你自己改的bean名")。
beans.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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-scanbase-package="testspring.main"/>
</beans>
在spring的配置文件里面只需要加上<context:annotation-config/>和<context:component-scanbase-package="需要实现注入的类所在包"/>,可以使用base-package="*"表示全部的类。
编写主类测试:
@Service
public class Main{
@Autowired
@Qualifier("chinese")
private Man man;
public static void main(String[] args) {
// TODO code application logic here
ApplicationContext ctx = newClassPathXmlApplicationContext("beans.xml");
Main main = (Main) ctx.getBean("main");
System.out.println(main.getMan().sayHello());
}
public Man getMan() {
return man;
}
}
在Man接口前面标上@Autowired和@Qualifier注释使得Man接口可以被容器注入,当Man接口存在两个实现类的时候必须指定其中一个来注入,使用实现类首字母小写的字符串来注入。否则可以省略,只写@Autowired
相关文章推荐
- Spring 4.0新功能:@Conditional注解详细介绍
- Spring 注解详细介绍(二)
- spring 注解 aop 详细介绍
- Spring 注解详细介绍(三)
- Spring注解详细介绍(四)
- spring注解详细介绍
- Spring中常用注解的详细介绍
- Android 中的注解详细介绍
- Hibernate注解详细介绍
- [转]Spring注解原理的详细剖析与实现
- 超详细 Spring @RequestMapping 注解使用技巧
- spring四种依赖注入方式的详细介绍
- Spring事物隔离级别以及事物的七种传播特性详细介绍,以及应用当中需要注意的点
- Spring集成Ibatis详细介绍
- spring的@Transactional注解详细用法
- 详细介绍Spring事务管理
- SpringMVC Controller介绍及常用注解和SpringMVC表单标签简介
- 详解SpringMVC Controller介绍及常用注解
- Myeclipse8.5/SpringSource Tool Suite (STS)安装ExtJs插件(spket插件)方法详细介绍
- Spring Boot 集成 resteasy篇 — jax-rs常用注解介绍