Spring注解标签详解@Autowired @Qualifier等 @Slf4j
@Slf4j @Slf4j注解实现日志输出
自己写日志的时候,肯定需要:
private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
1
每次写新的类,就需要重新写logger
有简单的方式,就是使用@Slf4j注解
首先是在pom中引入:
<!--可以引入日志 @Slf4j注解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
1
2
3
4
5
然后在类上写上@Slf4j注解
在方法中直接使用
如果注解@Slf4j注入后找不到变量log,需要IDEA安装lombok插件,
File → settings → Plugins
如图
安装完成后重启即可
package cn.chenhaoxiang;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Created with IntelliJ IDEA.
* User: 陈浩翔.
* Date: 2018/1/8.
* Time: 下午 8:05.
* Explain:日志测试
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {
private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
/**
* 传统方式实现日志
*/
@Test
public void test1(){
logger.debug("debug");//默认日志级别为info
logger.info("info");
logger.error("error");
logger.warn("warn");
}
/**
* Slf4j注解方式实现日志
*/
@Test
public void test2(){
log.debug("debug");//默认日志级别为info
log.info("info");
log.error("error");
log.warn("warn");
}
}
@Autowired
spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造函数进行标注,配合AutowiredAnnotationBeanProcessor完成Bean的自动配置。使用@Autowired注释进行byType注入。
在applicationContext.xml中加入:
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
通过 @Autowired的使用来消除 set ,get方法。
- @Autowired
- private UserDao userdao;
这样就可以删除set ,get方法和spring中的相关配制了。
- <bean id="userDao" class="..."/>
- <bean id="userService" class="...">
- <property name="userDao">
- <ref bean="userDao"/>
- </property>
- </bean>
通过@Autowired属的Setter方法给父类中的属性注入值。
- @Autowired
- public void setDataSource(DataSource dataSource)
- {
- super.setDataSource(dataSource);
- }
@Autowired(required = false)
当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false) ,这等于告诉 Spring:在找不到匹配 Bean 时也不报错。
当然,一般情况下,使用 @Autowired 的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false) 会很少用到。
@Qualifier
使用@Autowired注释进行byType注入,如果需要byName(byName就是通过id去标识)注入,增加@Qualifier注释。一般在候选Bean数目不为1时应该加@Qualifier注释。
在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出
BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。
和找不到一个类型匹配 Bean 相反的一个错误是:如果 Spring 容器中拥有多个候选 Bean,Spring 容器在启动时也会抛出 BeanCreationException 异常。
Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了,可以通过下面的方法解决异常:
- @Autowired
- public void setOffice(@Qualifier("office")Office office)
- {
- this.office =office;
- }
也可以直接注入到属性:
- @Autowired
- @Qualifier("office")
- private Office office;
@Qualifier(“office”)中的office是Bean的名称,所以@Autowired和@Qualifier结合使用时,自动注入的策略就从byType转变成byName了。
@Autowired可以对成员变量、方法以及构造函数进行注释,而@Qualifier的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以Spring不将 @Autowired和@Qualifier统一成一个注释类。
@Qualifier 只能和@Autowired 结合使用,是对@Autowired有益的补充。
一般来讲,@Qualifier对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。
@Resource
Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
@Resource装配顺序
1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;
@Component、@Repository、@Service、@Controller
Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。
在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。
虽然目前这3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。
所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用上述注解对分层中的类进行注释。
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
- @Service
- public class VentorServiceImpl implements iVentorService {
- }
- @Repository
- public class VentorDaoImpl implements iVentorDao {
- }
在一个稍大的项目中,如果组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。
Spring2.5为我们引入了组件自动扫描机制,他在类路径下寻找标注了上述注解的类,并把这些类纳入进spring容器中管理。
它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:
<?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"> <context:annotation-config /> <context:component-scan base-package=”com.iteedu.spring”> </beans>
annotation-config是对标记了@Required、@Autowired、@PostConstruct、@PreDestroy、@Resource、@WebServiceRef、@EJB、@PersistenceContext、@PersistenceUnit等注解的类进行对应的操作使注解生效。
base-package为需要扫描的包(含所有子包),负责扫描那些类有注解。
可以使用以下方式指定初始化方法和销毁方法:
- @PostConstruct
- public void init() {
- }
- @PreDestroy
- public void destory() {
- }
from: http://www.voidcn.com/blog/u013144121/article/p-2933655.html
http://iteedu.com/blog/2012/12/18/222.html
Spring注解标签详解@Autowired @Qualifier等 @Slf4j相关推荐
- Spring注解标签详解@Autowired @Qualifier等
@Autowired spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造函数进行标注,配合AutowiredAnnotationBeanProc ...
- Spring 注解@Value详解
一.spring(基础10) 注解@Value详解[1] 一 配置方式 @value需要参数,这里参数可以是两种形式: [html] view plaincopy @Value("#{con ...
- spring : springmvc常用注解标签详解(转)
新的项目,新的学习,好久没用这些注解了,同时在学习使用shiro ,lucene 等等.在网上找了些博文,感谢作者的总结和分享. 欢迎交流,言归正传: 1.@Controller 在SpringMVC ...
- springmvc常用注解标签详解
参考:https://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Disp ...
- [转]springmvc常用注解标签详解
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
- SoringMVC-常用注解标签详解(摘抄)
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
- Spring注解——@Profile详解
目录 一.Spring中的Profile 二.为什么要使用Profile 三.配置Profile示例 四.激活Profile的方式 一.Spring中的Profile Spring中的Profile功 ...
- spring注解注入详解
1.Spring配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...
- spring之旅第四篇-注解配置详解
spring之旅第四篇-注解配置详解 一.引言 最近因为找工作,导致很长时间没有更新,找工作的时候你会明白浪费的时间后面都是要还的,现在的每一点努力,将来也会给你回报的,但行好事,莫问前程!努力总不会 ...
最新文章
- 求一批整数中出现最多的个位数字_C语言经典100例007-求低n-1位的数
- java 怎么自定义排序_Java如何实现List自定义排序
- jQuery最佳实践
- Markdown-基础语法
- IOS学习笔记——Objective-c基础(一)
- 浅谈Java的数据结构
- vue标签旋转_vue.js编写移动端页面,检测旋转屏幕,横竖屏。
- 关于css中class属性多值问题(亲测,各种情况,附详细代码)
- sql 差值_SQL_LeetCode的连续性题目
- 百度地图api vue集成模板收录
- android自动化必备之界面元素
- 中文如何翻译成英文?手机中英文一键翻译超简单
- linux 使用tar命令压缩文件夹
- Oracle 19c 新特性一览
- 拿捏住C字符串,这个烦人程度不亚于指针的小东西
- 阿里少壮派上位:70后、80后全面掌舵阿里
- 加密与解密的基本概念--GPG加密工具的使用
- Airbin_0720
- 大二Web课程设计期末考试——基于HTML+CSS+JavaScript+jQuery电商类化妆品购物商城
- 【智能物流】AGV在汽车零部件厂内物流的应用案例;AGV自动引导车选型手册