注解方式实例化Java类
context:component-scan标签:
Sprng容器通过context:component-scan标签扫描其base-package标签属性值指定的包及其子包内的所有的类并实例化被@Component、@Repository、@Service或@Controller等注解所修饰的类。
@Component:基本注解
@Respository:持久层(一般为dao层)注解
@Service:服务层或业务层(一般为service层)注解
@Controller:控制层(一般为controller层)注解
默认情况下Spring依据默认命名策略为通过注解实例化的对象命名:类名第一个字母小写. 也可以在注解中通过@Component、@Repository、@Service或@Controller注解的value属性标识名称。
bean内属性赋值:
Sprng容器通过context:component-scan标签扫描时还会自动实例化AutowiredAnnotationBeanPostProcessor 类, 该实例对象可以自动装配具有 @Autowired 和 @Resource 或@Inject注解的属性。
@Autowired
1、成员变量(不考虑访问权限)、构造函数或setter方法都可以使用@Autowired注解
2、默认情况下@Autowired 注解依据类型自动为成员变量赋值,当 IOC 容器里存在多个类型相同的 Bean 对象时就会依据成员变量名进行赋值,此时要求变量名必须是多个Bean对象中的一个,否则就会出现异常, 该异常可以通过在 @Qualifier 注解指定实例名的方式解决;
3、默认情况下,通过@Authwired 注解为成员变量赋值时,如果Spring 找不到匹配的 Bean为成员变量赋值则会抛出异常;如果该成员变量允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false;
操作如下所示:
//创建一个类
package com.zzj.vo; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; //使用Component基本注解
@Component
public class School { //bean内属性赋值
@Autowired
//从多个实现类中选择需要的,如果不加当有多个实现类时会报错
@Qualifier("b")
private Date birth; public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} }
<!--application.xml-->
<bean id="b" class="java.util.Date"></bean>
<bean id="p" class="java.util.Date"></bean>
<!--Spring扫描com.zzj.vo包中Date类,由于该类使用了@Component注解,所以该类被实例化-->
<context:component-scan base-package="com.zzj.vo"></context:component-scan>
//测试类
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
System.out.println(context.getBean(School.class).getBirth());
注意:
1、使用context:component-scan标签需要添加spring-aop-4.3.10.RELEASE.jar包
2、base-package标签属性属性值:
a、Spring 容器将会扫描该属性值指定包及其子包中的所有类;
b、该属性值支持*通配符,例如“com.lq.*.imp”表示扫描诸如com.lq.book.imp包及其子包中的类;
c、当需要扫描多个包时, 使用逗号分隔;
d、resource-pattern标签属性可以指定Spring容器仅扫描特定的类而非基包下的所有类,比如resource-pattern="/*.class"表示只扫描基包下的类,基包的子包不会被扫描。
context:component-scan子标签:
1、<context:include-filter> 子标签设定Spring容器扫描时仅扫描哪些expression指定的类,该子标签需要和context:component-scan父标签中的use-default-filters属性一起使用;
2、<context:exclude-filter>子标签设定Spring容器扫描时不扫描哪些expression指定的类;
<context:component-scan base-package="com.zzj.vo">
<context:exclude-filter type="annotation" expression=""/>
<context:include-filter type="annotation" expression=""/>
</context:component-scan>
注解方式实例化Java类的更多相关文章
- spring利用注解方式实现Java读取properties属性值
1. 建立properties文件:我在resource下面建立一个config文件夹,config文件夹里面有mytest.properties文件,文件内容如下: sam.username=sam ...
- 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
[原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- Jsonschema2pojo从JSON生成Java类(Maven)
1.说明 jsonschema2pojo工具可以从JSON Schema(或示例JSON文件)生成Java类型, 并且可以配置生成Jackson 1.x,Jackson 2.x, Moshi 1.x或 ...
- Spring框架中的AOP技术----注解方式
利用AOP技术注解的方式对功能进行增强 CustomerDao接口 package com.alphajuns.demo1; public interface CustomerDao { public ...
- spring注解方式在一个普通的java类里面注入dao
spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...
- Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式
本节主要内容: 1. 给MessageBean注入参数值 2. 测试Spring自动组件扫描方式 3. 如何控制ExampleBean实例化方式 4. 使用注解方式重构Jdb ...
- JAVA配置&注解方式搭建简单的SpringMVC前后台交互系统
前面两篇文章介绍了 基于XML方式搭建SpringMVC前后台交互系统的方法,博文链接如下: http://www.cnblogs.com/hunterCecil/p/8252060.html htt ...
随机推荐
- Linux 添加新磁盘 && 创建分区 && 挂载
参考: 挂载目录 分区:https://blog.csdn.net/arenn/article/details/78866251 挂载:https://www.jb51.net/article/108 ...
- 关于html的基本知识
先上基本介绍图 HTML 简介 超文本标记语言 Hyper Text Markup Language 什么是标签(大多数标签) 尖括号包围.成对出现 开始标签,结束标签 元素 编辑器 HBuilder ...
- Linux软Raid--mdadm命令
mdadm:为软RAID提供管理界面,RAID设备可命名为/dev/md0./dev/md1./dev/md2./dev/md3等 命令的语法格式:mdadm[mode] <raiddevice ...
- 单元测试报错:unable to find a @SpringBootConfiguration
完整异常: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBoo ...
- 如何在ubuntu14.04(64位)编译运行32位程序
sudo -i cd /etc/apt/sources.list.d echo "deb http://archive.ubuntu.com/ubuntu/ raring main rest ...
- Ubuntu14 安装JDK 8
参考: [1]Ubuntu安装JDK7/JDK8 [2]Oracle官网安装JDK10 安装包安装 本文采用安装包安装方式 1.下载JDK安装包 JDK8下载 ,根据所使用系统选择安装包(这里选.ta ...
- 题解 P4949 【最短距离】
吼题啊 刚开始看上去又以为是LCT啥子的. 后来发现,TM是个图. 然后果断准备放弃,突然发现只有N个点N条边. woc,这不就一个基环树上树链剖分吗... 关于基环树问题,相信大家都一定很有经验了吧 ...
- ClientDataSet.locate报错问题
数据集循环之后如果使用locate定位,需要首先将数据集first
- c++存储区域
来自:https://www.cnblogs.com/simonote/articles/3146038.html 在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储 ...
- Android 获取当前日期距离过期时间的日期差值的完整方法直接使用
/*** * 获取当前日期距离过期时间的日期差值 * @param endTime * @return */public String dateDiff(String endTime) { Strin ...