SSH spring注解方式ioc注入

applicationContext.xml

配置扫描包位置,因为本人包目录不存在com之类的上层包,而base-package只写*会报错,所以需要分别配置。


	<context:component-scan base-package="action" />
	<context:component-scan base-package="service.impl" />
	<context:component-scan base-package="dao.impl" />

struts.xml


	<constant name="struts.objectFactory" value="spring" />

action

Controller value的值为struts.xml中action class的值
action中scope一定是多例的

@Controller(value="userAction")
@Scope(value="prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {
	User user = new User();
	@Autowired
	private UserService userService;
}

	<action name="UserAction_*" class="userAction" method="{1}">

service.impl

Service的值为借口实现的类名

@Service(value="UserService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
}

dao.impl

@Repository(value="UserDao")
public class UserDaoImpl implements UserDao {
	@Autowired
	private SessionFactory sessionFactory;
}

注解的方式会省去xml里很大一部分配置bean的空间。

你可能感兴趣的:(SSH spring注解方式ioc注入)