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 ...
随机推荐
- VS2017
Visual Studio常用快捷键https://www.cnblogs.com/DonetRen/p/8182911.htmlVisual Studio 2017 Product Family S ...
- Yii2 Restful api搜索实现
- html5-select和datalist元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- SQL query - check latest 3 days failed job.
select top 100 js.last_run_date ,j.name, js.step_id,js.step_name,js.last_run_date,jsl.log,jh.message ...
- uvalive 3126 Taxi Cab Scheme
题意: 有m个人要坐出租车,每个人给出出发时间,出发地点和目的地(以二维坐标表示),两个地点之间所花的时间计算方式是两点之间的哈密顿距离.现在需要排遣车出去,一辆车每次只能装一个人,如果一辆车在装完一 ...
- 浏览器页面请求js、css大文件处理
当页面引用一个比较大的js和css文件时,会出现较大下载延迟,占用带宽的问题,如果一个应用里有很多这样的js或CSS文件,那么就需要优化了. 比如ext-all.js有1.4M,页面引用这个文件,正常 ...
- <keep-alvie></keep-alive>
<keep-alive></keep-alive>的作用是什么? <keep-alive></keep-alive> 包裹动态组件时,会缓存不活动的组件 ...
- python 列表推导
废话不多说,直接上代码 #coding=utf-8 def getitem(index, element): return '%d: %s' % (index, element) def getite ...
- Linux基础命令---arch
Arch Arch指令主要用于显示当前主机的硬件结构类型,我们可以看到它输出的结果有:i386.i486.mips.alpha等.此命令的适用范围:RedHat.RHEL.Ubuntu ...
- Radio中REG
Auto REG/REG OFF在广播接收质量不好时,收音机首先仅调整到该广播电台当前发射的可选频率.但是,如果接收质量差到“该发射电台濒临消失”的程度,则收音机也会接收德国NDR1(北德意志广播电台 ...