您的位置:首页 > 编程语言 > Java开发

springMVC 注解标签 结合 <context:component-scan>使用的功能等效于 <bean id="XXX" class="XXX">

2016-02-22 09:43 603 查看
1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。比如:

package com.org.base.web.controller;

import java.io.File;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.UUID;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import org.springframework.web.servlet.ModelAndView;

import com.tentcoo.base.constant.Attrs;

import com.tentcoo.base.entity.Kindergarten;

import com.tentcoo.base.entity.Teacher;

import com.tentcoo.base.entity.TeacherCard;

import com.tentcoo.base.entity.YzFunction;

import com.tentcoo.base.entity.YzFunctionTeacher;

import com.tentcoo.base.entity.YzRole;

import com.tentcoo.base.service.KindergartenService;

import com.tentcoo.base.service.PrincipalService;

import com.tentcoo.base.service.TeacherCardService;

import com.tentcoo.base.service.YzFunctionTeacherService;

import com.tentcoo.base.service.YzFunctionYzRoleService;

import com.tentcoo.core.util.HashPathUtil;

import com.tentcoo.core.util.PropertiesUtil;

import com.tentcoo.core.util.QiniuUtil;

import com.tentcoo.core.util.SHAencryptUtil;

@Scope(“prototype”) //多例模式

@Controller //注解为controller层,功能类似于XML配置文件中的

@RequestMapping(value = “/principal”) //请求的相对路径

public class UserController {

@Resource          //注入别的实例,功能类似于xml配置文件中的<property name="principalService"  ref="XXX"/>
private PrincipalService principalService;

@Resource
private TeacherCardService teacherCardService;

@Resource
private KindergartenService kindergartenService;

@Resource
private YzFunctionTeacherService yzFunctionTeacherService;

@Resource
private YzFunctionYzRoleService yzFunctionYzRoleService;

private static final Logger log = LoggerFactory.getLogger(UserController.class);


}

2:在XML配置文件中进行组件扫描即可

<mvc:annotation-driven />
<context:component-scan base-package="com.tentcoo.base.web.controller" />


3:上面两步就代替了XML配置文件中

<bean id="controller" class="com.org.base.web.controller.UserController ">
<property name="principalService"  ref="XXX"/>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息