12_注解04_注解實現Action調用Service,Service調用Dao的過程


【工程截圖】

 

【PersonDao.java】

package com.HigginCui.annotation;

public interface PersonDao {
    public void savePerson();
}

【PersonDaoImpl.java】

package com.HigginCui.annotation;

import org.springframework.stereotype.Repository;
/*
 * @Repository("personDao")相當於
 * <bean id="personDao" class="......PersonDaoImpl"></bean>
 */
@Repository("personDao")
public class PersonDaoImpl implements PersonDao{
    public void savePerson() {
        System.out.println("save Person...");
    }
}

【PersonService.java】

package com.HigginCui.annotation;

public interface PersonService {
    public void savePerson();
}

【PersonServiceImpl.java】

package com.HigginCui.annotation;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service("personService") public class PersonServiceImpl implements PersonService{
    @Resource(name="personDao")
    private PersonDao personDao;
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    @Override
    public void savePerson() {
        this.personDao.savePerson();
    } 
}

【PersonAction.java】

package com.HigginCui.annotation;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;

@Controller("personAction")
public class PersonAction {
    @Resource(name="personService")
    private PersonService personService;

    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }
    
    public void savePerson(){
        this.personService.savePerson();
    }
}

【applicationContext.xml】

<?xml version= "1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <!-- 把一個類放入到Spring容器中,該類就是一個component,此時不需要聲明對應的bean -->
    <context:component-scan base-package="com.HigginCui.annotation"></context:component-scan>
</beans>

【testPerson.java】

save Person...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM