Spring IOC容器装配Bean_基于注解配置方式
bean的实例化
1.导入jar包(必不可少的)
2.实例化bean
- applicationContext.xml(xml的写法)
<bean id="userDao" class="com.igeekhome.dao.impl.UserDao"></bean>
- 注解的写法
第一种:在 applicationContext.xml中开启注解扫描(同时引入context命名空间)
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描
context:component-scan 配置spring ioc容器开启注解扫描,扫描指定包下的@Component修饰的类,将这些类的对象创建交给spring ioc容器完成
base-package: 需要扫描哪些包(及其子包)
-->
<context:component-scan base-package="com.igeekhome"></context:component-scan>
</beans>
第二种:@Component、@Service、@Controller、@Repository 用于实例化bean
Spring3.0为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理
@Service、@Controller、@Repository是@Component衍生的子注解
- @Repository用于标注数据访问组件(如DAO层组件)
- @Service用于标注业务层组件(如Service层)
- @Controller用于标注控制层组件(如struts中的action层)
- @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
/*
注解:添加在实现类上(思想同配置文件)
@Component 等价于 <bean id="userDaoImpl" class="com.igeekhome.dao.impl.UserDaoImpl"></bean>
id:默认是类名(首字母小写) value:bean的id
@Component(value = "userDao")
@Component("userDao")
*/
@Repository("userDao")
public class UserDaoImpl implements IUserDao {
public void select() {
System.out.println("UserDao...select...");
}
}
bean属性的依赖注入
简单数据类型依赖注入
Spring3.0后,提供 @Value注解,可以完成简单数据的注入
/*
@Component
@Component(value="customerService")
@Service(value="customerService")
四者都等价
*/
@Service("customerService")
public class CustomerService {
//简单类型的成员变量
@Value("Rose")//参数的值简单类型
private String name="Jack";
//保存业务方法
public void save(){
System.out.println("CustomerService业务层被调用了。。。");
System.out.println("name:"+name);
}
}
复杂类型数据依赖注入
1.使用@Value 结合SpEL -- spring3.0 后用
@Service("userService")
public class UserServiceImpl implements IUserService {
/*<bean id="userDao" class="com.igeekhome.dao.impl.UserDao"> </bean>
@Value("#{bean的id}")
*/
//写法一 (在属性声明上面注入,底层自动还是生成setUserDao())
@Value("#{userDao}")
private UserDaoImpl userDao;
//写法二
@Value("#{userDao}")
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
public void list() {
System.out.println("UserServiceImpl...list...");
userDao.select();
}
}
2.使用@Autowired 结合 @Qualifier
@Autowired:可以单独使用,如果单独使用,按照类型进行注入(会在spring ioc容器中查找类型为com.igeekhome.dao.impl.UserDaoImpl的bean并进行注入,如果找不到,肯定注入失败;如果找到匹配的单个bean,则注入成功; 如果找到多个相同类型的bean,则会选择其中一个bean进行注入
@Qualifier:与@Autowired结合,按照名称进行注入
@Autowired//默认按照类型注入的
@Qualifier("userDao")//必须配合@Autowired注解使用,根据名字注入
private UserDaoImpl userDao;
3.JSR-250标准(基于jdk) 提供注解@Resource
如果没有指定name属性,那么先按照名称(注解修饰的属性名)进行注入,在容器中查找bean的name/id为userDao的bean,如果找到,则注入成功。如果按照名称进行注入没有找到相对应的bean,那么就会使用按照类型进行装配,如果没有改类型,则注入失败,抛出异常;如果容器中存在单个该类型的bean,则注入成功;如果容器中存在多个相同类型bean,则注入失败(expected single matching bean but found 2: userDaoxxx,userDaox)
如果指定了name属性,那么就只会按照name进行注入
@Resource//(name="userDao")
private UserDaoImpl userDao;
4.JSR-330标准(jdk) 提供 @Inject和@Named注解(不推荐)
需要先导入 javax.inject 的 jar
使用@Inject,默认按照类型注入,使用@inject和@Named注解,则按照名称注入。用法与@Autowired 结合 @Qualifier 的用法一致
bean的初始化和销毁
1.applicationContext.xml 中的写法
<!--
init-method:指定初始化方法
destroy-method: 指定销毁触发方法
-->
<bean id="lifecycle" class="com.igeekhome.bean.LifeCycleBean" scope="singleton" init-method="initMethod" destroy-method="destroyMethod"></bean>
2.注解写法
使用 @PostConstruct 注解, 标明初始化方法 ---相当于 init-method 指定初始化方法
使用 @PreDestroy 注解, 标明销毁方法 ----相当于 destroy-method 指定对象销毁方法
@Repository("userDao")
public class UserDaoImpl implements IUserDao {
public void select() {
System.out.println("UserDao...select...");
}
//init-method
@PostConstruct
public void init() {
System.out.println("init...");
}
//destory-method
@PreDestroy
public void destory() {
System.out.println("destory");
}
}
bean的作用域
1.applicationContext.xml 中的写法
默认是 singleton 单例 ,prototype是多例
<bean id=”” class=”” scope=”prototype”>
2.注解写法
通过@Scope注解,指定Bean的作用域
@Service("userService")
//@Scope("singleton")默认是单列
@Scope("prototype")
public class UserServiceImpl implements IUserService { }
备注
只有在Spring配置文件中开启了注解扫描
才能使用 @Component @Autowired @Resource @PostConstruct @PreDestroy等注解
Spring IOC容器装配Bean_基于注解配置方式的更多相关文章
- Spring IOC容器装配Bean_基于XML配置方式
开发所需jar包 实例化Bean的四种方式 1.无参数构造器 (最常用) <?xml version="1.0" encoding="UTF-8"?> ...
- IoC容器装配Bean(xml配置方式)(Bean的生命周期)
1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...
- Spring IoC 源码分析 (基于注解) 之 包扫描
在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...
- 使用Spring框架入门四:基于注解的方式的AOP的使用
一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...
- Spring IOC 容器预启动流程源码探析
Spring IOC 容器预启动流程源码探析 在应用程序中,一般是通过创建ClassPathXmlApplicationContext或AnnotationConfigApplicationConte ...
- spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean
5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...
- 学习Spring(一) 实例化Spring IoC容器
实例化Spring IoC容器 1,读取其配置来创建bean实例 2,然后从Spring IoC容器中得到可用的bean实例 Spring提供两种IoC容器实现类型 a,一种为bean工厂 b,应用程 ...
- Spring IoC容器管理Action
Spring IoC容器管理Action有两种方式:DelegatingRequestProcessor.DelegatingActionProxy 不管采用哪一种方式,都需要随应用启动时创建Appl ...
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
随机推荐
- RocketMQ实战:生产环境中,autoCreateTopicEnable为什么不能设置为true
1.现象 很多网友会问,为什么明明集群中有多台Broker服务器,autoCreateTopicEnable设置为true,表示开启Topic自动创建,但新创建的Topic的路由信息只包含在其中一台B ...
- B/b.cpp:表达式化简,二分答案
不知道能不能粘题面于是不粘了. 首先声明这道题可以怎么水过: 随机化几万次操作,取最优答案. 暴力O(n2log n)可过. 不想打正解的可以走了. emm然而我的应该是正解,O(n log n). ...
- 股票交易——单调队列优化DP
题目描述 思路 蒟蒻还是太弱了,,就想到半个方程就GG了,至于什么单调队列就更想不到了. $f[i][j]$表示第$i天有j$张股票的最大收益. 那么有四种选择: 不买股票:$f[i][j]=max( ...
- ElasticSearch(二):文档的基本CRUD与批量操作
ElasticSearch(二):文档的基本CRUD与批量操作 学习课程链接<Elasticsearch核心技术与实战> Create 文档 支持自动生成文档_id和指定文档_id两种方式 ...
- [转载]2.6 UiPath循环嵌套的介绍和使用
一.循环嵌套的介绍 一个循环体内又包含另一个完整的循环结构,就称之为循环嵌套.内嵌的循环中还可以嵌套循环,这就是多层循环,也叫做多重循环. 二.在UiPath中结合使用循环嵌套生成99乘法表 1.打开 ...
- access,trunk,hybrid端口分析
1.access 接收:当数据没有tag时打上pvidtag进入,若有则看是否与pvid相等,相等则接收,不想等则丢弃. 转发:看tag是否等于pvid,若等则去tag发送,否则不处理. 2.trun ...
- T-SQL Part VI: Prevent error message "Saving changes is not permitted" in SSMS
使用SSMS时,经常遇到的问题是,修改一张table时,弹出一个错误对话框:“Saving changes is not permitted”. 这个错误通常是因为以下错误(参阅MSDN的KB文档 h ...
- 打包上传被拒 Guideline 2.5.1 - Performance - Software Requirements
打包上传被拒 Guideline 2.5.1 - Performance - Software Requirements 在项目中全部搜索:prefs:root 找到后,把这个私有的 NSURL *u ...
- 实现两个数字的交换(C语言)
int num1=10; int num2=20; //1.简单的数学方法实现数字交换 num1=num1+num2;//num1=30 num2=num1-num2;//num2=10 num1=n ...
- nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...