Spring first thing (8), spring

Source: Internet
Author: User

Spring first thing (8), spring

1 <! -- 2 component: Put a class into the spring container. This class is a component3 that scans all classes in the package specified by base-package and sub-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 for multiple examples 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 }

Test

 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.ClassPathXmlApplicationContext; 6  7 public class PersonTest { 8  9     @Test10     public void testPersonAction() {11         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-scan-mvc.xml");12         PersonAction person = (PersonAction)context.getBean("personAction");13         person.displaySave();14     }15 16 }

Github address: https://github.com/leechenxiang/maven-spring001-helloworld

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.