码迷,mamicode.com
首页 > 编程语言 > 详细

[Spring] IOC - Annotation

时间:2014-09-09 18:04:19      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   使用   java   ar   

Spring Annotation使用例子。

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


 

Project结构:

bubuko.com,布布扣


 

配置文件: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);
    }

}

 


 

输出结果:

bubuko.com,布布扣

bubuko.com,布布扣

[Spring] IOC - Annotation

标签:style   blog   http   color   os   io   使用   java   ar   

原文地址:http://www.cnblogs.com/HD/p/3962879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!