@@ -22,11 +22,14 @@ public BeanDiscoveryMode getBeanDiscoveryMode() {
2222 };
2323
2424 private static final String CLOSING_TAG_PREFIX = "</" ;
25+ private static final String CLOSING_TAG_EMPTY = "/>" ;
2526 private static final String OPENING_TAG_PREFIX = "<" ;
2627 private static final String TAG_SUFFIX = ">" ;
2728 private static final String TAG_SUFFIX_NEW_LINE = ">\n " ;
2829 private static final String TAG_SUFFIX_SELF_CLOSE_NEW_LINE = " />\n " ;
2930 private static final String ALTERNATIVES_ELEMENT_NAME = "alternatives" ;
31+ private static final String DECORATORS_ELEMENT_NAME = "decorators" ;
32+ private static final String INTERCEPTORS_ELEMENT_NAME = "interceptors" ;
3033 private static final String CLASS = "class" ;
3134
3235 private static final String SCAN_ELEMENT_NAME = "scan" ;
@@ -37,11 +40,14 @@ public BeanDiscoveryMode getBeanDiscoveryMode() {
3740 private static final String NAME_ATTRIBUTE_NAME = "name" ;
3841 private static final String VALUE_ATTRIBUTE_NAME = "value" ;
3942
43+ private static final String TRIM_ELEMENT_NAME = "trim" ;
44+
4045 private final List <Class <?>> alternatives ;
4146 private final List <Class <?>> interceptors ;
4247 private final List <Class <?>> decorators ;
4348 private final List <Class <?>> stereotypes ;
4449 private final List <Exclude > excludeFilters ;
50+ private boolean trimArchive ;
4551
4652 private BeanDiscoveryMode mode = BeanDiscoveryMode .ANNOTATED ;
4753
@@ -215,6 +221,11 @@ public BeansXml excludeFilters(Exclude... filters) {
215221 return this ;
216222 }
217223
224+ public BeansXml trim () {
225+ this .trimArchive = true ;
226+ return this ;
227+ }
228+
218229 public BeanDiscoveryMode getBeanDiscoveryMode () {
219230 return mode ;
220231 }
@@ -226,20 +237,21 @@ public void setBeanDiscoveryMode(BeanDiscoveryMode mode) {
226237 @ Override
227238 public InputStream openStream () {
228239 StringBuilder xml = new StringBuilder ();
229- xml .append ("<beans version=\" 1 .1\" bean-discovery-mode=\" " );
240+ xml .append ("<beans version=\" 4 .1\" bean-discovery-mode=\" " );
230241 xml .append (getBeanDiscoveryMode ().getValue ());
231242 xml .append ("\" >\n " );
232243 appendExcludeFilters (excludeFilters , xml );
233244 appendAlternatives (alternatives , stereotypes , xml );
234- appendSection ("interceptors" , CLASS , interceptors , xml );
235- appendSection ("decorators" , CLASS , decorators , xml );
245+ appendSection (INTERCEPTORS_ELEMENT_NAME , CLASS , interceptors , xml );
246+ appendSection (DECORATORS_ELEMENT_NAME , CLASS , decorators , xml );
247+ appendTrimming (trimArchive , xml );
236248 xml .append ("</beans>" );
237249
238250 return new ByteArrayInputStream (xml .toString ().getBytes ());
239251 }
240252
241253 private void appendExcludeFilters (List <Exclude > filters , StringBuilder xml ) {
242- if (filters .size () > 0 ) {
254+ if (! filters .isEmpty () ) {
243255 xml .append (OPENING_TAG_PREFIX ).append (SCAN_ELEMENT_NAME ).append (TAG_SUFFIX_NEW_LINE );
244256 for (Exclude ex : filters ) {
245257 xml .append (OPENING_TAG_PREFIX ).append (EXCLUDE_ELEMENT_NAME );
@@ -264,7 +276,7 @@ private static void appendAttribute(String name, String value, StringBuilder xml
264276 }
265277
266278 private static void appendAlternatives (List <Class <?>> alternatives , List <Class <?>> stereotypes , StringBuilder xml ) {
267- if (alternatives .size () > 0 || stereotypes .size () > 0 ) {
279+ if (! alternatives .isEmpty () || ! stereotypes .isEmpty () ) {
268280 xml .append (OPENING_TAG_PREFIX ).append (ALTERNATIVES_ELEMENT_NAME ).append (TAG_SUFFIX_NEW_LINE );
269281 appendClasses (CLASS , alternatives , xml );
270282 appendClasses ("stereotype" , stereotypes , xml );
@@ -273,13 +285,19 @@ private static void appendAlternatives(List<Class<?>> alternatives, List<Class<?
273285 }
274286
275287 private static void appendSection (String name , String subName , List <Class <?>> classes , StringBuilder xml ) {
276- if (classes .size () > 0 ) {
288+ if (! classes .isEmpty () ) {
277289 xml .append (OPENING_TAG_PREFIX ).append (name ).append (TAG_SUFFIX_NEW_LINE );
278290 appendClasses (subName , classes , xml );
279291 xml .append (CLOSING_TAG_PREFIX ).append (name ).append (TAG_SUFFIX_NEW_LINE );
280292 }
281293 }
282294
295+ private static void appendTrimming (boolean trimArchive , StringBuilder xml ) {
296+ if (trimArchive ) {
297+ xml .append (OPENING_TAG_PREFIX ).append (TRIM_ELEMENT_NAME ).append (CLOSING_TAG_EMPTY );
298+ }
299+ }
300+
283301 private static void appendClasses (String name , List <Class <?>> classes , StringBuilder xml ) {
284302 for (Class <?> clazz : classes ) {
285303 xml .append (OPENING_TAG_PREFIX ).append (name ).append (TAG_SUFFIX ).append (clazz .getName ()).append (CLOSING_TAG_PREFIX )
0 commit comments