View Javadoc

1   /*
2    * $Id: ClassReloadingInstantiationStrategy.java 793388 2009-07-12 18:17:55Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.spring;
22  
23  import org.springframework.beans.factory.support.SimpleInstantiationStrategy;
24  import org.springframework.beans.factory.support.RootBeanDefinition;
25  import org.springframework.beans.factory.BeanFactory;
26  import org.springframework.beans.BeanInstantiationException;
27  import org.springframework.beans.BeanUtils;
28  
29  import java.lang.reflect.Constructor;
30  
31  /***
32   * Same as SimpleInstantiationStrategy, but constructor is not cached
33   */
34  public class ClassReloadingInstantiationStrategy extends SimpleInstantiationStrategy {
35      public Object instantiate(
36              RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) {
37  
38          // Don't override the class with CGLIB if no overrides.
39          if (beanDefinition.getMethodOverrides().isEmpty()) {
40              Class clazz = beanDefinition.getBeanClass();
41              if (clazz.isInterface()) {
42                  throw new BeanInstantiationException(clazz, "Specified class is an interface");
43              }
44              try {
45                  Constructor constructor = clazz.getDeclaredConstructor((Class[]) null);
46                  return BeanUtils.instantiateClass(constructor, null);
47              }
48              catch (Exception ex) {
49                  throw new BeanInstantiationException(clazz, "No default constructor found", ex);
50              }
51          } else {
52              // Must generate CGLIB subclass.
53              return instantiateWithMethodInjection(beanDefinition, beanName, owner);
54          }
55      }
56  }