簡體   English   中英

注釋在春季不起作用

[英]Annotation not working in spring

我是春季的初學者,春季有dao類。但是注釋@autowired或@service無法正常工作,我已經通過在應用程序上下文中創建bean解決了該問題,這是為什么注釋在春季不起作用的原因。還提供了“ context:component-scan base-package =“,但注釋不起作用

      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 />

問題尚不清楚,但是看起來您的控制器也應該使用@Controller進行注釋。 屬於Spring項目且具有自動關聯的所有類都需要由Spring創建。 簡而言之,您絕對不應new一個Spring類並使用ApplicationContext#getBean()或在其他類注入它時創建它們。

此外,請記住構造函數,因為在創建bean時,自動裝配的依賴關系為null(未初始化),因此您需要創建一個帶有@PostConstruct注釋的init()方法。

兩個主要原因是:

  1. 您必須告訴spring,在哪里掃描您的組件(確保軟件包正確)。

  2. 您沒有實際的bean實現(請確保StudentDao也具有@Service)

請發布您得到的錯誤,也可以從發布的代碼中看到,即使您已經使用@service注釋對StudentDaoImpl類進行了注釋,您仍在xml配置中定義了相同的代碼,請使用基於注釋的config或基於xml的配置。

請檢查您的文件是否包含

  1. xml配置文件中提到了<context:annotation-config/> (如果啟用了組件掃描,則不需要)
  2. 檢查軟件包是否啟用了組件掃描
  3. 請參考答案以獲取正確的架構位置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM