Java spring development environment construction and simple introductory example tutorial
This article describes the construction of Java spring development environment and a simple entry example. Share with you for your reference, as follows:
preface
Although I have used spring before, I still encounter difficulties in trying to build it today, and I find tutorials on the Internet. Many of them write examples of using spring MVC in the web. The getting start in the official document talks about the principle at the beginning (maybe the opening method is wrong). I can't do it. It's not easy to succeed in the experiment. Write it down so as to avoid trouble in the future.
Add dependent package
Enter the spring official website, switch to projects and click spring framework What is written on the official website is in the form of Maven dependencies, so you can create a maven project and add the following dependencies to POM In XML
Alternatively, you can click fork me on GitHub in the upper right corner of the page, download the dependency package in GitHub, and then add it to the build path of the project.
Note that spring context only contains the core functions of spring, such as dependency injection, aspect, etc.
Create spring configuration file
Create a new bean The XML configuration file is placed in the code directory. The contents of the file are as follows:
There are several points to be explained in this configuration file:
1. Namespace
This is the syntax of XML, which configures the tag format in the current XML file. Here, context and P namespaces are configured. For example, with a context space, you can use tags such as < / context: XXX >.
2. Auto scan component
This configuration allows the spring framework to automatically scan the classes annotated with @ component in the code and automatically create the objects of these classes.
Finally, notice the bean XML should be placed in the code directory in order to put the bean Add XML to the classpath.
Write code
First, write a person class as a bean class. The so-called bean class is simply a class in which all member variables have getter and setter methods and have parameter free construction methods. Then, the @ component ("person") annotation is used to mark the person class as a component. In this way, you can use @ resource to inject an instance of person into the members of other objects, or use the getBean (class) method of the application class to get an instance.
Then there is class A. class A has a person member variable that references an instance of person. We use spring dependency injection to manage the creation of the member variable person. In order to achieve this purpose, we only need to annotate the person variable with @ resource annotation.
summary
There are only three steps to create a spring project: ① adding dependencies ② writing bean classes ③ writing beans xml
The @ component and @ resource annotations are used when writing bean classes
Readers interested in more Java related content can view the topics on this site: introduction and advanced tutorial of spring framework, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills