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 CodeTest 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 CodeWrite cut plane
View Code
Write the main configuration class
View Code
Main execution Method
View CodeAspect programming reference: http://www.cnblogs.com/xrq730/p/4919025.html