Announcement Announcement Module
Collapse
No announcement yet.
Exception in Spring AOP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception in Spring AOP

    Hi All,

    I am getting the below error while running my APP using TOMCAT.

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'supplyService' defined in ServletContext resource [/WEB-INF/conf/spring/sffs-aop-domain.xml]: Invocation of init method failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class it.freshfruits.domain.entity.FruitTypeImpl]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    	at
    My context file is given below:

    Code:
        <!--
            Enable @Configurable Annotation in conjunction with LTW jvm:
            (test) -javaagent:<path>/spring-agent.jar 
            (tomcat) <tomcat6>/lib/spring-tomcat-weaver.jar-->
        <context:load-time-weaver /> <!-- enable the use of META-INF/aop.xml -->
        <context:spring-configured />
        <context:annotation-config />
    
    
        <!-- Aspects  -->
            
        <bean id="cacheAspect" class="it.freshfruits.aspect.CacheAspect"/>
    
        <aop:config proxy-target-class="true">
            <aop:pointcut id="customerFactoryReadOperation" expression="execution(* it.freshfruits.domain.factory.CustomerFactoryImpl.get*(..))" /> 
            <aop:pointcut id="customerRepoReadOperation" expression="execution(* it.freshfruits.application.repository.CustomerRepositoryImpl.select*(..))" />              
            <aop:pointcut id="customerRepoInsertOperation" expression="execution(* it.freshfruits.application.repository.CustomerRepositoryImpl.insert*(..))" />            
            <aop:pointcut id="customerRepoUpdateOperation" expression="execution(* it.freshfruits.application.repository.CustomerRepositoryImpl.update*(..))" />        
            <aop:pointcut id="customerRepoDisableOperation" expression="execution(* it.freshfruits.application.repository.CustomerRepositoryImpl.disable*(..))" />                          
            <aop:pointcut id="fruitReadOperation" expression="execution(* it.freshfruits.application.repository.FruitTypeRepositoryImpl.get*(..))" />
            <aop:pointcut id="fruitInsertOperation" expression="execution(* it.freshfruits.application.repository.FruitTypeRepositoryImpl.insert*(..))" />
            <aop:pointcut id="fruitUpdateOperation" expression="execution(* it.freshfruits.application.repository.FruitTypeRepositoryImpl.update*(..))" />  
    
            <aop:aspect id="customerAspect" ref="customerCacheAspect">
                <aop:around pointcut-ref="customerFactoryReadOperation" method="invoke" />
                <aop:around pointcut-ref="customerRepoReadOperation" method="invoke" />
                <aop:before pointcut-ref="customerRepoInsertOperation" method="flush" />
                <aop:before pointcut-ref="customerRepoUpdateOperation" method="flush" />
                <aop:before pointcut-ref="customerRepoDisableOperation" method="flush" />   
            </aop:aspect>
            
            <aop:aspect id="fruitAspect" ref="fruitCacheAspect">
                <aop:around pointcut-ref="fruitReadOperation" method="invoke" />
                <aop:before pointcut-ref="fruitInsertOperation" method="flush" />
                <aop:before pointcut-ref="fruitUpdateOperation" method="flush" />
            </aop:aspect>
            
        </aop:config>
        
        <!-- Aspects with schema -->
        
        <bean id="customerCacheAspect" class="it.freshfruits.aspect.CacheAspect" >
            <property name="cache">
                <bean id="customerCache" parent="cache">
                    <property name="cacheName" value="customerCache" />
                </bean>
            </property>
        </bean>
        
        <bean id="fruitCacheAspect" class="it.freshfruits.aspect.CacheAspect" >
            <property name="cache">
                <bean id="fruitCache" parent="cache">
                    <property name="cacheName" value="fruitCache" />
                </bean>
            </property>
        </bean>
        
        <bean id="customer" scope="prototype" class="it.freshfruits.domain.entity.CustomerImpl"
            p:orderRepository-ref="orderRepository" p:customerRepository-ref="customerRepository"/>
        
        <bean id="fruitType"  scope="prototype" class="it.freshfruits.domain.entity.FruitTypeImpl"/>
        
        <bean id="order"  scope="prototype" class="it.freshfruits.domain.entity.OrderImpl"/>
            
        <bean id="supplyService" class="it.freshfruits.domain.service.SupplyServiceImpl" init-method="init"/>
            
    </beans>
    Service class here:

    Code:
    public class SupplyServiceImpl implements SupplyService {
    
        public SupplyServiceImpl() {
            this.availableItems = new HashMap<String, QuantityAndItemVO>();
            this.reservedItems = new HashMap<String, List<OrderItem>>();
        }
    
        public void init() {
            FruitType fruit = new FruitTypeImpl.Builder("orange", new Integer(2), new BigDecimal("0.20")).build();
            OrderItem item = new OrderItemImpl.Builder(fruit, 400, "1").build();
            availableItems.put(item.getFruitType().getId().toString(), new QuantityAndItemVO(item));
            FruitType fruitTwo = new FruitTypeImpl.Builder("lemon", new Integer(3), new BigDecimal("0.15")).build();
            OrderItem itemTwo = new OrderItemImpl.Builder(fruitTwo, 350, "1").build();
            availableItems.put(itemTwo.getFruitType().getId().toString(), new QuantityAndItemVO(itemTwo));
        }
    
        public Map<String, QuantityAndItemVO> getItemsAvailable() {
            return availableItems;
        }
    
        public Map<String, List<OrderItem>> getReservedItems() {
            return reservedItems;
        }
    
        public Boolean isAvailable(OrderItem item) {
            return availableItems.containsKey(item.getFruitType().getId().toString());
        }
    
        public Boolean release(String idOrder, String idItem) {
            Boolean result = false;
    
            List<OrderItem> listItems = reservedItems.get(idOrder);
    
            if (listItems != null && listItems.size() > 0) {
    
                for (int index = 0; index < listItems.size(); index++) {
    
                    OrderItem item = listItems.get(index);
                    if (item.getFruitType().getId().toString().equals(idItem)) {
                        listItems.remove(item);
                        QuantityAndItemVO qat = availableItems.get(idItem);
                        qat.add(item.getQuantity());
                        availableItems.put(idItem, qat);
                        reservedItems.put(idOrder, listItems);
                        result = true;
                    }
                }
            }
            return result;
        }
    
        public Boolean retainItem(OrderItem item) {
    
            Boolean result = false;
    
            QuantityAndItemVO qat = availableItems.get(item.getFruitType().getId().toString());
    
            if (qat != null) {
    
                if (qat.getQuantity() >= item.getQuantity()) {
    
                    List<OrderItem> items = reservedItems.get(item.getIdOrder().toString());
                    if (items == null) {
                        items = new ArrayList<OrderItem>();
                    }
                    items.add(item);
                    reservedItems.put(item.getIdOrder().toString(), items);
                    qat.setQuantity(qat.getQuantity() - item.getQuantity());
                    availableItems.put(qat.getItem().getFruitType().getId().toString(), qat);
                    result = true;
                }
            }
            return result;
        }
    
        private Map<String, QuantityAndItemVO> availableItems;
        private Map<String, List<OrderItem>> reservedItems;
    }
    jars using :

    aspectjrt-1.6.2.jar
    aspectjweaver-1.6.2.jar
    spring.jar
    spring-aspects.jar

    Can anyone figure out what's going wrong?

    Thank You,
    Manoj

  • #2
    First off all why the sh*t load of pointcuts, why not write 2 one for customers one for fruits. Next to that have you actually READ the stacktrace.

    Code:
    Could not generate CGLIB subclass of class [class it.freshfruits.domain.entity.FruitTypeImpl]
    The problem lies in that class.

    Finally just a tought why are you writing your own caching solution, there are perfect caching solutions out there with ready to use implementations.

    Comment


    • #3
      Exception in Spring AOP

      Hi ,

      My FruitTypeImpl class is written below. still i donot understand why "Common causes of this problem include using a final
      class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null " error is throwing.
      constructors but no arguments were given


      Code:
      package it.freshfruits.domain.entity;
      
      import it.freshfruits.application.repository.FruitTypeRepository;
      import it.freshfruits.util.ValidationUtils;
      
      import java.io.Serializable;
      import java.math.BigDecimal;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Configurable;
      
      @Configurable(dependencyCheck = true)
      public class FruitTypeImpl implements FruitType, Serializable {
      
          @Autowired
          public void setFruitTypeRepository(FruitTypeRepository fruitTypeRepository) {
              this.fruitTypeRepository = fruitTypeRepository;
          }
      
          public void save() {
              if (id == 0) {
                  fruitTypeRepository.insert(this);
              } else {
                  fruitTypeRepository.update(this);
              }
          }
      
          public String getColor() {
              return color;
          }
      
          public String getFlavour() {
              return flavour;
          }
      
          public String getLocation() {
              return location;
          }
      
          public String getName() {
              return name;
          }
      
          public BigDecimal getPrice() {
              return price;
          }
      
          public Integer getId() {
              return id;
          }
      
          public String toString() {
              return new StringBuilder().append("\nid:").append(id).append("\nname:").append(name).append("\ncolor:").append(color).append("\nflavour:").append(flavour).append("\nlocation:").append(
                      location).toString();
          }
      
          public static class Builder {
              // Required parameters
              private String name = "";
              private Integer id = 0;
              private BigDecimal price;
      
              // optional parameters
              private String color = "";
              private String flavour = "";
              private String location = "";
              private FruitTypeRepository fruitTypeRepository;
      
              private void validatePrice(BigDecimal price) {
      
                  if (price == null || (price.compareTo(BigDecimal.ZERO) <= 0))
      
                      throw new IllegalArgumentException("price argument < 0 :" + price);
              }
      
              private void validateName(String name) {
                  if (name == null || name.length() != 0 && name.length() < 3)
                      throw new IllegalArgumentException("name argument < 3 :" + name);
              }
      
              private void validateColor(String color) {
                  if (color == null || color.length() != 0 && color.length() < 3)
                      throw new IllegalArgumentException("color argument < 3 :" + color);
              }
      
              private void validateFlavour(String flavour) {
                  if (flavour == null || flavour.length() != 0 && flavour.length() < 4)
                      throw new IllegalArgumentException("flavour argument < 4 :" + flavour);
              }
      
              private void validateLocation(String location) {
                  if (location == null || location.length() != 0 && location.length() < 3)
                      throw new IllegalArgumentException("location argument < 3 :" + location);
              }
      
              public Builder(String name, Integer id, BigDecimal price) {
                  ValidationUtils.validateId(id.toString());
                  validateName(name);
                  validatePrice(price);
                  this.name = name;
                  this.id = id;
                  this.price = price;
              }
      
              public Builder color(String val) {
                  validateColor(val);
                  color = val;
                  return this;
              }
      
              public Builder flavour(String val) {
                  validateFlavour(val);
                  flavour = val;
                  return this;
              }
      
              public Builder location(String val) {
                  validateLocation(val);
                  location = val;
                  return this;
              }
      
              public Builder fruitTypeRepository(FruitTypeRepository fruitTypeRepository) {
                  this.fruitTypeRepository = fruitTypeRepository;
                  return this;
              }
      
              public FruitTypeImpl build() {
                  return new FruitTypeImpl(this);
              }
          }
      
          private FruitTypeImpl(Builder builder) {
              id = builder.id;
              name = builder.name;
              price = builder.price;
              color = builder.color;
              flavour = builder.flavour;
              location = builder.location;
              fruitTypeRepository = builder.fruitTypeRepository;
          }
      
          private Integer id;
          private BigDecimal price;
          private String color, flavour, location, name;
          private static final long serialVersionUID = 4133562402526792290L;
          private FruitTypeRepository fruitTypeRepository;
      }

      Comment


      • #4
        I really should write a book on reading stacktraces.

        Your CGlib requires your classes to have a no-argument constructor, your class doesn't have one.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          .... there are perfect caching solutions out there with ready to use implementations.
          I used Spring Modules that provides a great alternative for declarative caching with @Cacheable annotation, etc, but is no longer maintained.

          For that, I'm trying to write my own caching system with Spring AOP using annotations and ehcache.

          Can you give me a current example, please?

          Thanks

          Comment


          • #6
            Take a look at code examples from chapter 6 from AspectJ in Action (sources downloadable from http://manning.com/laddad2). It uses @Cacheable along with OSCache. It can be easily modified to work with EHCache or any other caching solution.

            -Ramnivas

            Comment

            Working...
            X