Spring configuration file and spring configuration file

Source: Internet
Author: User

Spring configuration file and spring configuration file

When I first came into contact with the spring framework, I encountered such and such difficulties during configuration files. I did not make it clear in many aspects when I checked the information. I also sorted it out for future reference.

 

First, let's briefly describe the spring framework. As a Lightweight Framework, dependency injection and Aspect-Oriented are its features, then, the configuration file configures beans in the Bean factory as the classes of the system.

 

After learning about spring, download the jar package of spring and put it in the lib folder under web-inf and import it to start configuration.

First, start with web. xml. here we need to define a general front-end controller, which serves as the unique access point for spring to the outside world.

 

Here, we use <servlet> </servlet> to configure this springMVC servlet and import the dispatcherservlet class in the spring package as this class. At the same time, we also introduce other configurations, the classpath here refers to the src folder. Then, you can use servlet-mapping to configure interception. The "/" in url-pattern indicates the default interception method, indicating that the accessor will intercept all information.

According to the configuration in web. xml, we create the configuration file applicationcontext. xml in the src folder, introduce the namespace through xmlns, and write the header file of this xml file.

Here, I use the annotation form in the controller, so I also need to configure it here. Through <mvc: annotation-driven/>, spring registers the controller and can recognize it, next, you need to set the default controller package, which is also a line of code <context: component-scan base-package = "com. test2.controller "/> remember to change the package name of your controller later. You also need to configure the interception. Otherwise, you will not be able to access the static files. Here, use the default spring Interceptor to intercept the static files and use <mvc: default-servlet-handler/>.

In this way, the entire spring network can be connected. The next step is to connect the database.

Here, the database I use is mysql, and the database operations use the jdbctemplate interface that comes with spring. First, we pack the database connection with a bean and name it dataSource. Then, we can inject data when we need to perform database operations. During the connection, the com. mysql. jdbc. Driver Class is injected into driverClassName, and the database user name and password are configured. The jdbctemplate mentioned above also needs to be configured and injected with dataSource. In this way, the jdbctemplate method can be used in spring for operations. Paste my applicationcontext. the last part is the interface for processing the video. Here I select the file with the suffix .html as the view under the root directory.

 

In this way, the configuration in applicationcontext is basically complete. You can connect the database to the front-end normally, and add the required configurations to the front-end.

 

Related Article

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.