[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 ...
随机推荐
- 【转】PDF电子书分享
http://www.voidcn.com/blog/u013830841/article/p-4343018.html http://www.voidcn.com/blog/u013830841/a ...
- Find a point on a 'line' between two Vector3
Find a point on a 'line' between two Vector3http://forum.unity3d.com/threads/find-a-point-on-a-line- ...
- [转]为什么不能用memcached存储Session
以下内容转自:http://www.infoq.com/cn/news/2015/01/memcached-store-session -------------------------分割线---- ...
- 常用的获取时间差的sql语句
"select count(*) from [注册] where datediff(day,time,getdate())<1";//获取当天注册人员数 sql=" ...
- WP8.1 Study18:动态磁贴
一.前言 动态磁贴在WindowsPhone8.1和Windows8.1都是其特色,有人喜欢有人讨厌,不过我觉得还是挺好的,可以让使用者很快知道App内的内容和吸引使用者打开App.下面来学习下怎样添 ...
- Android - 动态调整ListView高度
布局中,如果设计ListView的高度为包裹内容,那么ListView的高度是随着它的子条目的数量的变化而改变的, 这就可能会导致ListView下面的一些控件的位置也会随着ListView的高度的变 ...
- 转载:ODS简介
什么是ODS? 信息处理的多层次要求导致了一种新的数据环境——DB-DW的中间层ODS(操作型数据存储)的出现.ODS是“面向主题的.集成的.当前或接近当前的.不断变化的”数据.通过统一规划,规范框架 ...
- pull刷新
package com.example.mylist; import java.util.ArrayList; import java.util.List; import com.example.ad ...
- 绑定hosts
测试过程中需绑定hosts.将测试环境IP绑定域名,输入域名即可连接到测试环境. 1 hosts文件地址:C:\WINDOWS\system32\drivers\etc 2 用记事本打开hosts ...
- Java中的泛型
1:泛型(掌握) (1)泛型概述 是一种把明确类型的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型. (2)格式: <数据类型> 注意:该数据类型只能是引用类型. (3)好处: ...