首页 > 代码库 > [Spring] IOC - Annotation
[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;@Repositorypublic 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;@Controllerpublic 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。