spring注解或依赖注入模拟MVC结构案例<五>

来源:互联网 发布:魔女之泉3 知乎 编辑:程序博客网 时间:2024/05/16 18:42

问题?spring注解或依赖注入模拟MVC结构案例


一、首先是扫描注解解析器的方式来模拟MVC结构

(1)最常用的扫描解析器


工具类(只是将气动阀spring容器给抽取出来,作为工具类用,避免代码的重复性):

package cn.itcast.sh.spring.util;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringUtil {public static ApplicationContext applicationContext;static{applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");}}


action层

package cn.itcast.sh.spring.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Component;@Component("personaction")public class PersonAction {@Resource(name="personservice")private PersonService personService;public PersonService getPersonService() {return personService;}public void setPersonService(PersonService personService) {this.personService = personService;}public void savePerson1(){this.personService.savePerson();}}

service层

package cn.itcast.sh.spring.mvc;public interface PersonService {public void savePerson();}

package cn.itcast.sh.spring.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Component;@Component("personservice")public class PersonServiceImp implements PersonService {@Resource(name="persondao")private PersonDao personDao;public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public void savePerson() {this.personDao.savePerson();}}

dao层

package cn.itcast.sh.spring.mvc;public interface PersonDao {public void savePerson();}

package cn.itcast.sh.spring.mvc;import org.springframework.stereotype.Component;@Component("persondao")public class PersonDaoImp implements PersonDao {public void savePerson() {System.out.println("sava Person Dao!!");}}

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">                                 <!-- 扫描注解解析器来模拟MVC结构 -->         <context:component-scan base-package="cn.itcast.sh.spring.mvc"></context:component-scan>             </beans>


测试类:

package cn.itcast.sh.spring.ioc.test;import org.junit.Test;import cn.itcast.sh.spring.mvc.PersonAction;import cn.itcast.sh.spring.util.SpringUtil;public class Mvc_Annotation_Test extends SpringUtil {@Testpublic void A(){PersonAction personAction = (PersonAction)applicationContext.getBean("personaction");personAction.savePerson1();}}


(2)不是很常用的扫描解析器模拟MVC结构:就是将上一个例子的@Component更加细化了


      定义:Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说, 你能用@Component来注解你的组件类, 但如果用@Repository、@Service 或@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联


案例:

action层

package cn.itcast.sh.spring.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;//@Component("personaction")@Controller("personaction")public class PersonAction {@Resource(name="personservice")private PersonService personService;public void savePerson1(){this.personService.savePerson();}}

service层

package cn.itcast.sh.spring.mvc;public interface PersonService {public void savePerson();}

package cn.itcast.sh.spring.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;//@Component("personservice")@Service("personservice")public class PersonServiceImp implements PersonService {@Resource(name="persondao")private PersonDao personDao;public void savePerson() {this.personDao.savePerson();}}

dao层

package cn.itcast.sh.spring.mvc;public interface PersonDao {public void savePerson();}

package cn.itcast.sh.spring.mvc;import org.springframework.stereotype.Component;import org.springframework.stereotype.Repository;//@Component("persondao")@Repository("persondao")public class PersonDaoImp implements PersonDao {public void savePerson() {System.out.println("sava Person Dao!!");}}

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">                                            <!--二、 扫描注解解析器来模拟MVC结构 -->          <context:component-scan base-package="cn.itcast.sh.spring.mvc"></context:component-scan>                                 </beans>

测试类

package cn.itcast.sh.spring.ioc.test;import org.junit.Test;import cn.itcast.sh.spring.mvc.PersonAction;import cn.itcast.sh.spring.util.SpringUtil;public class Mvc_Annotation_Test extends SpringUtil {@Testpublic void A(){PersonAction personAction = (PersonAction)applicationContext.getBean("personaction");personAction.savePerson1();}}







二、注解解析器方式模拟MVC结构

action层

package cn.itcast.sh.spring.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Component;//@Component("personaction")public class PersonAction {@Resource(name="personservice")private PersonService personService;public void savePerson1(){this.personService.savePerson();}}

service层

package cn.itcast.sh.spring.mvc;public interface PersonService {public void savePerson();}

package cn.itcast.sh.spring.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Component;//@Component("personservice")public class PersonServiceImp implements PersonService {@Resource(name="persondao")private PersonDao personDao;public void savePerson() {this.personDao.savePerson();}}

dao层

package cn.itcast.sh.spring.mvc;public interface PersonDao {public void savePerson();}

package cn.itcast.sh.spring.mvc;import org.springframework.stereotype.Component;//@Component("persondao")public class PersonDaoImp implements PersonDao {public void savePerson() {System.out.println("sava Person Dao!!");}}

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">                            <!-- 注解解析器模拟MVC结构           注解解析器能够加注解解析器的只能是引用类型的,不能是基本类型的 --><context:annotation-config></context:annotation-config><bean id="personAction" class="cn.itcast.sh.spring.mvc.PersonAction"></bean><bean id="personservice" class="cn.itcast.sh.spring.mvc.PersonServiceImp"></bean><bean id="persondao" class="cn.itcast.sh.spring.mvc.PersonDaoImp"></bean>           </beans>

测试类

package cn.itcast.sh.spring.ioc.test;import org.junit.Test;import cn.itcast.sh.spring.mvc.PersonAction;import cn.itcast.sh.spring.util.SpringUtil;public class MVCTest extends SpringUtil {@Testpublic void A(){PersonAction personAction = (PersonAction)applicationContext.getBean("personAction");personAction.savePerson1();}}




三、依赖注入的方式模拟MVC结构

action层

package cn.itcast.sh.spring.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Component;//@Component("personaction")public class PersonAction {//@Resource(name="personservice")private PersonService personService;public PersonService getPersonService() {return personService;}public void setPersonService(PersonService personService) {this.personService = personService;}public void savePerson1(){this.personService.savePerson();}}

service层

package cn.itcast.sh.spring.mvc;public interface PersonService {public void savePerson();}

package cn.itcast.sh.spring.mvc;import javax.annotation.Resource;import org.springframework.stereotype.Component;//@Component("personservice")public class PersonServiceImp implements PersonService {//@Resource(name="persondao")private PersonDao personDao;public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public void savePerson() {this.personDao.savePerson();}}

dao层

package cn.itcast.sh.spring.mvc;public interface PersonDao {public void savePerson();}

package cn.itcast.sh.spring.mvc;import org.springframework.stereotype.Component;//@Component("persondao")public class PersonDaoImp implements PersonDao {public void savePerson() {System.out.println("sava Person Dao!!");}}

applicatonContext.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"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">                      <!-- Spring模拟MVC -->           <bean id="personDao" class="cn.itcast.sh.spring.mvc.PersonDaoImp"></bean>                      <bean id="personService" class="cn.itcast.sh.spring.mvc.PersonServiceImp">           <property name="personDao">           <ref bean="personDao"/>           </property>           </bean>                      <bean id="personAction" class="cn.itcast.sh.spring.mvc.PersonAction">             <property name="personService">             <ref bean="personService"/>             </property>           </bean>                                 </beans>

测试类:

package cn.itcast.sh.spring.ioc.test;import org.junit.Test;import cn.itcast.sh.spring.mvc.PersonAction;import cn.itcast.sh.spring.util.SpringUtil;public class MVCTest extends SpringUtil {@Testpublic void A(){PersonAction personAction = (PersonAction)applicationContext.getBean("personAction");personAction.savePerson1();}}



到这里两个方法来模拟MVC ,其实注解和依赖注入都是一样的。

0 0
原创粉丝点击