Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i am a beginner in spring ,i have a dao class in spring .but annotation @ auto wired or @ service is not working ,i have solved the issue by creating a bean in application context,what is the reason for annotation not working in spring .provided with "context:component-scan base-package=" also but annotations are not working

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
158 views
Welcome To Ask or Share your Answers For Others

1 Answer

Please post the error that you are getting,Also from the code you have posted I can see that even though you have annotated the StudentDaoImpl class with @service annotation you are again defining the same in the xml configuration,Kindly use either annotation based config or xml based configuration.

Please check if your file has

  1. <context:annotation-config/> is mentioned in the xml config(Not needed if component scan is enabled)
  2. Check if component scan is enabled for the package
  3. Please refer to this answer for the correct schema location

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...