Spring注解

[b]Spring注解[/b]


[b]Spring注解配置[/b]
1.注解本身做不了任何事情,和XML一样,只起到配置的作用,让你不能写XML(你用了注解将由Spring里面的注解代码为你实现)
2.以下为实现注解在XML中的相关元素的作用分析

[b]<context:annotation-config/>[/b]
1.他的作用是向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。
2.注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。


[b]<context:component-scan base-package="com.*"/>[/b]
1.他的作用是扫描那些要扫描的包(当然也可以全扫,速度慢),扫描包的过程就是Spring Bean的生成过程。(因为由注解代
替XML来生成Bean了)


[b]Spring相关注解[/b]
[b]1.@Component[/b],不推荐使用

[b]2.@Controller[/b],对应表现层的Bean,也就是Action,(就是与页面的交互层Bean)
@Controller
@Scope("prototype") //
public class UserAction extends BaseAction<User>{
……
}


[b]3.@Service[/b],服务层的Bean
@Service("userService") //userService为这个Bean的名字
public class UserServiceImpl implements UserService {
………
}


[b]4.@Repository[/b],对应数据访问层Bean
@Repository(value="userDao")
public class UserDaoImpl extends BaseDaoImpl<User> {
………
}


[b]5.@Autowired[/b],对一个bean的引用
@Autowired
protected RedisTemplate<Serializable, Serializable> redisTemplate;


[b]6.@Resource[/b],作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。

[b]7.@ModelAttribute[/b],请求参数放到一个模型(Bean)中

[b]8.@PathVariable[/b],用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数。
@Controller
public class TestController {
@RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)
public String getLogin(@PathVariable("userId") String userId,
@PathVariable("roleId") String roleId){
System.out.println("User Id : " + userId);
System.out.println("Role Id : " + roleId);
return "hello";
}
}


[b]9.@RequestMapping[/b],RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。RequestMapping注解有六个属性
1)、 value, method;
value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
2)、consumes,produces
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3)、params,headers
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

@RequestMapping(value = "/base2pro/data/transmit", produces = "text/plain;charset=UTF-8", method = RequestMethod.POST)
public @ResponseBody String appTransmitDataReportController(@ModelAttribute AppTransmitDataReportModel atdr) {
String returnMsg = null;
try {
logger.info("MCloud2Base appTransmitDataReportController Receive Msg:" + atdr.toString());
returnMsg = jsonObject.toString();
} catch (Exception e) {
logger.error("MCloud2Base appTransmitDataReportController Exception Error", e);
}
return returnMsg;
}



还有很多,后面用到加上

参考原文:[url]http://www.cnblogs.com/xdp-gacl/p/3495887.html[/url]
  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值