日期:2014-05-20  浏览次数:20803 次

The annotation @Autowired is disallowed for this location
如题所示:

以下 Dao implement 的源码:

Java code

public class User_Update_Dao_Impl implements User_Update_Dao {
    public boolean updateUser(User user){
            @Autowired
            
            SessionFactory sessionfactory ;
            User existinguser;
            
            try{
                existinguser=getUserByUsername(user.getUsername());
                
                existinguser.setUsername(user.getUsername());
                existinguser.setPassword(user.getPassword());
                existinguser.setGroupname(user.getGroupname());
                existinguser.setApplicationname(user.getApplicationname());
                existinguser.setLastname(user.getLastname());
                existinguser.setFirstname(user.getFirstname());
                existinguser.setEmail(user.getEmail());
                existinguser.setAddress(user.getAddress());
                existinguser.setDescription(user.getDescription());
                
                
                sessionfactory.getCurrentSession().update(existinguser);
                sessionfactory.getCurrentSession().flush();
                sessionfactory.getCurrentSession().clear();
                return true;
            }catch(Exception e){
                System.out.println("*****Error in update user in Dao: "+e);
            }
            return false;
        }

    @Override
    public List<User> listUsers() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public User getUserByUsername(String username) {
        // TODO Auto-generated method stub
        return null;
    }

}



bean文件的代码:

XML code

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.hp.sandbox.aa.model.entity.User</value>
                <value>model.entity.Privilege</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>                
            </props>
        </property>
    </bean>



求高手指点,小弟感激不尽



------解决方案--------------------
public class User_Update_Dao_Impl implements User_Update_Dao {
@Autowired

SessionFactory sessionfactory ;
public boolean updateUser(User user){


试试
------解决方案--------------------
看LZ的意思是想拿到配置的bean sessionfactory ,
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
 SessionFactory sessionfactory ;
=(SessionFactory )ac.getBean("sessionFactory");

在通过注解拿到User 对象existinguser,在User上必须做:existinguser 注释: @Entity
难后代码中
@Autowired
User existinguser
拿到对象。
------解决方案--------------------
探讨
最好能给我个解释,为虾米,这样做,谢谢

------解决方案--------------------