Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring annotated controller bean duplicate in application context

When I define bellow controller in my app context I'm getting duplicate errors when I try to use it.

How do I pass the constructor-args to the controller without getting duplicate error messages?

My application context:

 <context:component-scan base-package="org.brickred.socialauth.spring.controller" />

 <bean id="socialAuthWebController" class="org.brickred.socialauth.spring.controller.SocialAuthWebController">
    <constructor-arg value="http://www.mysite.com/" />
    <constructor-arg value="/authSuccess.html" />
    <constructor-arg value="/failed.html" />
 </bean>

 <tx:annotation-driven transaction-manager="txManager"/>

The annotated controller:

    @Controller
    @RequestMapping("/socialauth")
    public class SocialAuthWebController {

    private String baseCallbackUrl;
    private String successPageURL;
    private String accessDeniedPageURL;
    @Autowired
    private SocialAuthTemplate socialAuthTemplate;
    @Autowired
    private SocialAuthManager socialAuthManager;
    private final Log LOG = LogFactory.getLog(getClass());

    /**
     * Constructs a SocialAuthWebController.
     * 
     * @param applicationUrl
     *            the base URL for this application (with context e.g
     *            http://opensource.brickred.com/socialauthdemo, used to
     *            construct the callback URL passed to the providers
     * @param successPageURL
     *            the URL of success page or controller, where you want to
     *            access sign in user details like profile, contacts etc.
     * @param accessDeniedPageURL
     *            the URL of page where you want to redirect when user denied
     *            the permission.
     */
    @Inject
    public SocialAuthWebController(final String applicationUrl,
                    final String successPageURL, final String accessDeniedPageURL) {
            this.baseCallbackUrl = applicationUrl;
            this.successPageURL = successPageURL;
            this.accessDeniedPageURL = accessDeniedPageURL;
    }
    ...

rest of the source code at http://code.google.com/p/socialauth/source/browse/trunk/socialauth-spring/src/org/brickred/socialauth/spring/controller/SocialAuthWebController.java

I get following error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'socialAuthWebController' defined in URL [jar:file://Tomcat%207.0/webapps/ROOT/WEB-INF/lib/socialauth-spring-2.0-beta2.jar!/org/brickred/socialauth/spring/controller/SocialAuthWebController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: : No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

like image 212
MatBanik Avatar asked Dec 10 '25 14:12

MatBanik


1 Answers

You're getting the error because the same Controller is defined in both XML and annotations, as other posts have noted. Remove the Controller's XML config and do the following:

Since the success and failure URLs are not really configuration values, but are defined in your app, move those either 1) directly into the handler method as hard-coded values, or 2) if they are common to more than 1 method or Controller, move them as constants into a BaseController which all other Controllers extend. Then you avoid the DI issues for those two arguments altogether.

That leaves the baseCallbackUrl. If this is a value which is dependent on the application's URL, maybe make that value a system variable and directly inject it using @Value, as follows:

@Value("#{systemProperties.baseCallbackUrl}")
private String baseCallbackUrl;
like image 178
atrain Avatar answered Dec 12 '25 04:12

atrain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!