Chapter 1 spring basics and chapter 1 spring Basics

Source: Internet
Author: User

Chapter 1 spring basics and chapter 1 spring Basics

Since the company developed and used spring boot, it began to learn spring boot. This series is mainly based on spring boot practices.

1.1 set up the spring environment and configure maven as follows:

<Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion> 4.0.0 </modelVersion> <groupId> com. antsoldier. chapter01 </groupId> <artifactId> springBoot </artifactId> <version> 0.0.1-SNAPSHOT </version> <properties> <org. springframework-version> 4.1.5.RELEASE </org. springframework-version> </properties> <dependencies> <dependency> <groupId> org. springframework </groupId> <artifactId> spring-context </artifactId> <version >$ {org. springframework-version }</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId> org. apache. maven. plugins </groupId> <artifactId> maven-compiler-plugin </artifactId> <version> 2.3.1 </version> <configuration> <source> 1.7 </source> <target> 1.7 </target> <span> View Code

 

 

 

(2) Use @ Service to introduce the spring container object class to the container, and @ Autowired automatically injects the object.

View Code

 

(3) Main configuration class

@ Configuration // indicates that this is a main Configuration class.
@ ComponentScan ("com. antsoldier. service") // indicates the package to be scanned by spring.

View Code

 

(4) run the program

View Code

 

1.4 spring 4.x does not recommend using configuration files for configuration, but also Java configuration.

Java Configuration is implemented through @ Configuration and @ Bean:

@ Configuration declares that the current class is a Configuration class, which is equivalent to a Spring Configuration XML

@ Bean annotation in the method, declare that the return value of the current method is a bean

A simple example is as follows:

Java configuration class:

View Code

Test main class

View Code

 

1.5 spring AOP example

@ Aspect declare a plane

@ After @ Before @ Around defines advice. You can directly use the interception rule (cut point) as a parameter.

@ After @ Before @ Around: the interception rule of the parameter is point)

Modify the pom file to add a new dependency

View Code

 

Compile the classes intercepted by the usage rules

View Code

Write cut plane

View Code

 

Write the main configuration class

View Code

 

Main execution Method

View Code

Aspect programming reference: http://www.cnblogs.com/xrq730/p/4919025.html

 

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.