除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置。Spring 提供以下介个 Annotation 来标注 Spring Bean:
  @Component:标注一个普通的 Spring Bean
  @Controller:标注一个控制器组件类
  @Service:标注一个业务逻辑组件类
  @Repository:标注一个 DAO 组件类

基于 Annotation 配置的示例

DAO 组件以 @Repository 标注:

public interface UserDao {
public User getUserByUsername(String username);
}

@Repository("userDao")
public class UserDaoImpl implements UserDao { List<User> users = new ArrayList<User>(); public UserDaoImpl() {
users.add(new User(1001, "huey", "123"));
users.add(new User(1002, "tmac", "abc"));
users.add(new User(1003, "suer", "xxx"));
} public User getUserByUsername(String username) {
for (User user : users) {
if (username.equals(user.getUsername())) {
return user;
}
}
return null;
} }

业务逻辑组件以 @Service 标注:

public interface UserServ {
public User queryUserByUsername(String username);
}

@Service("userServ")
public class UserServImpl implements UserServ { @Resource(name="userDao")
private UserDao userDao; public User queryUserByUsername(String username) {
return userDao.getUserByUsername(username);
} }

Spring 配置文件,无需配置 Bean,但须配置 <context:component-scan/>:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 自动扫描指定包及其子包下的所有 Bean 类 -->
<context:component-scan base-package="com.huey.dream" />
</beans>

测试方法:

@Test
public void testAnnotation() throws Exception {
ApplicationContext appCtx =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserServ userServ = appCtx.getBean("userServ", UserServ.class); String username = "huey";
User user = userServ.queryUserByUsername(username);
System.out.println(user);
}

Spring(3.2.3) - Beans(8): 基于 Annotation 的配置的更多相关文章

  1. Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制

    Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...

  2. Spring - IoC(8): 基于 Annotation 的配置

    除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置.Spring 提供以下介个 Annotation 来标注 Spring Bean: @Component:标注一个普 ...

  3. Spring使用AspectJ开发AOP:基于Annotation

    基于 Annotation 的声明式 在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关的配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维 ...

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring声明式事务管理(基于Annotation注解方式实现)

    在 Spring 中,除了使用基于 XML 的方式可以实现声明式事务管理以外,还可以通过 Annotation 注解的方式实现声明式事务管理. 使用 Annotation 的方式非常简单,只需要在项目 ...

  5. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  6. Hibernate基于注解annotation的配置

    Annotation在框架中是越来越受欢迎了,因为annotation的配置比起XML的配置来说方便了很多,不需要大量的XML来书写,方便简单了很多,只要几个annotation的配置,就可以完成我们 ...

  7. 基于 Annotation 拦截的 Spring AOP 权限验证方法

    基于 Annotation 拦截的 Spring AOP 权限验证方法 转自:http://www.ibm.com/developerworks/cn/java/j-lo-springaopfilte ...

  8. 【学习】Spring 的 AOP :基于Annotation 的“零配置”方式

    转自:http://www.cnblogs.com/jbelial/archive/2012/07/20/2539123.html AOP(Aspect Orient Programming ) , ...

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

随机推荐

  1. ucGUI例程收藏

    ucGUI 几个重要例程Demo   按钮的定制 #include <stddef.h> #include <string.h> #include "WM.h&quo ...

  2. springMVC部署

      一.导入springMVC所需要的jar包   下载地址:http://repo.spring.io/release/org/springframework/spring/   二.springM ...

  3. 图片中的Exif信息 的ExifDirectory的大部份常量

    #define FMT_BYTE        1           //Format Byte ////////////////////////////////////////////////// ...

  4. python常用字符串操作

    #!/usr/bin/env python name='cunzhang' print(name.capitalize())#首字母大写 print(name.count('n'))#统计字符有几个 ...

  5. 推荐一个CodeProject上的SlideForm控件

    CodeProject有一篇文章介绍了怎么实现一个SlideForm,非常不错,收藏在此. http://www.codeproject.com/KB/dialog/csslideform.aspx ...

  6. 【转】Rails 3.1错误-Could not find a JavaScript runtime及execjs和therubyracer介绍

    转自:http://rubyer.me/blog/740/ Rails 3.1错误 /gems/execjs-1.1.2/lib/ execjs/runtimes.rb:43:in `autodete ...

  7. NAT的全然分析及其UDP穿透的全然解决方式

    NAT的全然分析及其UDP穿透的全然解决方式   一:基本术语 防火墙 防火墙限制了私网与公网的通信,它主要是将(防火墙)觉得未经授权的的包丢弃,防火墙仅仅是检验包的数据,并不改动数据包中的IP地址和 ...

  8. mvc4 web-api 与unity搭建接口

    对于接口重要的应该是 功能实现,合法性验证,性能监控,日志等模块 通过unity aop功能可以实现统一的日志模块和性能监控. 1.新建mvc4 webapi项目 nuget添加 unity 3.0+ ...

  9. IE8不显示字体图标

    bootstrap流行,随着自带的字体图标也火起来了.美丽的字体系统中没有.制作成字体文件,下载到本地.浏览美丽的网页哦. 在项目中遇到有些IE8显示不了,原因是IE8下设置了禁止字体下载

  10. delphi Caption 垂直显示标签文本

    //垂直显示标签文本 Label1.Caption:='垂'+#13+'直'+#13+'显'+#13+'示';//使用相同的方法,我们也可以制作垂直显示的提示(Hint):button1.Hint:= ...