Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value
Bean管理注解实现
- Classpath扫描与组件管理
- 类的自动检测与注册Bean
- 类的注解@Component、@Service等作用是将这个实例自动装配到Bean容器中管理
- 而类似于@Autowired、@Required等注解则是将所代表的实例Bean1注册到需要这个实例的另一个Bean2中,在Bean2初始化时使其属性Bean1值不为null,他们并不能使Bean装配到Bean容器中。使用这些注解时,其代表的实例是要已经装配到Bean容器中的,否则会报错。
- <context:component-scan >
- 自动扫描类的注解:将使用注解的类注册到IOC容器中
<context:component-scan base-package="com.jing.spring.annotation"></context:component-scan>
- 自动扫描类的注解:将使用注解的类注册到IOC容器中
- @Component(value=" ")
- 是所有注解的父注解,用于不确定类是具体应该使用哪种类型注解时使用。可以代表任何一种类型注解。
- @Scope
- Bean的作用域
- @Scope(value="singleton")
- @Scope("value=prototype")
- @Repository
- 注解数据DAO层
- @Service
- 注解Service层
- @Controller
- @Required
- 适用于Bean的setter方法,表示Bean初始化时,Bean的属性必须被填充。
- @Autowired(!!!)
- @Autowired注解只是将Bean注入到具体属性或参数中,它不具备将Bean装配到Bean容器中的功能。
- 适用于Bean的setter方法,表示Bean初始化时,Bean的属性必须被填充。(功能同@Required)
private BeanTest beanTest; @Autowired
private void setBeanTest ( BeanTest beanTest){
this.beanTest =beanTest;
} - 适用于构造器
class BeanAnnotation{
private BeanTest beanTest; @Autowired
public BeanTest ( BeanTest beanTest){
this.beanTest =beanTest;
}
} - 适用于属性
class BeanAnnotation{ @Autowired
private BeanTest beanTest; public BeanTest ( BeanTest beanTest){
this.beanTest =beanTest;
}
} - 在使用@Autowired时,如果找不到合适的Bean将会报错。我们可以使用以下方法避免这个问题,代表这个Bean不是必要的
class BeanAnnotation{ @Autowired(required=false)
private BeanTest beanTest; public BeanTest ( BeanTest beanTest){
this.beanTest =beanTest;
}
} - 当使用@Autowired(required=true)时,代表这个Bean是必须已经装配到Bean容器中,每个类只可以在一个构造器上使用。
- 当一个类中,有多个setter需要标记为必须装配时,可以使用@Required代替@Autowired(required=true)。
- 注解常用的接口,比如BeanFactory、Application、ResourceLoader、MessageSource,用于在类中直接获取实例。
public class TestAutowired{ @Autowired
private ApplicationContext application; public TestAutowired{
application.getBean("");
}
} - 注解给需要该类型的数组,使其不为null,如List、Set
public class TestAutowired{ //AutowiredBeanI为接口,实现这个接口的类都将注入到list中
@Autowired
private List<AutowiredBeanI> list;
} - 注解给需要该类型的Map,使其不为null
public class TestAutowired{ //AutowiredBeanI为接口,实现这个接口的类都将注入到map中
@Autowired
private Map<String,AutowiredBeanI> map;
} - Next
- @Qualifier
- @Qualifier注解是将具体Bean注入到属性或参数中,它不具备将Bean装配到Bean容器中的功能。使用这个注解的前提是这个Bean已经装配到Bean容器中,否则会报错。
- @Qualifier可以指定具体名称的Bean,将Bean注入到属性或参数中,如下代码:TestQualifierI接口有多个实现类,如果不通过@Qualifier指定具体Bean,这几个实现类的Bean将都会注入到List中;指定具体Bean名称后,只会注入指定的Bean。
@Component(value = "qualifierEntry")
public class QualifierEntry { @Autowired
@Qualifier(value = "qualifierOne")
private List<TestQualifierI> qualifierIS; public void print(){
for (TestQualifierI qualifier:qualifierIS) {
qualifier.say();
}
}
} - 可以通过@Autowired和@Qualifier(value=" ")的方式指定Bean名字将Bean注入。但是这种方式一般不建议使用,可以使用JSR-250@Resource注解替代,这个注解是通过使用特定的名称来定义和识别特定的目标(这个匹配过程与Bean声明的类型无关)。
- @Configuration、@Bean、@Scope
- @Configuration标记类可以作为配置文档来使用,和配置文件产生关联
- 在使用@Configuration标记的类中,使用@Qualifier(value=" ")时,@Qualifier不能够注入其他类的实例,只可以引用本类中使用@Bean装配的实例。
- @Bean用于配置和初始化一个用于IOC容器管理的新实例,它可以用于标识具有特殊数据内容的实例,将这个实例存储在IOC中,方便读取。
- @Bean标记的类默认为singleton,可以和@Scope搭配使用,标记改变类的作用域
- @Bean只能应用于标记类的方法,这个类的作用是产生新的实例,并由@Bean标记交由IOC容器管理,和@Configuration注解一起使用。
package com.jing.spring.annotation; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class Configure { @Bean(value = {"configure2","configure1"},initMethod = "init",destroyMethod = "destroy")
public BeanConfigureI beanCon(){
return new BeanConfigureImpl();
}
}
- @ImportResource、@Value
- @ImportResource加载本地资源文件:xml,使用@Value将properties中得变量赋值到Java属性中,配置使用
<!-- spring-config.xml -->
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" > <context:property-placeholder location="classpath:/config.properties"></context:property-placeholder>
</beans>#config.properties
url:127.0.0.1
name:jingguoliangpackage com.jing.spring.annotation; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; @Configuration
@ImportResource(locations = {"classpath:spring-config.xml"})
public class Configure { @Value(value = "url")
private String url;
@Value(value = "name")
private String name; @Bean(value = {"configResource"})
public ValueAndReImportResource getConfigResource(){
ValueAndReImportResource valueAndReImportResource = new ValueAndReImportResource(url,name); return valueAndReImportResource;
}
} - Next
- @ImportResource加载本地资源文件:xml,使用@Value将properties中得变量赋值到Java属性中,配置使用
Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value的更多相关文章
- Java程序员常用的@Component、@Repository、@Controller、@Service系列【案例demo3】
Java程序员常用的@Component.@Repository.@Controller.@Service系列[案例demo3] 很多程序员通过在类上使用@Repository.@Componen ...
- Spring学习之常用注解(转)
使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...
- spring 以及 spring mvc 中常用注解整理
spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...
- SpringAnnotation注解之@Component,@Repository,@Service,@Controller
@Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字 ...
- Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)
Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...
- Spring中的常用注解
Spring中的常用注解 1.@Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象.
- springmvc学习笔记(常用注解)
springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...
- SpringMVC注解@Component、@Repository、@Service、@Controller区别
SpringMVC中四个基本注解: @Component.@Repository @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller 控制层, ...
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...
随机推荐
- react native中使用ScrollableTabView
第一步,下载依赖 npm install react-native-scrollable-tab-view --save 第二步,引入 import ScrollableTabView, { Scro ...
- java中垃圾回收机制中的引用计数法和可达性分析法(最详细)
首先,我这是抄写过来的,写得真的很好很好,是我看过关于GC方面讲解最清楚明白的一篇.原文地址是:https://www.zhihu.com/question/21539353
- ubuntu安装python-mysqldb
前期准备: sudo apt-get install libmysqld-dev sudo apt-get install libmysqlclient-dev sudo apt-get insta ...
- Spring Cloud 服务的注册与发现(Eureka)
Eureka服务注册中心 一.Eureka Server Eureka Server是服务的注册中心,这是分布式服务的基础,我们看看这一部分如何搭建. 首先,Spring Cloud是基于Spring ...
- html5-fieldset和legend和keygen元素的用法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- D. Duff in Beach
题意 数字串a[0---n-1], 通过不断的重复组成了 b[0,---l-1]l<10^18, 让你计算出 长度小于等于k的最长非递减子序列,满足,取得第 i 个取得是 L1 第i+1个取得 ...
- Another Meaning (KMP + DP)
先用KMP重叠匹配求出各个匹配成功的尾串位置.然后利用DP去求,那转移方程应该是等于 上一个状态 (无法匹配新尾巴) 上一个状态 + 以本次匹配起点为结尾的状态(就是说有了新的位置) + 1 (单单一 ...
- Spring Boot REST API 自动化测试
Spring Boot需要写大量的Junit代码来测试REST API, 这点让不了解代码的人很头疼.如果使用REST client工具测试REST API,很多REST Client工具是不支持自动 ...
- 纯Java增删改查
//自己写的一个完整的带增删改查提交重置功能的表单代码.package com.l16.test5;import java.awt.Color;import java.awt.Container;im ...
- puts函数
1.puts函数是gets函数的输出版本,它把指定的字符串写到标准输出并在末尾添加一个换行符 #include <stdio.h> #include <stdlib.h> in ...