如题所示:以下 Dao implement 的源码:
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文件的代码:
<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 {
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文件的代码:
<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>
求高手指点,小弟感激不尽
@Autowired
SessionFactory sessionfactory ;
public boolean updateUser(User user){
试试
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
SessionFactory sessionfactory ;
=(SessionFactory )ac.getBean("sessionFactory");在通过注解拿到User 对象existinguser,在User上必须做:existinguser 注释: @Entity
难后代码中
@Autowired
User existinguser
拿到对象。
你仔细看看错误提示信息:The annotation @Autowired is disallowed for this location
这个貌似已经讲得很清楚了啊。
这个是自动装配的语法要求--不允许在局部进行自动装配。
自动装配的动作都是在容器启动的时候,容器在读取配置文件而且在加载类的时候就需要进行装配了,如果你将它放在方法内,它就成了局部的信息,对外是不可见的,无法进行装配,必须是成员变量,这样在加载的时候容器才检测到需要装配的变量。呵呵,可能文字组织的不好,不过感觉描述的思路已经讲明了,希望能够帮到楼主..
good, Thanks... 等我有时间,立刻结贴