2017-01-16 80 views
-1

我是春天的初學者,我在春天有一個dao類,但註解@ auto wired或@ service不起作用,我通過在應用程序上下文中創建一個bean來解決問題,什麼是用於註釋在彈簧不工作的原因·提供了「背景:組分掃描基包=」也但註釋不工作註解在春天不工作

 StudentDao 

    public interface StudentDao { 
     public int addStudent(StudentEntity s); 
    } 
    ----------------------------------- 
    @Service("studentDaoImpl") 
    public class StudentDaoImpl implements StudentDao{ 
    @PersistenceContext 
    private EntityManager em; 
    @Override 
    @Transactional 
    public int addStudent(StudentEntity student) { 
    // TODO Auto-generated method stub 
    em.persist(student); 
    return student.getId(); 
    } 
} 
    ------------------------------------------------ 
FascadeDaoImpl 
public class FascadeControllerImpl implements FascadeController { 
// @Autowired 
private StudentDao studentDao; 
private UserContext uc; 

public void studentDao() { 
    studentDao=(StudentDao) uc.getContext().getBean("studentDao"); 

    } 
} 

public int addStudent(StudentEntity student) { 
    studentDao(); 
    return studentDao.addStudent(student); 
} 

的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:p="http://www.springframework.org/schema/p" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-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/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

<context:component-scan base-package="sms.spring.dao" /> 
<context:component-scan base-package="sms.spring.fascade" /> 
<context:component-scan base-package="sms.spring.entity" /> 
<context:annotation-config /> 


<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> 
<bean class="sms.spring.entity.StudentEntity" id="studentbean"/> 
<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName"> 
     <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> 
    </property> 
    <property name="url"> 
     <value>jdbc:sqlserver://localhost;databaseName=dbstsms</value> 
    </property> 
    <property name="username"> 
     <value>sa</value> 
    </property> 
    <property name="password"> 
     <value>sa123</value> 
    </property> 
</bean> 
<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> 
    <property name="persistenceUnitName" value="PERSUnit" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
    </property> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
     </props> 
    </property> 
</bean> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

<bean id="fascadeController" class="sms.spring.fascade.FascadeControllerImpl"></bean> 
<bean id="studentDao" class="sms.spring.dao.StudentDaoImpl"></bean> 
<bean id="loginDao" class="sms.spring.dao.LoginDaoImpl"></bean> 
<bean id="facultyDao" class="sms.spring.dao.FacultyDaoImpl"></bean> 
<bean id="markDao" class="sms.spring.dao.MarkDaoImpl"></bean> 
<bean id="notificationDao" class="sms.spring.dao.NotificationDaoImpl"></bean> 
<tx:annotation-driven /> 

+2

「不工作」不是我們可以幫助的問題的工作描述。 – GhostCat

+0

您正在嘗試將基於xml的配置與註釋混合使用。它也是可能的,但你需要照顧的事情。 很難用代碼來猜測問題,因爲你沒有共享所有的配置。 我會建議要麼只使用基於xml的配置或基於註釋的配置。 –

回答

1

請發表你得到,而且從您發佈我可以看到代碼,即使您已標註的錯誤帶有@service註釋的StudentDaoImpl類再一次在xml配置中定義相同,請使用基於註釋的配置或基於xml的配置。

請檢查您的文件有

  1. <context:annotation-config/>在XML配置(如果啓用了組件掃描沒必要)
  2. 檢查被提及,如果組件掃描爲包
  3. 請參閱啓用this答案爲正確的架構位置
+0

org.springframework.beans.factory.BeanCreationException:創建名爲'fascadebean'的bean時出錯:注入自動裝配依賴失敗;嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝入字段:private sms.spring.dao.StudentDaoImpl sms.spring.fascade.FascadeControllerImpl.studentDaoImpl;嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有符合條件的類型爲[sms.spring.dao的bean。StudentDaoImpl]找到依賴關係: – storm

+0

預計至少有1個bean符合此依賴關係的自動連線候選資格。依賴註釋:{org.springframework.beans.factory.annotation.Autowired(req uired = true)@Optimus – storm

+0

剛剛意識到您正在搜索名爲studentDao的bean,但是在基於註解的配置中,您已經給出了bean的名稱作爲studentDaoImpl,由於基於註釋的配置覆蓋了基於xml的配置,所以你可能會遇到這個問題,你可以將'studentDao =(StudentDao)uc.getContext()。getBean(「studentDao」);' 改爲'studentDao =(StudentDao )uc.getContext()。getBean(「studentDaoImpl」);' – Optimus

1

問題不清楚,但它看起來像你的控制器也應註明@Controller。屬於Spring項目的所有屬於自動佈線依賴關係的類都需要由Spring創建。總之,你永遠不應該創建一個Spring類的new,並使用ApplicationContext#getBean()或者當它被另一個類注入時創建它們。

此外,熊與構造爲在創建bean的點心中的自動裝配Autowired依賴是空(未初始化),你需要創建一個init()@PostConstruct註解。

1

兩個主要的原因是:

  1. 你一定要告訴春天,在哪裏掃描組件(確保包裝是正確的)

  2. 你沒有實際的bean的實現(確保StudentDao有@Service太)