2

I have built a web project using spring framework and according to that i have added jar to it. But when I'm going to execute that it gives following error. Please provide me solution for this error.

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController' defined in file [D:\WebsiteDemo\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\loginexample\WEB-INF\classes\com\login\controller\LoginController.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.methodSecurityMetadataSourceAdvisor': Cannot resolve reference to bean 'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0': 1 constructor arguments specified but no matching constructor found in bean 'org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

My dispatcher file is

  <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<context:component-scan base-package="com.login.controller"/>
<mvc:annotation-driven />

<security:global-method-security secured-annotations="enabled"/>

<security:http auto-config="true">
<security:intercept-url pattern="/main*" access="ROLE_ADMIN,ROLE_REGULAR_USER" />
<security:form-login login-page="/login" default-target-url="/main"
authentication-failure-url="/loginError"/>
</security:http>

<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="alpha" password="pass1" authorities="ROLE_ADMIN" />
<security:user name="beta" password="pass2" authorities="ROLE_REGULAR_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>

LoginController code

package com.login.controller;

import java.security.Principal;

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

    @RequestMapping(value="/main", method = RequestMethod.GET)
    public String printWelcome(ModelMap model, Principal principal ) {
        String name = principal.getName();
        model.addAttribute("username", name);
        return "main_page";
        }

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login(ModelMap model) { 
        return "login_page"; 
    }

    @RequestMapping(value="/loginError", method = RequestMethod.GET)
        public String loginerror(ModelMap model) {
        model.addAttribute("error", "true");
        return "login_page";
    }


    @Secured({"ROLE_REGULAR_USER","ROLE_ADMIN"})
    @RequestMapping(value="/common", method = RequestMethod.GET)
    public String common(ModelMap model) {
        return "common_page";
    }

    @RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {
        return login(model); 
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • You have a bean defined and want to instantiate it with one argument, but there is no matching constructor. So please share your bean definition with us and the relevant portions of the code as well as the complete stacktrace. – hotzst Oct 07 '15 at 12:16
  • i have not use any bean for this project. I am just doing simple login program with hard coded username and password. – Ghanshyam Mule Oct 07 '15 at 12:27
  • `com.login.controller.LoginController` looks like a bean to me and you should define component scan with the package and not the class name. Otherwise you can ommit the `context:component-scan` and define the controller as a bean. And you should add ``. See http://stackoverflow.com/questions/13661985/spring-mvc-difference-between-contextcomponent-scan-and-annotation-driven – hotzst Oct 07 '15 at 12:57
  • Still i m facing following error org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController' defined in file . i did change suggested by you – Ghanshyam Mule Oct 07 '15 at 13:30
  • Could you update your question with the changes you have made and also post the code of the class `LoginController` – hotzst Oct 07 '15 at 13:33
  • now i have done the changes in above question – Ghanshyam Mule Oct 07 '15 at 13:39
  • You get the error about `loginController` not defined because you are still missing ``. This element will allow Spring to scan for beans that are annotated with `@Controller` in the packages you defined in you component-scan. – hotzst Oct 07 '15 at 13:43
  • i have been used in dispatcher file still facing above error mentioned in question – Ghanshyam Mule Oct 08 '15 at 05:44
  • So I guess the configuration now is ok. Can you add the code of your `LoginController`? – hotzst Oct 08 '15 at 06:05
  • If you remove the `component-scan` from your bean configuration and instead define the `LoginController` as a bean as described [here](https://lessonsincode.wordpress.com/2012/07/10/explicitly-defining-a-spring-mvc-annotation-based-controller/), does that make a difference? – hotzst Oct 08 '15 at 06:16
  • I m not getting you,what you are saying? – Ghanshyam Mule Oct 08 '15 at 06:24
  • Remove `` and add a bean definition for `LoginController` like in the link. That way the controller is not detected through scanning the packages, but directly by declaring the controller in your dispatcher file. – hotzst Oct 08 '15 at 07:11
  • can u please provide code for that – Ghanshyam Mule Oct 08 '15 at 07:27

1 Answers1

0

Do not pick up the LoginController through component scan but define it in the bean definition xml (you name it dispatcher file):

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<mvc:annotation-driven />

<!-- Controller bean definitions -->

<bean id="loginController" class="com.login.controller.LoginController" />

<security:global-method-security secured-annotations="enabled"/>

<security:http auto-config="true">
<security:intercept-url pattern="/main*" access="ROLE_ADMIN,ROLE_REGULAR_USER" />
<security:form-login login-page="/login" default-target-url="/main"
authentication-failure-url="/loginError"/>
</security:http>

<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="alpha" password="pass1" authorities="ROLE_ADMIN" />
<security:user name="beta" password="pass2" authorities="ROLE_REGULAR_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>
hotzst
  • 7,238
  • 9
  • 41
  • 64