Spring Annotation使用例子。

与XML配置的例子一样:http://www.cnblogs.com/HD/p/3962541.html


Project结构:


配置文件:springContext.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:component-scan base-package="com.my" /> </beans>

DAO:

package com.my.dao;

import org.springframework.stereotype.Repository;

@Repository
public abstract class AccountDaoFactory {
/*
* Fin account by id
*/
public abstract Integer findAccount(Integer accountID);
}
package com.my.dao.mysql;

import org.springframework.stereotype.Repository;

@Repository(value="accountDao")
public class AccountDao extends com.my.dao.AccountDaoFactory {
/*
* Find account by account id
*/
@Override
public Integer findAccount(Integer accountID) {
return accountID;
}
}

Service:

package com.my.service;

import javax.annotation.Resource;

import com.my.dao.AccountDaoFactory;

public abstract class AccountServiceFactory {
@Resource(name="accountDao")
protected AccountDaoFactory accountDao; public abstract Integer findAccount(Integer accountID); public AccountDaoFactory getAccountDAO() {
return accountDao;
} public void setAccountDAO(AccountDaoFactory accountDAO) {
this.accountDao = accountDAO;
}
}
package com.my.service.mysql;

import org.springframework.stereotype.Service;

import com.my.service.AccountServiceFactory;

@Service(value="accountService")
public class AccountService extends AccountServiceFactory { @Override
public Integer findAccount(Integer accountID) {
return accountDao.findAccount(accountID);
} }

Controller:

package com.my.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.my.service.AccountServiceFactory;

@Controller
public class SpringController {
@Resource(name="accountService")
private AccountServiceFactory accountService; public AccountServiceFactory getAccount() {
return accountService;
} public void setAccount(AccountServiceFactory account) {
this.accountService = account;
} public Integer findAccount(Integer accountID){
return accountService.findAccount(accountID);
} }

JUnit测试:

package com.my.controller;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringControllerTest {
private SpringController springController;
private ApplicationContext context; @Before
public void setUp() throws Exception {
context = new ClassPathXmlApplicationContext("springContext.xml");
// Use class load
springController = (SpringController)context.getBean(SpringController.class);
} @Test
public void testFindAccount() {
Integer id = 100;
Integer result = springController.findAccount(id);
assertEquals(id, result);
System.out.println(result);
} }

输出结果:

[Spring] IOC - Annotation的更多相关文章

  1. 模仿 spring IOC Annotation版自动装配

    spring 有两大核心 IOC和AOP.  IOC (inversion of control) 译为 控制反转,也可以称为 依赖注入 ; AOP(Aspect Oriented Programmi ...

  2. Spring学习笔记之三----基于Annotation的Spring IOC配置

    使用Annotation 来创建Bean有两种方式 在配置类中创建bean(配置类是指标注为@Configuration的类),在配置类中每一个创建bean的方法都应该标注为@Bean,可以在@Bea ...

  3. spring笔记6 spring IOC的中级知识

    1,spring ioc的整体流程,xml配置 spring ioc初始化的流程结合上图 步骤编号 完成的工作 1 spring容器读取配置文件,解析称注册表 2 根据注册表,找到相应的bean实现类 ...

  4. Java Spring IOC用法

    Java Spring IOC用法 Spring IoC 在前两篇文章中,我们讲了java web环境搭建 和 java web项目搭建,现在看下spring ioc在java中的运用,开发工具为In ...

  5. Spring学习进阶(二)Spring IoC

    在使用Spring所提供的各种丰富而神奇的功能之前,必须在Spring IoC容器中装配好Bean,并建立Bean与Bean之间的关联关系.控制反转(Inverser of Control ioc)是 ...

  6. Spring IOC之基于JAVA的配置

    基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...

  7. Spring IOC之基于注解的容器配置

    Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...

  8. 纯注解快速使用spring IOC容器

    使用spring的ioc容器实现对bean的管理与基本的依赖注入是再经典的应用了.基础使用不在详述. 这里主要介绍下使用注解实现零配置的spring容器.我相信你也会更喜欢使用这种方式.Spring ...

  9. Spring IOC(四)总结

    目录 1.spring容器中Bean生命周期 2.IOC容器中核心接口 3.IOC容器启动流程 4.IOC依赖注入流程 =============正文分割线================== Spr ...

随机推荐

  1. #pragma once与 #ifndef的区别

      为了避免同一个文件被include多次 1   #ifndef方式2   #pragma once方式 在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一些细微的区别.   ...

  2. 利用SVN进行任意文件对比

    都知道SVN可以比较已经上传的文件的内容,看到两个文件有什么不同的地方. 但是有时候并不想上传想要比较的文件,能不能利用SVN这样一个功能去比较别的两个文件呢? 琢磨来琢磨去, 发现只要在资源管理器里 ...

  3. C++ | boost库 类的序列化

    是的,这是今年的情人节,一篇还在研究怎么用的文章,文结的时候应该就用成功了. 恩,要有信心 神奇的分割线 不知何时装过boost库的header-only库, 所以ratslam中的boost是可以编 ...

  4. easyui表单多重验证,动态设置easyui控件

    要实现的功能:在做添加学生信息的时候,利用easyui的验证功能判断 学号是否重复和学号只能为数字 最终效果如下图: 但在做这个的过程中,遇到了一系列的问题: 扩展validatebox的验证方法,最 ...

  5. iOS开发UI篇—在UItableview中实现加载更多功能

    一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据.                      二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...

  6. wp8.1 Study19:通知

    一.通知形式 在windowsphone系统中,通知有很多中形式,如下图 (Tile磁贴在前一博文已复习过,Badge形式与tile类似) 1.Toast 它主要是利用xml文件来编写的,xml代码如 ...

  7. 分享原创powershell脚本小工具ctracert.ps1

    ----------[脚本介绍]----------- 脚本名称:ctracert.ps1软件名称:灰主牛 跟踪路由 归属地版 V1.0脚本作用:1跟踪路由.2显示归属地.(注意不带显示时间功能)脚本 ...

  8. U盘格式化后的恢复

    软件:http://www.easeus.com/datarecoverywizard/free-data-recovery-software.htm

  9. php中json_encode UTF-8中文乱码问题

    最近在接口代码当中用到过json_encode,在网上找到说json_encode编码设置为UTF-8中文就不会乱码,经验证这办法确实是有效果的,但是不知道为什么,代码在用过一段时间之后就不太管用了. ...

  10. linux命令:less

    1.命令介绍: less用来逐页输出文件内容,less相比more功能更加强大,less可以前后翻页,前后搜索. 2.命令格式: less [选项] 文件 3.命令参数: -b <缓冲区大小&g ...