Announcement Announcement Module
Collapse
No announcement yet.
How to create the a proxied object in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create the a proxied object in Spring

    I have created my daos using spring factory method getDAO(). In getDAO() I am creating the userDAO instance using Java Reflection by calling appropriate constructor and I keep it in a HashMap.
    Below is the config

    <bean id="userDao" factory-bean="schema1DaoFactory" factory-method="getDAO">
    <constructor-arg value="map" />
    <constructor-arg ref="engLocale" />
    </bean>

    I am using the @Transactional annotation in the userDAO. Now the issue is that my trasanction does not work as the object userDAO is not a proxied Spring object. So how can I create a proxied object for @Transactional annotation after the object has been created as a typical Java object.
    Is BeanPostProcessor is of help, if yes then how. Please help.

  • #2
    Code:
    package your.package;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
    import org.springframework.beans.factory.config.BeanDefinition;
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.beans.factory.support.GenericBeanDefinition;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.util.ClassUtils;
    
    public class Schema1DaoFactory implements ApplicationContextAware {
        ApplicationContext applicationContext;
        private BeanDefinitionRegistry registry;
    
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if (!ClassUtils.isAssignableValue(BeanDefinitionRegistry.class, applicationContext)) {
                throw new BeanNotOfRequiredTypeException("allicationContext", BeanDefinitionRegistry.class, applicationContext.getClass());
            } else {
                this.applicationContext = applicationContext;
                registry = (BeanDefinitionRegistry) applicationContext;
            }
        }
    
        public UserDao getDao() {
            GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
            beanDefinition.setBeanClass(UserDao.class);
            beanDefinition.setLazyInit(false);
            beanDefinition.setAbstract(false);
            beanDefinition.setAutowireCandidate(true);
            //determine bean's scope
            beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
    
            //get bean's constructor from somewhere
            Object constructor0 = new Object();
            beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, constructor0);
            //Object constructor1....
            //add more construtor ....
    
            Object propertyObject = new Object();
            beanDefinition.getPropertyValues().add("someproperty", propertyObject);
            //add more properties
    
            //register userdao definition
            registry.registerBeanDefinition("userDao", beanDefinition);
            UserDao result = applicationContext.getBean("userDao", UserDao.class);
            //remove definition if no more required.
            registry.registerBeanDefinition("userDao", beanDefinition);
            return result;
        }
    }

    Comment


    • #3
      Thanks Kent for replying,

      But my issue is that I have already created an object calling a typical constructor in my app. Now I wanted to create a proxy over it for my @transactional tag to work. I hope you got it. I can understand that by calling the getBean() on application context it creates a proxy object. But I need to create proxied object over already created object instance.
      Thanks,
      Jayesh

      Comment

      Working...
      X