Spring注解配置三层
applicaltionContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 配置搜索的Bean所在包的位置 --> <context:component-scan base-package="com.zl.action"/> <context:component-scan base-package="com.zl.biz"/> </beans>
package org.slsk.action;import javax.annotation.Resource;import org.slsk.biz.SeasonService;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionContext;@Scope("prototype") //设置action为原型的@Controller("indexAction") //表示为控制器 'indexAction'为beanIdpublic class IndexAction extends AbstractActionSupport{private static final long serialVersionUID = 897641176074745343L;private SeasonService saService; //注入业务层对象saService@Resource(name="saService")public void setSaService(SeasonService saService) {this.saService = saService;}@Overridepublic String execute() throws Exception {ActionContext.getContext().put("list", saService.searchSeasonings());return SUCCESS;package org.slsk.biz;import java.util.List;import org.slsk.entity.User;import org.springframework.stereotype.Service;@Service("userService") //表示为业务bean public class UserService extends AbstractBizSupport {@SuppressWarnings("unchecked")public boolean checkUserByUsername(String username) throws Exception{List<User> list = ht.find("select u from User u where u.username = ?",username);return (null==list || list.size()==0)?true:false;}} //表示为数据访问层 @Resource("userDAO") public class UserDAOImp implements UserDAO { public void add(User user) { System.out.println(user.getUsername()); } }