P
P
P_Alexander2018-06-20 22:22:46
Java
P_Alexander, 2018-06-20 22:22:46

How to solve org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController'?

Please help me to solve this error, I don't know what to do anymore, Any help is appreciated!!!!!
The stack trace says there is a problem with the userService field in the controller, but I don’t understand what I need to do with it!
config files
app-config string

<import resource="spring-servlet.xml"/>
        <import resource="spring-security.xml"/>
        <import resource="db-config.xml"/>

    <context:component-scan base-package="com.*"/>
    <context:annotation-config/>
    <context:property-placeholder location="classpath:application.properties"/>

db-config
<context:component-scan base-package="com"/>
    <context:property-placeholder location="classpath:application.properties"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>

    <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="dataSource" ref="dataSource"/>
        <!--<property name="persistenceUnitName" value="myJpaPersistenceUnit"/>-->
        <property name="packagesToScan">
            <list>
                <value>com.model</value>
            </list>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryBean"/>
    </bean>
    <tx:annotation-driven/>

spring security
<http auto-config="true">
        <intercept-url pattern="/" access="hasAnyRole('none', 'user', 'admin')"/>
        <intercept-url pattern="/main" access="hasAnyRole('none', 'user', 'admin')"/>
        <intercept-url pattern="/home" access="hasAnyRole('admin', 'user')"/>

        <form-login login-page="/authorization" default-target-url="/home"
                    authentication-failure-url="/authorization?error"
                    username-parameter="login" password-parameter="password"/>

        <logout logout-success-url="/authorization?logout" />
    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="userDetailsServiceImpl">
            <password-encoder ref="encoder"></password-encoder>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="userDetailsServiceImpl" class="com.service.security.UserDetailsServiceImpl">
    </beans:bean>

    <beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <beans:constructor-arg name="strength" value="11"/>
    </beans:bean>

spring-servlet
<context:component-scan base-package="com.controller"/>
    <mvc:annotation-driven/>
    <context:annotation-config/>
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:validation</value>
            </list>
        </property>
    </bean>

    <bean id="templateResolve" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML"/>
        <property name="cacheable" value="false"/>
    </bean>

    <bean id="templateEngine"
          class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolve"/>
        <property name="additionalDialects">
            <set>
                <bean class="nz.net.ultraq.thymeleaf.LayoutDialect"/>
            </set>
        </property>
    </bean>

    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="order" value="1" />
        <property name="viewNames" value="*" />
    </bean>

UserController.class
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserValidator userValidator;

    @RequestMapping(value = "/registration" ,method = RequestMethod.GET)
    public String registration(Model model){
        model.addAttribute("userForm", new User());
        System.out.println("regstration");
        return "registration";
    }

    @RequestMapping(value = "/registration" ,method = RequestMethod.POST)
    public String createNewUser(@ModelAttribute("userForm") User user, BindingResult bindingResult, Model model){
        userValidator.validate(user, bindingResult);

        if(bindingResult.hasErrors()){
            return "registration";
        }

        userService.saveUser(user);

        securityService.autoLogin(user.getLogin(), user.getPasswordTwo());
        System.out.println("registration");
        return "redirect:/home";
    }

    @RequestMapping(value = "/authorization" ,method = RequestMethod.GET)
    public String authorization(Model model, String error, String logout){
        if (error != null){
            model.addAttribute("error", "Your username and password is invalid.");
        }

        if (logout != null)
            model.addAttribute("message", "You have been logged out successfully.");

        return "authorization";
    }


    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(Model model){
        model.addAttribute("message", "Hello home");
        System.out.println("home");
        return "home";
    }

}

UserService.class
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private RoleDAO roleDAO;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public User findById(int id) {
        return userDAO.findById(id);
    }

    @Override
    public User findByLogin(String login) {
        User user = userDAO.findByLogin(login);
        return user;
    }

    @Override
    public User findByEmail(String email) {
        User user = userDAO.findByEmail(email);
        return user;
    }

    @Override
    public void saveUser(User user) {
        user.setDate(LocalDate.now());
        Role role = roleDAO.findByRoleName("user");
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        user.setRole(role);
        role.getUsers().add(user);
        userDAO.save(user);
    }

    @Override
    public void updateUser(User user) {
        userDAO.updateUser(user);
    }

    @Override
    public void deleteUser(String login) {
        userDAO.deleteByLogin(login);
    }

    @Override
    public List<User> findAllUsers() {
        return userDAO.findAllUsers();
    }

    @Override
    public boolean isUserLoginUnique(Integer id, String login) {
        User user = findByLogin(login);
        return (user == null || ((id != null) && (user.getId() == id)));
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2018-06-21
@P_Alexander

We need to go deeper
https://stackoverflow.com/questions/20333147/autow...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question