1 <!--
2 component:把一个类放入到spring容器中,该类就是一个component
3 在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 IStuService {
4 public void saveStu();
5 }
PersonAction.java
package com.lee.spring012.scan.mvc.annotation;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
@Controller
@Scope("prototype") // action为多例
public class PersonAction {
@Resource
public IStuService stuServiceImpl;
public void displaySave() {
System.out.println("mvc action: saving stu...");
stuServiceImpl.saveStu();
}
}
StuDAOImpl.java
package com.lee.spring012.scan.mvc.annotation;
import org.springframework.stereotype.Repository;
@Repository
public class StuDAOImpl implements IStuDAO {
@Override
public void saveStu() {
System.out.println("mvc dao: saving stu...");
}
}
StuServiceImpl.java
package com.lee.spring012.scan.mvc.annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class StuServiceImpl implements IStuService {
@Resource
public IStuDAO stuDAOImpl;
@Override
public void saveStu() {
System.out.println("mvc service: saving stu...");
stuDAOImpl.saveStu();
}
}
测试:
package com.lee.spring012.scan.mvc.annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonTest {
@Test
public void testPersonAction() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-scan-mvc.xml");
PersonAction person = (PersonAction)context.getBean("personAction");
person.displaySave();
}
}
點(diǎn)擊查看更多內(nèi)容
風(fēng)間影月說
去圍觀
創(chuàng)業(yè)公司技術(shù)總監(jiān), 10年+開發(fā)和技術(shù)管理經(jīng)驗(yàn)。SUN認(rèn)證SCJP、PMP、MCP認(rèn)證。主要從事后端技術(shù)和架構(gòu)領(lǐng)域,有豐富的電商平臺(tái)與物流平臺(tái)核心系統(tǒng)的架構(gòu)設(shè)計(jì)和開發(fā)經(jīng)驗(yàn)。
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少


贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦