Spring 家族
2021-04-21 20:37:14 1 举报
AI智能生成
登录查看完整内容
Spring生态
作者其他创作
大纲/内容
Spring Boot
Spring Cloud
Spring 家族
Spring
核心
对象容器
对象工厂Bean Facotry
应用上下文Applicaton Context
基于注解配置的应用上下文。AnnotationConfigApplicationContext
从一个或多个基于JAVA注解配置类中加载应用上下文。Loads a Spring application context from one or more Java-based configuration classes
基于直接配置的WEB应用上下文。AnnotationConfigWebApplicationContext
从一个或多个基于JAVA注解配置类中加载WEB应用上下文。Loads a Spring web application context from one or more Java-based configuration classes
基于编译路径中XML文件的应用上下文。ClassPathXmlApplicationContext
基于文件系统中XML文件的应用上下文。FileSystemXmlApplicationContext
从一个或多个文件系统中的XML文件加载应用上下文。Loads a context definition from one ormore XML files in the filesystem
基于WEB应用包中的XML文件的应用上下文。XmlWebApplicationContext
从一个或多个包含在应用包中的XML文件加载应用上下文。Loads context definitions from one or moreXML files contained in a web application
对象Beans
对象生命周期Bean lifecycle
创建对象实例。1、Spring instantiates the bean.
向对象实例属性中注入基础值与对象引用。2、Spring injects values and bean references into the bean's properties.
对象生效范围Bean scope
单例Singleton
整个对象容器里面只有一个实例。One instance of the bean is created for the entire application.
会话Session
请求Request
原型Prototype
每一次获取或注入对象都创建新的对象实例。One instance of the bean is created every time the bean is injectedinto or retrieved from the Spring application context.
注解Annotations
native
@ComponentScan
@Primary
当存在多个符合条件的对象的时候,标识当前对象作为首选。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
@Component
标识当前类作为一个对象实例。Identify the class as a component
默认使用类名首字母小写作为对象实例名称。Default given one name by lowercasing the first letter of the class name.
@Autowired
自动注入对象依赖。Inject bean's dependencies automatically
自动注入的优先级:类型、类全路径标识、名称。execution order: Type、Qualifier、Name
@Configuration
标识该类是配置类。Identify this class as a configuration class
@Profile
当特定的配置被启用,被该注解标注的对象将被创建。The bean will be created when the specific profile is active.
激活配置标识KEYspring.profiles.active/spring.profiles.default
配置在Servlet中As initialization parameters on DispatcherServlet
配置在容器上下文中As context parameters of a web application
配置在JNDI属性对中As JNDI entries
配置在环境变量中As environment variables
配置在JVM系统参数中As JVM system properties
使用@ActiveProfiles注解配置在测试类中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 fromgetRegistry().
Check for the presence and values of environment variables via the Environmentretrieved from getEnvironment().
Read and inspect the contents of resources loaded via the ResourceLoaderreturned from getResourceLoader().
Load and check for the presence of classes via the ClassLoader returned fromgetClassLoader().
AnnotatedTypeMetadata
Do something about the annotation
@PropertySource
引用属性配置文件并加载到容器环境对象中。Reference a property file and load it into Spring's Environment
@Value(\"${xxx}\")
通过key引用属性配置文件中的值。Reference property value
PropertyPlaceholderConfigurer类根据容器加载的属性配置文件解析占位符。b style=\
PropertySourcesPlaceholderConfigrer类根据容器上下文Environment以及属性配置文件解析占位符。PropertySourcesPlaceholderConfigrer resolves placeholders against Spring Environment and its set of property sources
@EnableAspectJAutoProxy
开启自动代理。Turn on auto-proxying
@Aspect
标识该类作为一个切面类。Identify the class is an aspect.
@Pointcut
通过函数标识一个切点。Indentify a pointcut through a method.
@Before
表明该函数在被增强的函数之前执行。The advice method is called before the advised method is called.
@After
表明该函数在被增强的函数执行之后或抛出异常之后执行。The advice method is called after the advised method returns or throws anexception.
@AfterReturning
表明该函数在被增强的函数之后执行。The advice method is called after the advised method returns.
@AfterThrowing
表明该函数在被增强的函数抛出异常之后执行。The advice method is called after the advised method throws an exception.
@Around
表明该函数包裹着被增强的函数执行。The advice method wraps the advised method.
@EnableWebMvc
开启基于注解驱动的Spring MVC。Enable annotation-driven Spring mvc
@EnableTransactionManagement
开启声明式事物管理。Enable declarative transaction management
@Transactional
JSR-330
@Named
@Inject
JSR-250
@Resource
效果上和@Autowire注解类似,执行注入顺序为名称、类型、全量标识。execution order: Name、Type、Qualifier
依赖注入(DI)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
logical
conditional
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
examples
#{1}、#{false}、#{9.87E4}
#{'Hello'}
#{T(System).currentTimeMillis()}
bean operation #{bean.property}、#{bean.method()}、#{bean?.method()}
get system properties #{systemProperties['xx']}
面向切面编程(AOP)Aspect-Oriented Programming
terminology
Advice
The job of an aspect.
category
Before—The advice functionality takes place before the advised method isinvoked.
After-returning—The advice functionality takes place after the advised methodsuccessfully completes.
After-throwing—The advice functionality takes place after the advised methodthrows an exception.
Join Points
Opportunities for advice to be applied. A point in the execution of the application where an aspect can be plugged in.
Pointcuts
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.
Compile time—Aspects are woven in when the target class is compiled. Thisrequires 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 theJVM. This requires a special ClassLoader that enhances the target class’s bytecodebefore the class is introduced into the application. AspectJ 5’s load-timeweaving (LTW) support weaves aspects this way.
pointcut expression language
args()
Limits join-point matches to the execution of methods whose arguments areinstances of the given types
@args()
Limits join-point matches to the execution of methods whose arguments areannotated 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 proxyis 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 anannotation 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 (theexecution of methods declared in types with the given annotation when usingSpring AOP)
@annotation
Limits join-point matches to those where the subject of the join point has thegiven annotation
bean()
Limits join-point matches to the bean with specific id.
Spring MVC
Spring Security
0 条评论
回复 删除
下一页