A long time ago, I wanted to systematically learn and master the Spring framework, but I did not take any action for a long time. Now, there are not many things to do on a business trip, so I will spend some time studying the Spring framework from a simple perspective. Over the past few years, the Spring framework has developed into a powerful product. From the beginning, it was only used for dependency injection, but now it is impossible to avoid the package. The fields involved include dependency injection, MVC, JMS, Web flow, Batch job, Web service, Security ..... It covers almost all aspects of technology development. Although I have not been engaged in Java development for a long time, I have been involved in many components in Spring, such as those listed above. Spring is a library that Java programmers must master. Currently, the latest stable version of Spring is 4.0.2, which contains a large number of new features and is an important release. This version is basically used in this series. Let's start with a simple example and learn how to use the dependency injection function of Spring-Context. First, use maven to create a new project. 1 $: mvn archetype: After generate is created successfully, add the dependency on Spring-Context to the pom. xml file. Pom. xml1234567 <dependencies> <dependency> <groupId> org. springframework </groupId> <artifactId> spring-context </artifactId> <version> 4.0.2.RELEASE </version> </dependency> </dependencies> then we create a MovieService interface. MovieService. java123456package huangbowen.net. service; public interface MovieService {String getMovieName ();} create a DefaultMovieService to implement this interface. DefaultMovieService. java12345678package huangbowen.net. service; public class DefaultMovieService implements MovieService {public String getMovieName () {return "A Touch of Sin" ;}} then create A Cinema class and use MoviceService to publish A movie. Cinema. java123456789101112131415package huangbowen.net. service; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. component; @ Componentpublic class Cinema {@ Autowired private MovieService movieService; public void printMovieName () {System. out. println (movieService. getMovieName () ;}} creates an Application class. Application. java12345678910111213141516171819202122232425262728package huangbowen.net; import huangbowen.net. service. cinema; import huangbowen.net. service. defaultMovieService; import huangbowen.net. service. movieService; import org. springframework. context. annotation. annotationConfigApplicationContext; import org. springframework. context. annotation. bean; import org. springframework. context. annotation. compo NentScan; import org. springframework. context. annotation. configuration; @ Configuration @ ComponentScanpublic class Application {@ Bean public MovieService getMovieService () {return new DefaultMovieService ();} public static void main (String [] args) {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext (Application. class); Cinema cinema = applicationContext. g EtBean (Cinema. class); cinema. printMovieName () ;}} OK, run the main function, and get the console output: 1A Touch of Sin. In this example, the Annotation function is mainly used to inject MoviceService. We marked the header of Cinema. java as @ Component, indicating that this class is hosted by Spring. The MoviceService attribute in Cinema. java is marked as @ Autowired. Spring will find the Bean whose type is MovieService from the Application Context when initializing the Cinema class and assign it to Cinema. In Application. java, we declare a Bean of the MovieService type. Add Application. java as @ Configuration, which tells Spring to define one or more @ Bean methods in Application. java so that Spring containers can generate these beans at runtime. @ ComponentScan enables the Spring container to automatically scan the classes marked with @ Component under the current package. These classes will be hosted by Spring. Please download the source code in this example on GitHub.