Spring注释的控制器bean在应用程序上下文中重复

发布于 2024-12-06 03:56:52 字数 2925 浏览 3 评论 0原文

当我在应用程序上下文中定义以下控制器时,当我尝试使用它时,我会收到重复的错误。

如何将构造函数参数传递给控制器​​而不收到重复的错误消息?

我的应用程序上下文:

 <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"/>

带注释的控制器:

    @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;
    }
    ...

其余源代码位于 http://code.google.com/p/socialauth/source/browse/trunk/socialauth-spring/src/org/brickred/socialauth/spring/controller/SocialAuthWebController.java

我收到以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建 URL 中定义的名为“socialAuthWebController”的 bean 时出错[jar:file://Tomcat%207.0/webapps/ROOT/WEB-INF/lib/socialauth-spring-2.0-beta2.jar!/org/brickred/socialauth/spring/controller/SocialAuthWebController.class]:表达了不满足的依赖关系通过索引为 0 类型 [java.lang.String] 的构造函数参数::找不到依赖项的匹配类型 [java.lang.String] 的 bean:预计至少为 1 bean 符合此依赖项的自动装配候选者条件。依赖注解:{};嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项的 [java.lang.String] 类型的匹配 bean:预计至少有 1 个有资格作为此依赖项的自动装配候选者的 bean。依赖注释:{}

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: {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪痕残 2024-12-13 03:56:52

正如其他帖子所述,您收到错误是因为 XML 和注释中定义了相同的控制器。删除控制器的 XML 配置并执行以下操作:

由于成功和失败 URL 并不是真正的配置值,而是在您的应用程序中定义的,因此请将它们移至 1) 作为硬编码值直接到处理程序方法中,或 2) 如果它们对于超过 1 个方法或控制器来说是通用的,将它们作为常量移动到所有其他控制器扩展的 BaseController 中。然后你就可以完全避免这两个参数的 DI 问题。

这就剩下了baseCallbackUrl。如果这是一个依赖于应用程序 URL 的值,则可以将该值设为系统变量并使用 @Value 直接注入它,如下所示:

@Value("#{systemProperties.baseCallbackUrl}")
private String baseCallbackUrl;

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;
貪欢 2024-12-13 03:56:52

我认为组件扫描正在加载控制器,并且您的 bean 会再次加载,因为它是在 xml 中定义的(bean id="socialAuthWebController")。

您可以尝试注释组件扫描或 bean 声明吗?

I think component scan is loading the Controller and you bean is loaded again because it is defined in the xml (bean id="socialAuthWebController").

Can you try commenting either component-scan or bean declaration?
<context:component-scan base-package="org.brickred.socialauth.spring.controller" />

神回复 2024-12-13 03:56:52

也许尝试声明值的名称。请参阅 Spring 文档

<constructor-arg name="applicationUrl" value="http://www.mysite.com/" /> 

我认为 Spring 可能有问题,因为它不知道哪个字符串去了哪里。所以你可以使用“名称”或“索引”来让它知道。

来自链接:“如果 bean 定义的构造函数参数中不存在潜在的歧义,则在 bean 定义中定义构造函数参数的顺序就是当 bean 被调用时将这些参数提供给适当的构造函数的顺序。正在实例化”

由于您使用的是所有字符串,因此存在歧义。

Maybe try declaring the name of the values. See Spring Documentation

<constructor-arg name="applicationUrl" value="http://www.mysite.com/" /> 

I think Spring might be having a problem because it doesn't know which String goes where. So you could use "name" or "index" to let it know.

From the link: "If no potential ambiguity exists in the constructor arguments of a bean definition, then the order in which the constructor arguments are defined in a bean definition is the order in which those arguments are supplied to the appropriate constructor when the bean is being instantiated"

There is an ambiguity since you are using all Strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文