1、在spring-config.xml中进行组件扫描的配置;
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.struts2.demo.*"/>
2、对DAO组件、Service组件和Action组件分别进行Annotation注解;
@Repository("userDao")
public class UserDaoImpl extends GenericDaoImpl<User> implements UserDao {/*省略*/}
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {/*省略*/}
@Controller(value = "testAction")
public class TestAction extends ActionSupport {/*省略*/}
3、通过@Resource Annotation实现Bean的依赖注入(不再需要get,set)。
public class GenericDaoImpl<T> implements GenericDao<T> {
@Resource
private SessionFactory sessionFactory;
}
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
}
@Controller(value = "testAction")
public class TestAction extends ActionSupport {
@Resource
private UserService userService;
}