Spring 家族
2021-04-21 20:37:14 1 举报
AI智能生成
Spring生态
作者其他创作
大纲/内容
Spring Boot
Spring Cloud
Spring
核心
对象容器
对象工厂
Bean Facotry
Bean Facotry
应用上下文
Applicaton Context
Applicaton Context
基于注解配置的应用上下文。
AnnotationConfigApplicationContext
AnnotationConfigApplicationContext
从一个或多个基于JAVA注解配置类中加载应用上下文。
Loads a Spring application context from
one or more Java-based configuration classes
Loads a Spring application context from
one or more Java-based configuration classes
基于直接配置的WEB应用上下文。
AnnotationConfigWebApplicationContext
AnnotationConfigWebApplicationContext
从一个或多个基于JAVA注解配置类中加载WEB应用上下文。
Loads a Spring web application context from
one or more Java-based configuration classes
Loads a Spring web application context from
one or more Java-based configuration classes
基于编译路径中XML文件的应用上下文。
ClassPathXmlApplicationContext
ClassPathXmlApplicationContext
从一个或多个编译路径中的XML文件加载应用上下文,同时将应用上下文定义看做
编译路径中的资源。
Loads a context definition from one or more XML files located
in the classpath, treating context-definition files as classpath resources
编译路径中的资源。
Loads a context definition from one or more XML files located
in the classpath, treating context-definition files as classpath resources
基于文件系统中XML文件的应用上下文。
FileSystemXmlApplicationContext
FileSystemXmlApplicationContext
从一个或多个文件系统中的XML文件加载应用上下文。
Loads a context definition from one or
more XML files in the filesystem
Loads a context definition from one or
more XML files in the filesystem
基于WEB应用包中的XML文件的应用上下文。
XmlWebApplicationContext
XmlWebApplicationContext
从一个或多个包含在应用包中的XML文件加载应用上下文。
Loads context definitions from one or more
XML files contained in a web application
Loads context definitions from one or more
XML files contained in a web application
对象
Beans
Beans
对象生命周期
Bean lifecycle
Bean lifecycle
创建对象实例。
1、Spring instantiates the bean.
1、Spring instantiates the bean.
向对象实例属性中注入基础值与对象引用。
2、Spring injects values and bean references into the bean's properties.
2、Spring injects values and bean references into the bean's properties.
如果类实现了BeanNameAware接口,则调用setBeanName()函数注入实例ID。
3、If the bean implements BeanNameAware, Spring
passes the bean's ID to the set-BeanName() method.
3、If the bean implements BeanNameAware, Spring
passes the bean's ID to the set-BeanName() method.
如果类实现了BeanFactoryAware接口,则调用setBeanFactory()函数注入该对象的对象工厂实例。
4、If the bean implements BeanFactoryAware, Spring calls the setBeanFactory()
method, passing in the bean factory itself.
4、If the bean implements BeanFactoryAware, Spring calls the setBeanFactory()
method, passing in the bean factory itself.
如果类实现了ApplicationContext接口,则调用setApplicationContext()函数注入该对象所属的应用上下文。
5、If the bean implements ApplicationContextAware, Spring calls the set-
ApplicationContext() method, passing in a reference to the enclosing application
context.
5、If the bean implements ApplicationContextAware, Spring calls the set-
ApplicationContext() method, passing in a reference to the enclosing application
context.
如果类实现了BeanPostProcessor接口,则调用postProcessBeforeInitialization()函数,执行自定义逻辑。
6、If the bean implements the BeanPostProcessor interface, Spring calls its post-
ProcessBeforeInitialization() method.
6、If the bean implements the BeanPostProcessor interface, Spring calls its post-
ProcessBeforeInitialization() method.
如果类实现了InitializingBean接口,则调用afterPropertiesSet()函数,同样的,如果类生命了初始化方法,初始化方法也会被调用。
7、If the bean implements the InitializingBean interface, Spring calls its after-
PropertiesSet() method. Similarly, if the bean was declared with an initmethod,
then the specified initialization method is called.
7、If the bean implements the InitializingBean interface, Spring calls its after-
PropertiesSet() method. Similarly, if the bean was declared with an initmethod,
then the specified initialization method is called.
如果类实现了BeanPostProcessor接口,则调用postProcessAfterInitialization()函数。
8、If the bean implements BeanPostProcessor, Spring calls its postProcess-
AfterInitialization() method.
8、If the bean implements BeanPostProcessor, Spring calls its postProcess-
AfterInitialization() method.
到这一步,对象已经可以被容器使用,并且已经存在上下文中了。
9、At this point, the bean is ready to be used by the application and remains in the
application context until the application context is destroyed.
9、At this point, the bean is ready to be used by the application and remains in the
application context until the application context is destroyed.
如果类实现了DisposableBean接口,则调用destroy()函数。同样的,如果生命了销毁方法则调用该销毁方法。
10、If the bean implements the DisposableBean interface, Spring calls its
destroy() method. Likewise, if the bean was declared with a destroy-method,
the specified method is called.
10、If the bean implements the DisposableBean interface, Spring calls its
destroy() method. Likewise, if the bean was declared with a destroy-method,
the specified method is called.
对象生效范围
Bean scope
Bean scope
单例
Singleton
Singleton
整个对象容器里面只有一个实例。
One instance of the bean is created for the entire application.
One instance of the bean is created for the entire application.
会话
Session
Session
为每一个会话创建一个对象实例。
In a web application, one instance of the bean is created for each session.
In a web application, one instance of the bean is created for each session.
请求
Request
Request
为每一次请求创建一个对象实例。
In a web application, one instance of the bean is created for each request.
In a web application, one instance of the bean is created for each request.
原型
Prototype
Prototype
每一次获取或注入对象都创建新的对象实例。
One instance of the bean is created every time the bean is injected
into or retrieved from the Spring application context.
One instance of the bean is created every time the bean is injected
into or retrieved from the Spring application context.
注解
Annotations
Annotations
native
@ComponentScan
开启组件扫描,默认扫描配置类所在的包。容器扫描目标包及其子包下的有@Component注解的类。
Enable component scanning, default the same package as the configuration class.
Spring scan that package and any subpackages underneath it, looking for classes
that are annotated with @Component
Enable component scanning, default the same package as the configuration class.
Spring scan that package and any subpackages underneath it, looking for classes
that are annotated with @Component
@Primary
当存在多个符合条件的对象的时候,标识当前对象作为首选。
Identify this bean is the first choice when there are multiple
qualified beans. A.K.A this bean is your favorite one
Identify this bean is the first choice when there are multiple
qualified beans. A.K.A this bean is your favorite one
@Qualifier
@Scope
ConfigurableBeanFactory.SCOPE_PROTOTYPE
ConfigurableBeanFactory.SCOPE_SINGLETON
WebApplicationContext.SCOPE_REQUEST
WebApplicationContext.SCOPE_SESSION
ScopedProxyMode.INTERFACES
ScopedProxyMode.TARGET_CLASS
@Bean
表明被@Bean注解修饰的函数会返回将注册到应用上下文的对象。并且对象
默认名称和函数名保持一致。另一方面,如果函数有任何参数,容器会自动
注入到函数里。
Indicate the method with @Bean annotation will return an object that
should be registered in spring application context. And the default bean
id is the same as the name of the method. In the other hand, if the
method has any parameters, spring will autowired the dependency into the
method.
默认名称和函数名保持一致。另一方面,如果函数有任何参数,容器会自动
注入到函数里。
Indicate the method with @Bean annotation will return an object that
should be registered in spring application context. And the default bean
id is the same as the name of the method. In the other hand, if the
method has any parameters, spring will autowired the dependency into the
method.
@Component
标识当前类作为一个对象实例。
Identify the class as a component
Identify the class as a component
默认使用类名首字母小写作为对象实例名称。
Default given one name by lowercasing the first letter of the class name.
Default given one name by lowercasing the first letter of the class name.
@Autowired
自动注入对象依赖。
Inject bean's dependencies automatically
Inject bean's dependencies automatically
当该注解使用在构造器上,容器通过构造器初始化该对象。
When annotated upon constructor, spring instantiate the via this constructor
When annotated upon constructor, spring instantiate the via this constructor
当注解使用在setXX()函数上,在对象创建完之后,容器尝试通过setXX()进行属性注入。
When annotated upon setXX() method, after bean creation, spring try to satisfy
dependencies through setXX() method
When annotated upon setXX() method, after bean creation, spring try to satisfy
dependencies through setXX() method
自动注入的优先级:类型、类全路径标识、名称。
execution order: Type、Qualifier、Name
execution order: Type、Qualifier、Name
@Configuration
标识该类是配置类。
Identify this class as a configuration class
Identify this class as a configuration class
@Profile
当特定的配置被启用,被该注解标注的对象将被创建。
The bean will be created when the specific profile is active.
The bean will be created when the specific profile is active.
激活配置标识KEY
spring.profiles.active/spring.profiles.default
spring.profiles.active/spring.profiles.default
配置在Servlet中
As initialization parameters on DispatcherServlet
As initialization parameters on DispatcherServlet
配置在容器上下文中
As context parameters of a web application
As context parameters of a web application
配置在JNDI属性对中
As JNDI entries
As JNDI entries
配置在环境变量中
As environment variables
As environment variables
配置在JVM系统参数中
As JVM system properties
As JVM system properties
使用@ActiveProfiles注解配置在测试类中
Using the @ActiveProfiles annotation on an integration test class
Using the @ActiveProfiles annotation on an integration test class
@Conditional
user Condition interface to make the decision
Parameters of matches() method in Contidion
ConditionContext
Check for bean definitions via the BeanDefinitionRegistry returned from
getRegistry().
getRegistry().
Check for the presence of beans, and even dig into bean properties via the
ConfigurableListableBeanFactory returned from getBeanFactory().
ConfigurableListableBeanFactory returned from getBeanFactory().
Check for the presence and values of environment variables via the Environment
retrieved from getEnvironment().
retrieved from getEnvironment().
Read and inspect the contents of resources loaded via the ResourceLoader
returned from getResourceLoader().
returned from getResourceLoader().
Load and check for the presence of classes via the ClassLoader returned from
getClassLoader().
getClassLoader().
AnnotatedTypeMetadata
Do something about the annotation
@PropertySource
引用属性配置文件并加载到容器环境对象中。
Reference a property file and load it into Spring's Environment
Reference a property file and load it into Spring's Environment
@Value("${xxx}")
通过key引用属性配置文件中的值。
Reference property value
Reference property value
PropertyPlaceholderConfigurer类根据容器加载的属性配置文件解析占位符。
PropertyPlaceholderConfigurer resolves placeholders against Spring's property source
PropertyPlaceholderConfigurer resolves placeholders against Spring's property source
PropertySourcesPlaceholderConfigrer类根据容器上下文Environment
以及属性配置文件解析占位符。
PropertySourcesPlaceholderConfigrer resolves placeholders
against Spring Environment and its set of property sources
以及属性配置文件解析占位符。
PropertySourcesPlaceholderConfigrer resolves placeholders
against Spring Environment and its set of property sources
@EnableAspectJAutoProxy
开启自动代理。
Turn on auto-proxying
Turn on auto-proxying
@Aspect
标识该类作为一个切面类。
Identify the class is an aspect.
Identify the class is an aspect.
@Pointcut
通过函数标识一个切点。
Indentify a pointcut through a method.
Indentify a pointcut through a method.
@Before
表明该函数在被增强的函数之前执行。
The advice method is called before the advised method is called.
The advice method is called before the advised method is called.
@After
表明该函数在被增强的函数执行之后或抛出异常之后执行。
The advice method is called after the advised method returns or throws an
exception.
The advice method is called after the advised method returns or throws an
exception.
@AfterReturning
表明该函数在被增强的函数之后执行。
The advice method is called after the advised method returns.
The advice method is called after the advised method returns.
@AfterThrowing
表明该函数在被增强的函数抛出异常之后执行。
The advice method is called after the advised method throws an exception.
The advice method is called after the advised method throws an exception.
@Around
表明该函数包裹着被增强的函数执行。
The advice method wraps the advised method.
The advice method wraps the advised method.
@EnableWebMvc
开启基于注解驱动的Spring MVC。
Enable annotation-driven Spring mvc
Enable annotation-driven Spring mvc
@EnableTransactionManagement
开启声明式事物管理。
Enable declarative transaction management
Enable declarative transaction management
@Transactional
JSR-330
@Named
效果上和@Component注解类似。
Has the same effect as @Component, but with subtle differences
Has the same effect as @Component, but with subtle differences
@Inject
效果上和@Autowired注解类似,执行注入顺序为类型、全量标识、名称。
Has the same effect as @Autowired, but with subtle differences.
execution order: Type、Qualifier、Name
Has the same effect as @Autowired, but with subtle differences.
execution order: Type、Qualifier、Name
JSR-250
@Resource
效果上和@Autowire注解类似,执行注入顺序为名称、类型、全量标识。
execution order: Name、Type、Qualifier
execution order: Name、Type、Qualifier
依赖注入(DI)
Dependency Injection
Dependency Injection
reference
Auto Wiring
Constructor Wiring
Set method Wiring
value
Property placeholder ${xxx}
Environment Property
SpEL
syntax
#{xxx}
operators
arithmetic
+, -, *, /, %, ^
^ is power operation
comparison
<, lt, >, gt, ==, eq, <=, le, >=, ge
logical
and, or, not, |
conditional
?: (ternary), ?: (Elvis)
Elvis operator: #{disc.title ?: 'Rattle and Hum'}
regular expression
matches
#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}
collection
.?[]
select the sub collection
.^[]
select the first matching entry
.$[]
select the last matching entry
.![]
project properties of the elements of the
collection onto a new collection
collection onto a new collection
examples
#{1}、#{false}、#{9.87E4}
#{'Hello'}
#{T(System).currentTimeMillis()}
bean operation #{bean.property}、#{bean.method()}、#{bean?.method()}
get system properties #{systemProperties['xx']}
#{'This is a test'[3]} this references the fourth (zero-based) character in the String, or s.
面向切面编程(AOP)
Aspect-Oriented Programming
Aspect-Oriented Programming
terminology
Advice
The job of an aspect.
category
Before—The advice functionality takes place before the advised method is
invoked.
invoked.
After—The advice functionality takes place after the advised method completes,
regardless of the outcome.
regardless of the outcome.
After-returning—The advice functionality takes place after the advised method
successfully completes.
successfully completes.
After-throwing—The advice functionality takes place after the advised method
throws an exception.
throws an exception.
Around—The advice wraps the advised method, providing some functionality
before and after the advised method is invoked.
before and after the advised method is invoked.
Join Points
Opportunities for advice to be applied. A point in the execution
of the application where an aspect can be plugged in.
of the application where an aspect can be plugged in.
Pointcuts
Matching one or more join points at which advice should be woven.
In other word, pointcuts define which join points get advised.
In other word, pointcuts define which join points get advised.
Aspects
Take together advice and pointcut.
Introductions
Allow you to add new methods or attributes to exsiting classes.
Weaving
The process of applying aspects to a target object to create a new proxied object.
category
Compile time—Aspects are woven in when the target class is compiled. This
requires a special compiler. AspectJ’s weaving compiler weaves aspects this way.
requires a special compiler. AspectJ’s weaving compiler weaves aspects this way.
Class load time—Aspects are woven in when the target class is loaded into the
JVM. This requires a special ClassLoader that enhances the target class’s bytecode
before the class is introduced into the application. AspectJ 5’s load-time
weaving (LTW) support weaves aspects this way.
JVM. This requires a special ClassLoader that enhances the target class’s bytecode
before the class is introduced into the application. AspectJ 5’s load-time
weaving (LTW) support weaves aspects this way.
Runtime—Aspects are woven in sometime during the execution of the application.
Typically, an AOP container dynamically generates a proxy object that delegates
to the target object while weaving in the aspects. This is how Spring AOP
aspects are woven.
Typically, an AOP container dynamically generates a proxy object that delegates
to the target object while weaving in the aspects. This is how Spring AOP
aspects are woven.
pointcut expression language
args()
Limits join-point matches to the execution of methods whose arguments are
instances of the given types
instances of the given types
@args()
Limits join-point matches to the execution of methods whose arguments are
annotated with the given annotation types
annotated with the given annotation types
execution()
Matches join points that are method executions
this()
Limits join-point matches to those where the bean reference of the AOP proxy
is of a given type
is of a given type
target()
Limits join-point matches to those where the target object is of a given type
@target()
Limits matching to join points where the class of the executing object has an
annotation of the given type
annotation of the given type
within()
Limits matching to join points within certain types
@within()
Limits matching to join points within types that have the given annotation (the
execution of methods declared in types with the given annotation when using
Spring AOP)
execution of methods declared in types with the given annotation when using
Spring AOP)
@annotation
Limits join-point matches to those where the subject of the join point has the
given annotation
given annotation
bean()
Limits join-point matches to the bean with specific id.
Spring MVC
Spring Security
0 条评论
下一页