[Spring] IOC - Annotation
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的更多相关文章
- 模仿 spring IOC Annotation版自动装配
spring 有两大核心 IOC和AOP. IOC (inversion of control) 译为 控制反转,也可以称为 依赖注入 ; AOP(Aspect Oriented Programmi ...
- Spring学习笔记之三----基于Annotation的Spring IOC配置
使用Annotation 来创建Bean有两种方式 在配置类中创建bean(配置类是指标注为@Configuration的类),在配置类中每一个创建bean的方法都应该标注为@Bean,可以在@Bea ...
- spring笔记6 spring IOC的中级知识
1,spring ioc的整体流程,xml配置 spring ioc初始化的流程结合上图 步骤编号 完成的工作 1 spring容器读取配置文件,解析称注册表 2 根据注册表,找到相应的bean实现类 ...
- Java Spring IOC用法
Java Spring IOC用法 Spring IoC 在前两篇文章中,我们讲了java web环境搭建 和 java web项目搭建,现在看下spring ioc在java中的运用,开发工具为In ...
- Spring学习进阶(二)Spring IoC
在使用Spring所提供的各种丰富而神奇的功能之前,必须在Spring IoC容器中装配好Bean,并建立Bean与Bean之间的关联关系.控制反转(Inverser of Control ioc)是 ...
- Spring IOC之基于JAVA的配置
基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...
- Spring IOC之基于注解的容器配置
Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...
- 纯注解快速使用spring IOC容器
使用spring的ioc容器实现对bean的管理与基本的依赖注入是再经典的应用了.基础使用不在详述. 这里主要介绍下使用注解实现零配置的spring容器.我相信你也会更喜欢使用这种方式.Spring ...
- Spring IOC(四)总结
目录 1.spring容器中Bean生命周期 2.IOC容器中核心接口 3.IOC容器启动流程 4.IOC依赖注入流程 =============正文分割线================== Spr ...
随机推荐
- PHP二次开发discuz3.2最新体验
康盛官方于6月4号发布了discuz3.2的正式版,因为这两天一直忙于一个项目,一直没来的及体验,现在抽时间总算是装上了,也体验一把. 根据官方说明:Discuz! X3.2 在继承和完善 Discu ...
- MVC 与 webform比较
来自:http://www.cnblogs.com/xiaozhi_5638/p/4019065.html ASP.NET Webforms Behind Code的好处和存在的问题 ASP.NET ...
- SSM框架学习之高并发秒杀业务--笔记3-- Service层
上一节中已经包DAO层编写完成了,所谓的DAO层就是所有和数据访问的部分都应该放在这个层里,它负责与数据库打交道.对于一个web项目来说,大概由这几部分组成: 1. 前台的显示层. 2. 分发处理请求 ...
- HTTP 错误 500.23 - Internal Server Error 解决方法
分析原因:在安装完成后IIS已经支持ASP和ASP.NET 2.0,需要注意的是.NET站点的应用程序池应选用Classic .NET AppPool,而不能用默认的DefaultAppPool,否则 ...
- 做办公用品、文具方面的 B2C 是否有前景呢?
企乐买现在正在做这方面的事,从市场角度来说需求是有的,客单价和重复购买率都还可以,但是也存在几个致命问题使得施展不开举步维艰: 1.中国特有的市场环境:在美国企业办公用品一般都是网上采购,避免灰色的东 ...
- python 从文件导入分类
# -*- coding:utf-8 -*- """ 从文件导入分类 根据行首制表符或空格确定层级关系(4个空格等于一个制表符 同一行制表符和空格不能混用 ) 必须是 u ...
- 【转载】VMware虚拟机修改硬盘容量大小
很多人在安装虚拟机系统的时候,为了节省硬盘空间,把硬盘容量设置得较小,可是后来发现硬盘容量不够用了.在VMware中又不能直接修改虚拟机的硬盘容量大小,或者重建虚拟机系统,非常麻烦. 其实在VMwar ...
- 制造业如何基于BPM做供应链管理?
公司介绍深圳市吉祥腾达科技有限公司是中国网络产业的开航者,是中国无线网络领域的首批开拓者之一.历经10年的开拓创新,已经形成了拥有自主研发的全面产品线. 为了使公司物流.资金流和信息流实现优化整合,腾 ...
- GPS模块启动模式说明
1.启动方式 GPS开机定位分为冷启动、温启动和热启动三种: 1、冷启动:以下几种情况开机均属冷启动。初次使用时;电池耗尽导致星历信息丢失时;关机状态下将接收机移动1000公里以上距离。 2、温 ...
- 在 C++ 代码中使用 UE4 插件---Using a plugin in C++ code
例如使用 CustomMeshComponent 插件 在Build.cs 文件 中 添加以下两行代码 如图可配置路径,可解决#include "CustomMeshComponent&qu ...