除了基于 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. java 字符串String操作工具类

    /** * StrKit. */ public class StrKit { /** * 首字母变小写 */ public static String firstCharToLowerCase(Str ...

  2. 在Oracle数据库启动时提示没有权限 ora-01031:insufficient privileges

    环境:Linux 操作语句: [oracle@ora11r2 ~]$ sqlplus /nolog SQL*Plus: Release 11.1.0.6.0 - Production on Thu J ...

  3. Bootstrap栅格系统(布局)

    栅格系统(布局) Bootstrap内置了一套响应式.移动设备优先的流式栅格系统,随着屏幕设备或视口(viewport)尺寸的增加,系统会自动分为最多12列. 我在这里是把Bootstrap中的栅格系 ...

  4. iis 启用父目录路径访问

    今天公司有个客户保修网站后台无法访问,我查看了源代码,发现ASP代码本身并没有什么问题.而且我下到本地能够访问.就是在网上不能正常连接,显示入下错误: Server.MapPath() 错误 'ASP ...

  5. Transaction Manager Maximum Timeout

    TransactionManager.MaximumTimeout是个只读的属性, 默认只有10分钟, 要想修改它必须通过machine.config来修改. 为了单个应用而去修改这个值是不合适的. ...

  6. synchronized(this) 和synchronized(xxx.class)的区别和联系

    synchronized(ThreadTest.class)是对ThreadTest这个类进行加锁,类里面的属性,方法都是同步的,是针对于特定的类的~~ synchronized(this){}是对{ ...

  7. 关于MVC中DropDownListFor的一个bug

    如以下代码: //后台 代码 ViewData["source_type"] = new List<SelectListItem> { "}, "} ...

  8. C# 图片保存到数据库和从数据库读取图片并显示

    图片保存到数据库的方法: public void imgToDB(string sql)        {   //参数sql中要求保存的imge变量名称为@images            //调 ...

  9. HDU 1863 畅通工程

    畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  10. sqoop的安装与使用

    1.什么是Sqoop Sqoop即 SQL to Hadoop ,是一款方便的在传统型数据库与Hadoop之间进行数据迁移的工具.充分利用MapReduce并行特点以批处理的方式加快传输数据.发展至今 ...