当我在我的应用程序上下文中定义波纹管控制器时,我尝试使用它时会出现重复错误。
我的申请背景:
<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;
}
...
我收到以下错误:
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: {}
答案 0 :(得分:3)
您收到错误是因为在XML和注释中定义了相同的Controller,正如其他帖子所指出的那样。删除Controller的XML配置并执行以下操作:
由于成功和失败URL不是真正的配置值,而是在您的应用中定义,因此将1)直接移动到处理程序方法中作为硬编码值,或2)如果它们对多于1个方法是通用的或Controller,将它们作为常量移动到所有其他控制器扩展的BaseController中。然后你完全避免了这两个论点的DI问题。
离开baseCallbackUrl
。如果这是一个取决于应用程序URL的值,可以将该值设为系统变量并使用@Value
直接注入,如下所示:
@Value("#{systemProperties.baseCallbackUrl}")
private String baseCallbackUrl;
答案 1 :(得分:2)
我认为组件扫描正在加载Controller并且你的bean再次被加载,因为它是在xml(bean id="socialAuthWebController"
)中定义的。
你能尝试评论组件扫描或bean声明吗?
<context:component-scan base-package="org.brickred.socialauth.spring.controller" />
答案 2 :(得分:0)
也许尝试声明值的名称。见Spring Documentation
<constructor-arg name="applicationUrl" value="http://www.mysite.com/" />
我认为Spring可能遇到了问题,因为它不知道哪个String在哪里。所以你可以使用“名字”或“索引”让它知道。
从链接:“如果bean定义的构造函数参数中不存在潜在的歧义,那么在bean定义中定义构造函数参数的顺序是这些参数在适当的构造函数中提供的顺序。 bean被实例化“
由于您使用的是所有字符串,因此存在歧义。