2017-08-06 6 views
0

AdminService.javaSpring Beanの作成エラーをキャッチするには?

package service; 

import java.awt.Window.Type; 
import java.util.HashMap; 

import javax.servlet.http.HttpSession; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 
import org.springframework.stereotype.Service; 


import dao.IMemberDAO; 
import model.Member; 

@Service 
public class MemberService 
{ 
    @Autowired 
    private IMemberDAO memberDao; 

    // 로그인 
    public HashMap<String, Object> login(String id,String pw) 
    { 
     HashMap<String, Object> result = memberDao.selectOne(id); 

     if(result != null) // 존재하는 값이 있으면 
     { 
      String opwd = (String) result.get("pw"); // opwd = 존재하는값의 pw값을 가져온 값 

      if(opwd.equals(pw)) 
      { 
       return result; // true면 존재하는값을 반환 
      } 
      else 
      { 
       return null; 
       //return null; // 아니면 값을 반환하지 않음. 
      } 
     } 
     else // 존재하는 값이 없다면 
     { 
      return null; 
     } 
    } 

    // 아이디 체크 
    public boolean idCheck(String loginPerson) 
    { 
     HashMap<String, Object> user = memberDao.selectOne(loginPerson); 

     if(user != null) 
     { 
      return false; 
     } 
     else 
     { 
      return true; 
     } 
    } 

    // 멤버 추가 
    public boolean insertMember(Member member) 
    { 
     if(idCheck(member.getId())) 
     { 
      memberDao.insertMember(member); 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    public HashMap<String, Object> getMemberInfo(String id) 
    { 
     return memberDao.selectOne(id); 
    } 

    // 회원 수정 
    public void memberUpdate(HashMap<String, Object> params) 
    { 
     System.out.println("params is : " + params); 
     memberDao.updateMember(params); 
    } 

    // 회원삭제 
    public boolean memberDelete(String id,String pw) 
    { 
     HashMap<String, Object> user = memberDao.selectOne(id); 

     String pw2 = (String) user.get("pw"); 
     System.out.println("id and pw is : " + id + "/" + pw); 
     System.out.println("pw2 is : " + pw2); 


     if(pw2.equals(pw)) 
     { 
      memberDao.deleteMember(id); 
      System.out.println("return true"); 
      return true; 
     } 
     else 
     { 
      System.out.println("return false"); 
      return false; 
     } 
    } 
} 

AdminController.java

package controller; 

import java.io.IOException; 
import java.util.HashMap; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.servlet.ModelAndView; 

import service.AdminService; 
import service.MemberService; 

@Controller 
public class AdminController 
{ 
    @Autowired 
    public AdminService aService; 

    // 관리자 로그인 폼 페이지 
    @RequestMapping("admin.do") 
    public String adminLoginPage() 
    { 
     return "adminLoginPage"; 
    } 

    // 관리자 로그인했을 시 요청 
    @RequestMapping("adminLoginOK.do") 
    @ResponseBody 
    public String adminMainPage(@RequestParam(required=false) String id, @RequestParam(required=false)String pw,HttpSession session,HttpServletRequest req,HttpServletResponse resp) 
    { 
     HashMap<String, Object> adminLoginIdentify = aService.adminLogin(id, pw); 

     //if(session.getAttribute("id") == null){System.out.println("zzz kkk");} 
     //String admin_id = (String) session.getAttribute("id"); 

     if(adminLoginIdentify != null) 
     { 
      return "1"; 
     } 
     else 
     { 
      return "0"; 
     } 
    } 

    @RequestMapping("adminPage.do") 
    public String adminPage(
      @RequestParam(required = false) String keyword, 
      @RequestParam(defaultValue="0") int type, 
      HttpSession session,HttpServletRequest resquest,HttpServletResponse response) throws IOException 
    { 
     ModelAndView mav = new ModelAndView(); 
     HashMap<String, Object> params = new HashMap<String, Object>(); 

     params.put("type", type); 
     params.put("keyword", keyword); 
     System.out.println(params); 


     return "adminMainPage"; 
    } 
} 

adminDaoMapper.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper 
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

<mapper namespace="dao.IAdminDAO"> 
    <select id="selectOne" parameterType="Strig" resultType="java.util.HashMap"> 
     select * from member where id = #{id} 
    </select> 

    <select id="selectMemberAll" resultType="java.util.HashMap"> 
     select * from member 
    </select> 
</mapper> 

AdminDao.java

package dao; 

import java.util.HashMap; 

public interface IAdminDAO 
{ 
    public HashMap<String, Object> selectOne(String id); 
} 

コードMOD前applicationContextファイルが存在する場合は以下のようになります。

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" 
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 

    <context:component-scan base-package="service" /> 

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> 
     <property value="com.mysql.jdbc.Driver" name="driverClassName"></property> 
     <property value="jdbc:mysql://localhost/rachelvf" name="url"></property> 
     <property value="root" name="username"/> 
     <property value="mysql" name="password"/> 
    </bean> 

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"></property> 
     <property name="mapperLocations" value="classpath*:dao/mapper/*.xml"></property> 
    </bean> 

    <bean id="memberDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
     <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 
     <property name="mapperInterface" value="dao.IMemberDAO"></property> 
    </bean> 

</beans> 

これは変更後のアプリケーションコードです。

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" 
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 
    <context:component-scan base-package="service" /> 

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> 
     <property value="com.mysql.jdbc.Driver" name="driverClassName"></property> 
     <property value="jdbc:mysql://localhost/rachelvf" name="url"></property> 
     <property value="root" name="username"/> 
     <property value="mysql" name="password"/> 
    </bean> 


    <bean class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <property name="mapperLocations" value="classpath:dao/mapper/*.xml"></property> 
     <property name="typeAliasesPackage" value="model"></property> 
     <property name="dataSource" ref="dataSource"></property> 
    </bean> 
</beans> 

プロジェクトは、ApplicationContextのファイルが変更される前に、よく働い が、コードが変更された後に、エラーが発生します。

エラーコードです。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.IAdminDAO service.AdminService.adminDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.IAdminDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 

私は 、エラーの原因について考えたが、私は、サービスの注釈を挿入していなかったので、それはあると思います。

しかし、何らかの形での入力ミスはなく、すべて正しく書き込まれ、エラーが発生します。私が知らないことがありますか?

このエラーの原因を教えてください。

解決策はどうですか?これに

@Autowired 
public AdminService aService; 

答えて

1

があなたのこのコードを変更してみてください...私を助けてください

@Autowired 
public AdminService adminService; 

あなたがAutowire、彼はaService上を見ることができないときので、あなたのコンテキストは、彼がService beanのデフォルト名としてadminServiceのインスタンスを作成できない理由です。

EDIT:

もう一つは、あなたがインターフェースは覚えてインスタンス化することはできません、豆をインスタンス化するために、具体的なクラスに、インターフェイスを実装する必要がされているので、あなたがあなたの上にあるものを実装するための具体的な豆を必要としますインターフェイス。このように:

@Repository 
public class IAdminDAOImpl implements IAdminDAO { 
    //Implementation here 
} 

@Service 
public class AdminServiceImpl implements AdminService { 
    //Implementation here 
} 
+0

私が言ったようにコードを変更してプロジェクトを実行しようとしましたが、エラーが発生しました。 @ LKTN.25 –

+1

それは同じエラーですか? – msagala25

+0

はい、同じエラーです。私は何を確認すべきですか? –

関連する問題