[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);
    }

}

 


 

输出结果:

posted @ 2014-09-09 16:24  横渡  阅读(538)  评论(0编辑  收藏  举报