Skip to content

Commit e2e0410

Browse files
committed
Method-level javadoc on XmlReaderContext etc
1 parent f43ea96 commit e2e0410

File tree

6 files changed

+169
-26
lines changed

6 files changed

+169
-26
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/parsing/ReaderContext.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public class ReaderContext {
3838
private final SourceExtractor sourceExtractor;
3939

4040

41+
/**
42+
* Construct a new {@code ReaderContext}.
43+
* @param resource the XML bean definition resource
44+
* @param problemReporter the problem reporter in use
45+
* @param eventListener the event listener in use
46+
* @param sourceExtractor the source extractor in use
47+
*/
4148
public ReaderContext(Resource resource, ProblemReporter problemReporter,
4249
ReaderEventListener eventListener, SourceExtractor sourceExtractor) {
4350

@@ -52,83 +59,150 @@ public final Resource getResource() {
5259
}
5360

5461

62+
// Errors and warnings
63+
64+
/**
65+
* Raise a fatal error.
66+
*/
5567
public void fatal(String message, @Nullable Object source) {
5668
fatal(message, source, null, null);
5769
}
5870

71+
/**
72+
* Raise a fatal error.
73+
*/
5974
public void fatal(String message, @Nullable Object source, @Nullable Throwable cause) {
6075
fatal(message, source, null, cause);
6176
}
6277

78+
/**
79+
* Raise a fatal error.
80+
*/
6381
public void fatal(String message, @Nullable Object source, @Nullable ParseState parseState) {
6482
fatal(message, source, parseState, null);
6583
}
6684

85+
/**
86+
* Raise a fatal error.
87+
*/
6788
public void fatal(String message, @Nullable Object source, @Nullable ParseState parseState, @Nullable Throwable cause) {
6889
Location location = new Location(getResource(), source);
6990
this.problemReporter.fatal(new Problem(message, location, parseState, cause));
7091
}
7192

93+
/**
94+
* Raise a fatal error.
95+
*/
7296
public void error(String message, @Nullable Object source) {
7397
error(message, source, null, null);
7498
}
7599

100+
/**
101+
* Raise a regular error.
102+
*/
76103
public void error(String message, @Nullable Object source, @Nullable Throwable cause) {
77104
error(message, source, null, cause);
78105
}
79106

107+
/**
108+
* Raise a regular error.
109+
*/
80110
public void error(String message, @Nullable Object source, @Nullable ParseState parseState) {
81111
error(message, source, parseState, null);
82112
}
83113

114+
/**
115+
* Raise a regular error.
116+
*/
84117
public void error(String message, @Nullable Object source, @Nullable ParseState parseState, @Nullable Throwable cause) {
85118
Location location = new Location(getResource(), source);
86119
this.problemReporter.error(new Problem(message, location, parseState, cause));
87120
}
88121

122+
/**
123+
* Raise a non-critical warning.
124+
*/
89125
public void warning(String message, @Nullable Object source) {
90126
warning(message, source, null, null);
91127
}
92128

129+
/**
130+
* Raise a non-critical warning.
131+
*/
93132
public void warning(String message, @Nullable Object source, @Nullable Throwable cause) {
94133
warning(message, source, null, cause);
95134
}
96135

136+
/**
137+
* Raise a non-critical warning.
138+
*/
97139
public void warning(String message, @Nullable Object source, @Nullable ParseState parseState) {
98140
warning(message, source, parseState, null);
99141
}
100142

143+
/**
144+
* Raise a non-critical warning.
145+
*/
101146
public void warning(String message, @Nullable Object source, @Nullable ParseState parseState, @Nullable Throwable cause) {
102147
Location location = new Location(getResource(), source);
103148
this.problemReporter.warning(new Problem(message, location, parseState, cause));
104149
}
105150

106151

152+
// Explicit parse events
153+
154+
/**
155+
* Fire an defaults-registered event.
156+
*/
107157
public void fireDefaultsRegistered(DefaultsDefinition defaultsDefinition) {
108158
this.eventListener.defaultsRegistered(defaultsDefinition);
109159
}
110160

161+
/**
162+
* Fire an component-registered event.
163+
*/
111164
public void fireComponentRegistered(ComponentDefinition componentDefinition) {
112165
this.eventListener.componentRegistered(componentDefinition);
113166
}
114167

168+
/**
169+
* Fire an alias-registered event.
170+
*/
115171
public void fireAliasRegistered(String beanName, String alias, @Nullable Object source) {
116172
this.eventListener.aliasRegistered(new AliasDefinition(beanName, alias, source));
117173
}
118174

175+
/**
176+
* Fire an import-processed event.
177+
*/
119178
public void fireImportProcessed(String importedResource, @Nullable Object source) {
120179
this.eventListener.importProcessed(new ImportDefinition(importedResource, source));
121180
}
122181

182+
/**
183+
* Fire an import-processed event.
184+
*/
123185
public void fireImportProcessed(String importedResource, Resource[] actualResources, @Nullable Object source) {
124186
this.eventListener.importProcessed(new ImportDefinition(importedResource, actualResources, source));
125187
}
126188

127189

190+
// Source extraction
191+
192+
/**
193+
* Return the source extractor in use.
194+
*/
128195
public SourceExtractor getSourceExtractor() {
129196
return this.sourceExtractor;
130197
}
131198

199+
/**
200+
* Call the source extractor for the given source object.
201+
* @param sourceCandidate the original source object
202+
* @return the source object to store, or {@code null} for none.
203+
* @see #getSourceExtractor()
204+
* @see SourceExtractor#extractSource
205+
*/
132206
@Nullable
133207
public Object extractSource(Object sourceCandidate) {
134208
return this.sourceExtractor.extractSource(sourceCandidate, this.resource);

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ public <T> T createBean(Class<T> beanClass) throws BeansException {
298298
RootBeanDefinition bd = new RootBeanDefinition(beanClass);
299299
bd.setScope(SCOPE_PROTOTYPE);
300300
bd.allowCaching = ClassUtils.isCacheSafe(beanClass, getBeanClassLoader());
301+
// For the nullability warning, see the elaboration in AbstractBeanFactory.doGetBean;
302+
// in short: This is never going to be null unless user-declared code enforces null.
301303
return (T) createBean(beanClass.getName(), bd, null);
302304
}
303305

@@ -331,6 +333,8 @@ public Object configureBean(Object existingBean, String beanName) throws BeansEx
331333
BeanWrapper bw = new BeanWrapperImpl(existingBean);
332334
initBeanWrapper(bw);
333335
populateBean(beanName, bd, bw);
336+
// For the nullability warning, see the elaboration in AbstractBeanFactory.doGetBean;
337+
// in short: This is never going to be null unless user-declared code enforces null.
334338
return initializeBean(beanName, existingBean, bd);
335339
}
336340

@@ -349,6 +353,8 @@ public Object createBean(Class<?> beanClass, int autowireMode, boolean dependenc
349353
// Use non-singleton bean definition, to avoid registering bean as dependent bean.
350354
RootBeanDefinition bd = new RootBeanDefinition(beanClass, autowireMode, dependencyCheck);
351355
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
356+
// For the nullability warning, see the elaboration in AbstractBeanFactory.doGetBean;
357+
// in short: This is never going to be null unless user-declared code enforces null.
352358
return createBean(beanClass.getName(), bd, null);
353359
}
354360

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import org.springframework.beans.factory.CannotLoadBeanClassException;
5757
import org.springframework.beans.factory.FactoryBean;
5858
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
59-
import org.springframework.beans.factory.ObjectFactory;
6059
import org.springframework.beans.factory.SmartFactoryBean;
6160
import org.springframework.beans.factory.config.BeanDefinition;
6261
import org.springframework.beans.factory.config.BeanDefinitionHolder;
@@ -336,16 +335,13 @@ else if (mbd.isPrototype()) {
336335
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
337336
}
338337
try {
339-
Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
340-
@Override
341-
public Object getObject() throws BeansException {
342-
beforePrototypeCreation(beanName);
343-
try {
344-
return createBean(beanName, mbd, args);
345-
}
346-
finally {
347-
afterPrototypeCreation(beanName);
348-
}
338+
Object scopedInstance = scope.get(beanName, () -> {
339+
beforePrototypeCreation(beanName);
340+
try {
341+
return createBean(beanName, mbd, args);
342+
}
343+
finally {
344+
afterPrototypeCreation(beanName);
349345
}
350346
});
351347
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
@@ -389,7 +385,8 @@ public Object getObject() throws BeansException {
389385
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
390386
}
391387
}
392-
// For the nullability warning, see the elaboration in the comment above.
388+
// For the nullability warning, see the elaboration in the comment above;
389+
// in short: This is never going to be null unless user-declared code enforces null.
393390
return (T) bean;
394391
}
395392

spring-beans/src/main/java/org/springframework/beans/factory/xml/XmlReaderContext.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,15 @@ public class XmlReaderContext extends ReaderContext {
4949
private final NamespaceHandlerResolver namespaceHandlerResolver;
5050

5151

52+
/**
53+
* Construct a new {@code XmlReaderContext}.
54+
* @param resource the XML bean definition resource
55+
* @param problemReporter the problem reporter in use
56+
* @param eventListener the event listener in use
57+
* @param sourceExtractor the source extractor in use
58+
* @param reader the XML bean definition reader in use
59+
* @param namespaceHandlerResolver the XML namespace resolver
60+
*/
5261
public XmlReaderContext(
5362
Resource resource, ProblemReporter problemReporter,
5463
ReaderEventListener eventListener, SourceExtractor sourceExtractor,
@@ -60,43 +69,89 @@ public XmlReaderContext(
6069
}
6170

6271

72+
/**
73+
* Return the XML bean definition reader in use.
74+
*/
6375
public final XmlBeanDefinitionReader getReader() {
6476
return this.reader;
6577
}
6678

79+
/**
80+
* Return the bean definition registry to use.
81+
* @see XmlBeanDefinitionReader#XmlBeanDefinitionReader(BeanDefinitionRegistry)
82+
*/
6783
public final BeanDefinitionRegistry getRegistry() {
6884
return this.reader.getRegistry();
6985
}
7086

87+
/**
88+
* Return the resource loader to use, if any.
89+
* <p>This will be non-null in regular scenarios,
90+
* also allowing access to the resource class loader.
91+
* @see XmlBeanDefinitionReader#setResourceLoader
92+
* @see ResourceLoader#getClassLoader()
93+
*/
7194
@Nullable
7295
public final ResourceLoader getResourceLoader() {
7396
return this.reader.getResourceLoader();
7497
}
7598

99+
/**
100+
* Return the bean class loader to use, if any.
101+
* <p>Note that this will be null in regular scenarios,
102+
* as an indication to lazily resolve bean classes.
103+
* @see XmlBeanDefinitionReader#setBeanClassLoader
104+
*/
76105
@Nullable
77106
public final ClassLoader getBeanClassLoader() {
78107
return this.reader.getBeanClassLoader();
79108
}
80109

110+
/**
111+
* Return the environment to use.
112+
* @see XmlBeanDefinitionReader#setEnvironment
113+
*/
81114
public final Environment getEnvironment() {
82115
return this.reader.getEnvironment();
83116
}
84117

118+
/**
119+
* Return the namespace resolver.
120+
* @see XmlBeanDefinitionReader#setNamespaceHandlerResolver
121+
*/
85122
public final NamespaceHandlerResolver getNamespaceHandlerResolver() {
86123
return this.namespaceHandlerResolver;
87124
}
88125

89126

127+
// Convenience methods to delegate to
128+
129+
/**
130+
* Call the bean name generator for the given bean definition.
131+
* @see XmlBeanDefinitionReader#getBeanNameGenerator()
132+
* @see org.springframework.beans.factory.support.BeanNameGenerator#generateBeanName
133+
*/
90134
public String generateBeanName(BeanDefinition beanDefinition) {
91135
return this.reader.getBeanNameGenerator().generateBeanName(beanDefinition, getRegistry());
92136
}
93137

138+
/**
139+
* Call the bean name generator for the given bean definition
140+
* and register the bean definition under the generated name.
141+
* @see XmlBeanDefinitionReader#getBeanNameGenerator()
142+
* @see org.springframework.beans.factory.support.BeanNameGenerator#generateBeanName
143+
* @see BeanDefinitionRegistry#registerBeanDefinition
144+
*/
94145
public String registerWithGeneratedName(BeanDefinition beanDefinition) {
95146
String generatedName = generateBeanName(beanDefinition);
96147
getRegistry().registerBeanDefinition(generatedName, beanDefinition);
97148
return generatedName;
98149
}
99150

151+
/**
152+
* Read an XML document from the given String.
153+
* @see #getReader()
154+
*/
100155
public Document readDocumentFromString(String documentContent) {
101156
InputSource is = new InputSource(new StringReader(documentContent));
102157
try {

spring-orm/src/main/java/org/springframework/orm/hibernate5/support/OpenSessionInterceptor.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,38 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
100100
}
101101

102102
/**
103-
* Open a Session for the SessionFactory that this interceptor uses.
103+
* Open a Session for the given SessionFactory.
104104
* <p>The default implementation delegates to the {@link SessionFactory#openSession}
105105
* method and sets the {@link Session}'s flush mode to "MANUAL".
106+
* @param sessionFactory the SessionFactory to use
106107
* @return the Session to use
107108
* @throws DataAccessResourceFailureException if the Session could not be created
108-
* @since 4.3.9
109+
* @since 5.0
109110
* @see FlushMode#MANUAL
110111
*/
111112
@SuppressWarnings("deprecation")
112113
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
113-
try {
114-
Session session = sessionFactory.openSession();
115-
session.setFlushMode(FlushMode.MANUAL);
116-
return session;
117-
}
118-
catch (HibernateException ex) {
119-
throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
114+
Session session = openSession();
115+
if (session == null) {
116+
try {
117+
session = sessionFactory.openSession();
118+
session.setFlushMode(FlushMode.MANUAL);
119+
}
120+
catch (HibernateException ex) {
121+
throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
122+
}
120123
}
124+
return session;
125+
}
126+
127+
/**
128+
* Open a Session for the given SessionFactory.
129+
* @deprecated as of 5.0, in favor of {@link #openSession(SessionFactory)}
130+
*/
131+
@Deprecated
132+
@Nullable
133+
protected Session openSession() throws DataAccessResourceFailureException {
134+
return null;
121135
}
122136

123137
}

0 commit comments

Comments
 (0)