spring常用注解
使用注解构造IOC、替代传统的applicationContext.xml配置<bean/>和<property/>
传统的spring要在applicationContext.xml中配置:①<bean>类 ②<property>属性 如果有100个类和若干个属性,那么我们就要写成百上千个<bean>、<property>,这种就不利于维护,使用注解就能够解决这个问题。(项目中的方法也可以使用注解来替代)
在使用类注解前要在applicationContext.xml文件中添加:
<context:component-scan base-package="news"></context:component-scan>
注意:base-package可以指定一个包也可指定多个包,多个包逗号隔开
使用了<context:component-scan/>之后就可以把<context:annotation-config/>移除,因为<context:component-scan/>也能实现扫描包路径。
1.(类)@Controller
@Controller是对应控制层的<bean>,也就是action类
用法:
1 @Controller
2 @Scope("prototype")
3 public class NewsAction extends ActionSupport{
4 ……
5 }
注意:如果@Controller不指定其value【@Controller】,则默认的bean名字为这个类的类名首字母小写,如果指定value【@Controller(value="UserAction")】或者【@Controller("UserAction")】,则使用value作为bean的名字。
2、(类)@ Service
@Service对应的是业务层Bean
用法:
1 @Service("NewsService")
2 public class NewsServiceImpl implements NewsService {
3 ………
4 }
3、(类)@ Repository
@Repository对应数据访问层Bean,部分技术人员也喜欢叫dao类
用法
1 @Repository(value="NewsDao")
2 public class NewsDaoImpl extends NewsDaoImpl {
3 ………
4 }
注意:以上三种(@Controller、@Service、@Repository)是针对不同层的类所使用的注解,下面的是针对属性所使用的注解:
4.(属性)@ Autowired (不推荐使用,建议使用@Resource)
@Qualifier("...")
用法(在实现类中封装一个属性并私有化,在属性上面添加@Autowired @Qualifier)
如下:
public class NewsDaoImpl implements NewsDao {
//这种方式是spring注解
@Autowired
@Qualifier("mySessionFactory")
private SessionFactory sf;
}
注意:加入@Qualifier是为了让spring准确地知道要注入的对象,括号里自定义属性名字
5.(属性)@Resource(jdk)
用法:
public class NewsDaoImpl implements NewsDao {
@Resource(name="mySessionFactory")
private SessionFactory sf;
}
通过@Resource注解来给属性sf注入一个名字为mySessionFactory的值
spring常用注解的更多相关文章
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- Spring常用注解介绍【经典总结】
Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...
- Spring常用注解总结
转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...
- Spring注解 系列之Spring常用注解总结
参考:Spring系列之Spring常用注解总结 (1) Resource 默认是byName的方式进行bean配置,@AutoWired默认是按照byType的方式进行装配bean的:(2)Comp ...
- Spring系列之Spring常用注解总结 转载
Spring系列之Spring常用注解总结 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...
- spring常用注解笔记
spring常用注解解释: 1. Mybatis的映射文件xxxMapper.xml中resultMap标签的作用 resultMap标签是为了映射select查询出来结果的集合,其主要 作用是将实体 ...
- SpringBoot+Spring常用注解总结
为什么要写这篇文章? 最近看到网上有一篇关于 SpringBoot 常用注解的文章被转载的比较多,我看了文章内容之后属实觉得质量有点低,并且有点会误导没有太多实际使用经验的人(这些人又占据了大多数). ...
- Spring常用注解汇总
本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component- ...
- Spring常用注解,自动扫描装配Bean
1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...
- 【SSM 2】spring常用注解
声明:以下观点,纯依据个人目前的经验和理解,有不当之处,多指教! 一.基本概述 注解(Annotation):也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举 ...
随机推荐
- C# StreamReader
C#中的StreamReader()有重载的参数是encoding默认貌似是UTF-8的 当我们读取文本文件时,一般需要将其指定问default,default是gb2312.
- css 水平居中垂直居中的几种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spring中的JDBCTemplate
src\dayday\JDBCTestTest package dayday;import com.sun.org.apache.xalan.internal.xsltc.compiler.Templ ...
- Spring中配置文件中引用外部文件
src\dayday\conn.java package dayday;import java.sql.Connection;import java.sql.DriverManager;import ...
- Servlet向客户端发送中文数据的编码情况
(更多内容请关注本人微信订阅号:it_pupil) 本文讲述服务端servlet向客户端浏览器发送中文数据的编码情况,需要抓住下面几点: 输出流发送数据,必须是以字节形式传输的.也就是说,如果你在服务 ...
- 最新Xcode7.x环境下上架iOS App到AppStore 完整流程
最新Xcode7.x环境下上架iOS App到AppStore 流程 part 1 前言部分 之前App要上架遇到些问题到网上搜上架教程发现都是一些老的版本的教程 ,目前iTunesConnect ...
- JAVA学习遇到的问题:接口实现
引用知乎看到对接口的总结: 接口就是个招牌比如说你饿了,看到前面有个挂着KFC的店,然后你想到可以进去买汉堡了.KFC就是接口,我们看到了这个接口,就知道这个店会卖汉堡(实现接口).那么为什么我们要去 ...
- [转] Jenkins实战演练之Windows系统节点管理
[前提] 通过<Jenkins实战演练之Windows服务器快速搭建>(http://my.oschina.net/iware/blog /191818)和<Jenkins实战演练之 ...
- 批量过滤POST GET数据
if(get_magic_quotes_gpc()){ $_GET = stripslashes_array($_GET); $_POST = stripslashes_array($_POST); ...
- Struts2学习笔记
一.struts2的工作原理 上图为struts整体结构. 1.客户端初始化一个指向servlet的请求: 2.请求通过一系列过滤器(其中的ActionContextCleanUp为可选过滤器,对st ...