如何在spring框架

时间:2015-09-30 17:27:33

标签: spring spring-mvc

我设计了一个包装结构。

  • 控制器
  • Delegates(帮助类) - 此类执行所有业务并将值返回给控制器。
  • 服务
  • 服务实施
  • DAO
  • DAO实施。

我想实现autowired(Annotation)概念,并希望在spring-bean.xml上避免使用xml配置,例如服务和DAO配置。

如果我想避免xml配置,此代码无效。

我做了那些改变

  • bean id:loginDelegate,userService,userDao
  • 添加了@Service& @Repository注释到相应的服务& DAO实施。
@Controller("loginController")
public class LoginController {

    @Autowired
    private LoginDelegate loginDelegate;

    public LoginDelegate getLoginDelegate() {
        return this.loginDelegate;
    }
    public void setLoginDelegate(LoginDelegate tLoginDelegate) {
        this.loginDelegate = tLoginDelegate;
    }
    @RequestMapping(value="/login.do",method=RequestMethod.GET)
    public ModelAndView displayLogin(HttpServletRequest request, HttpServletResponse response) {
        log.info("<---displayLogin()--->");
        ModelAndView model = new ModelAndView("login");
        LoginBean loginBean = new LoginBean();
        model.addObject("loginBean", loginBean);
        return model;
    }
}   

public class LoginDelegate {
     @Autowired
     private IUserService userService;

     public IUserService getUserService() {
         return this.userService;
     }

     public void setUserService(IUserService userService) {
         this.userService = userService;
     }

     public boolean isValidUser(String username, String password) throws Exception {
         return userService.isValidUser(username, password);
     }
}

public interface IUserService {
    public boolean isValidUser(UserBean userObj);
    public int addUsers(UserBean userObj);
}

public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao userDao;

    public IUserDao getUserDao()  {
        return this.userDao;
    }

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    public boolean isValidUser(UserBean userObj) {
        return userDao.isExistUser(userObj);
    }

    @Override
    public int addUser(final UserBean userObj) {
        return userDao.saveUserDetails(userObj);    
    }
}

public interface IUserDao {
    public boolean isExistUser(UserBean userObj);
    public int saveUserDetails(UserBean userObj);
}

public class UserDaoImpl implements IUserDao {
    @Autowired
    UserBean userObj;

    @Autowired
    DataSource dataSource ;

    public DataSource getDataSource(){
        return this.dataSource;
    }

    public void setDataSource(DataSource dataSource){
        this.dataSource = dataSource;
    }

1 个答案:

答案 0 :(得分:0)

如果要完全摆脱基于XML的配置,请使用基于Java的配置

@Configuration
@ComponentScan(basePackages = "com.acme")
public class AppConfig  {
    ...
}
  1. 使用@Configuration注释时,上面的普通Java类使其成为“Spring配置类”(类似于基于XML的配置)。
  2. @ComponentScan注释扫描从启动时定义的包中使用@ Component,@ Controller,@ Service,@ Repository类注释的类,以使它们注册为Spring bean。这可以使用<context:component-scan base-package="com.acme" />
  3. 以XML格式完成

    参见:http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/beans.html#beans-java-instantiating-container-scan