0

Actually there are two question.

1) How to replace below code with @autowired annotation in spring mvc 3 where I want to pass dynamic value to constructor parameter from controller class.

EncDec lEncDec = (EncDec) context.getBean("encDec", new Object[]{EncDec.getMD5Hash(Id)});


normal java code would be EncDec lEncDec = new EncDec(EncDec.getMD5Hash(Id))

2) @service and @repository throws exception between I declare them in it is working fine but I want to remove dependency from xml

org.springframework.beans.factory.BeanCreationException: Error creating bean with name

I have declared my class in dispatch-servlet.xml

<context:component-scan base-package="com.controller,com.serviceimpl,com.daoimpl" />

Please see this mkyong example for question 2

StackTrace

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SAController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.esociety.service.SuperAdminDetailsManager com.esociety.controller.SAController.superAdminDetailsManager; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'superAdminDetailsManagerImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.esociety.dao.SuperAdminDetailsDao com.esociety.serviceimpl.SuperAdminDetailsManagerImpl.superAdminDetailsDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.esociety.dao.SuperAdminDetailsDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
pise
  • 849
  • 6
  • 24
  • 51
  • Why do you want to use `@Autowired` what is wrong with the plain java code? Not everything has to be a bean... – M. Deinum Jul 10 '14 at 10:25
  • because I read some where on net that it is not a good practice to use context.getBean and is it ok to go with new Object(); – pise Jul 10 '14 at 10:29
  • As I mentioned before not everything needs to be a bean, I still don't get why people thing when using Spring or CDI that everything needs to be injected... There is nothing wrong with `new`. – M. Deinum Jul 10 '14 at 10:31
  • @M.Deinum Ok, just asking you what if someone wants to use the same thing in spring. value annotation is there which can be used. Please see this link I didn't understood much so I posted this question. [link](http://stackoverflow.com/questions/6739566/anyway-to-autowire-a-bean-that-requires-constructor-arguments) – pise Jul 10 '14 at 10:37
  • If you want to do custom construction you need access to the `BeanFactory` which you could hide behind a global factory. But if you have the same piece of code in multiple places I would assume yuo already had a helper/factory/utility method for it. And then again not everything has to be a bean. Normal OO still applies although people tend to forget about that. – M. Deinum Jul 10 '14 at 10:40
  • @M.Deinum any idea regarding org.springframework.beans.factory.BeanCreationException: Error creating bean with name. I have declared service annotation, controller annotation and repository annotation in service,controller and dao class respectively – pise Jul 10 '14 at 11:01
  • Not without a full stacktrace and those kind of questions have been answered before. – M. Deinum Jul 10 '14 at 11:18
  • @M.Deinum. Yes I searched on net but they are saying about bean id in applicationcontext.xml but in mkyong tutorial he has used component annotation for auto scanning to remove bean id in xml file. But the same is not working for me and I have posted stack trace. Thanks – pise Jul 10 '14 at 12:38

0 Answers0