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;
}
Spring框架组件扫描与依赖注入实践
本文详细介绍了如何在Spring框架中使用组件扫描进行自动配置,并通过Annotation注解实现DAO、Service和Action组件的自动装配。同时展示了如何利用@Resource注解简化Bean之间的依赖注入过程。
1817

被折叠的 条评论
为什么被折叠?



