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 ...
随机推荐
- Git branch 分支与合并分支
Git branch 分支 查看当前有哪些branch bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch * master 新建一 ...
- Oracle / PLSQL函数 - DECODE
1.DECODE( expression , search , result [, search , result]... [, default] ) 参数说明: expression : 表中的某一 ...
- 1 virtual
1 differents: 'virtual' just in C# In Java It does't have the keyword 2 Usages in C# used in base cl ...
- ftp下载文件失败get: Access failed: 550 Failed to open file. (t1.log)
get: Access failed: 550 Failed to open file. (t1.log) 原因是被SELinux安全访问控制策略限制了. 科普: SELinux(Security-E ...
- over(partition by)开窗函数的使用
开窗函数是分析函数中的一种,开窗函数与聚合函数的区别是:开窗函数是用于计算基于组的某种聚合值且每个的组的聚合计算结果可以有多行,而聚合函数每个组的聚合计算结果只有一个.使用开窗函数可以在没有group ...
- Linux基础命令---arping
arping arping指令用于发送arp请求到一个相邻的主机,在指定网卡上发送arp请求指定地址,源地址使用-s指定.该指令可以直径ping MAC地址,找出哪些地址被哪些电脑使用了. 此命令的适 ...
- Linux下几种重启Nginx的方式,找出nginx配置文件路径和测试配置文件是否正确
Linux下几种重启Nginx的方式,找出nginx配置文件路径和测试配置文件是否正确 目录在/etc/ngnix/conf.d下找出nginx配置文件路径和测试配置文件是否正确# /usr/sbin ...
- 2017年3月29日 webService入门理解 二
前边说到了N多webService的概念. 其实,说白了,我个人理解的话,webService就是一个“概念”.就好像互联网一样,就是一个很虚幻,很高的一个概念.同样,webService也是.互联网 ...
- jumpserver堡垒机安装
1. 下载jumpserver cd /opt wget https://github.com/jumpserver/jumpserver/archive/master.zip unzip maste ...
- 3D模型文字动画
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...