星空网 > 软件开发 > Java

Spring 一二事(8)

1 <!-- 2       component:把一个类放入到spring容器中,该类就是一个component3       在base-package指定的包及子包下扫描所有的类4   -->5   <context:component-scan base-package="com.lee.spring012.scan.mvc.annotation"></context:component-scan>

IStuDAO.java

1 package com.lee.spring012.scan.mvc.annotation;2 3 public interface IStuDAO {4   public void saveStu();5 }

IStuService.java

1 package com.lee.spring012.scan.mvc.annotation;2 3 public interface IStuService {4   public void saveStu();5 }

PersonAction.java

 1 package com.lee.spring012.scan.mvc.annotation; 2  3 import javax.annotation.Resource; 4  5 import org.springframework.context.annotation.Scope; 6 import org.springframework.stereotype.Controller; 7  8 @Controller 9 @Scope("prototype")    // action为多例10 public class PersonAction {11 12   @Resource13   public IStuService stuServiceImpl;14 15   public void displaySave() {16     System.out.println("mvc action: saving stu...");17     stuServiceImpl.saveStu();18   }19 20 }

StuDAOImpl.java

 1 package com.lee.spring012.scan.mvc.annotation; 2  3 import org.springframework.stereotype.Repository; 4  5 @Repository 6 public class StuDAOImpl implements IStuDAO { 7  8   @Override 9   public void saveStu() {10     System.out.println("mvc dao: saving stu...");11   }12 13 }

StuServiceImpl.java

 1 package com.lee.spring012.scan.mvc.annotation; 2  3 import javax.annotation.Resource; 4  5 import org.springframework.stereotype.Service; 6  7 @Service 8 public class StuServiceImpl implements IStuService { 9 10   @Resource11   public IStuDAO stuDAOImpl;12   13   @Override14   public void saveStu() {15     System.out.println("mvc service: saving stu...");16     stuDAOImpl.saveStu();17   }18 19 }

测试

 1 package com.lee.spring012.scan.mvc.annotation; 2  3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPath 6  7 public class PersonTest { 8  9   @Test10   public void testPersonAction() {11     ApplicationContext context = new ClassPath);12     PersonAction person = (PersonAction)context.getBean("personAction");13     person.displaySave();14   }15 16 }

github地址:https://github.com/leechenxiang/maven-spring001-helloworld




原标题:Spring 一二事(8)

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

空运航空货运:https://www.goluckyvip.com/tag/35218.html
空运好还是陆运好:https://www.goluckyvip.com/tag/35219.html
空运和陆运哪个快:https://www.goluckyvip.com/tag/35220.html
空运货物货运:https://www.goluckyvip.com/tag/35221.html
空运价格在哪里查询:https://www.goluckyvip.com/tag/35222.html
空运进口清关:https://www.goluckyvip.com/tag/35223.html
为什么快递行业一直在打价格战?:https://www.kjdsnews.com/a/1842068.html
为什么快递行业一直在打价格战?:https://www.xlkjsw.com/news/90212.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流