UserAction中通过@Autowired注入UserServiceImpl无效,得到对象为空
UserAction.java
@Namespace("/") @ParentPackage("struts-default") @Controller public class UserAction extends ActionSupport implements ModelDriven { private User user = new User(); public User getModel() { return this.user; } // ***************************** @Autowired private UserService userService; @Action(value = "userAction_add", results = { @Result(name = "add", location = "/success.jsp") }) public String add() { this.userService.saveUser(user); return "add"; } }
UserServiceImpl.java
@Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Transactional public void saveUser(User user) { this.userDao.save(user); } @Transactional public void updateUser(User user) { this.userDao.update(user); } @Transactional public void deleteUser(User user) { this.userDao.delete(user); } @Transactional(readOnly = true) public User findUserById(Integer id) { return this.userDao.findById(id); } @Transactional(readOnly = true) public List findAllUser() { return this.userDao.findAll(); } }
配置文件
org.hibernate.dialect.MySQL5Dialect true true update none thread com.entity
请保证UserServiceImpl在被扫描的包内。
注解的扫描有问题
我建议你看看UserDao是否正确注入,这条链子上有一个注入失败了就会导致整体创建失败