Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式)
上面一遍文章Spring框架(2)---IOC装配Bean(xml配置方式)讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象
注解方式需要在原先的基础上重新配置环境:
(1)Component标签举例
1:导入架包:

这个包在spring开发包中就有,我测试了下,如果取消这个包,运行确实会报错:
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nested exception is
我只知道这个包是对于AOP起作用的,但我还是不知道这个包对于注解方面提供了怎样的帮助,大家如果知道,非常需要你的留言,谢谢!
第二个配置:applicationContext.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:p="http://www.springframework.org/schema/p"
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"> <!-- 配置注解bean所在的包 -->
<context:annotation-config/> <!--支持注解-->
<!--自动扫描--> <!-- base-package放的是包名,有多个包名中间用逗号隔开 -->
<context:component-scan base-package="com.study.spring.a_beandefinition"></context:component-scan>
Component标签注入
@Component("helloService")
//就相当于xml 文件中配置 <bean id="" class=""></bean> id="helloService" class就是当前类
public class HelloService {
}
HelloTest 测试类
public class HelloTest {
/*
* 除了搭建环境和xml有点区别,其它方式一样
*/
@Test
public void demo1() {
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService=(HelloService) applicationContext.getBean("helloService");
System.out.println(helloService);
}
/*
* 输出一个内存地址:com.study.spring.a_beandefinition.HelloService@1895c4c
*/
}
上面是一个最基本的Component注入的简单例子:
除了@Component外,Spring提供了3个功能基本和@Component等效的注解
@Repository 用于对DAO实现类进行标注
@Service 用于对Service实现类进行标注
@Controller 用于对Controller实现类进行标注
***** 三个注解为了后续版本进行增强的
(2)bean属性的注入:
普通属性
@Value(value="你好")
private String info;
对象属性
对象属性有两种方法,他们是对等的
/*
* @Autowired:自动装配默认使用类型注入.
* @Qualifier("userDao"):按名称进行注入.
*/
@Autowired
@Qualifier("userDao")
private UserDao userDao;
@Resource(name="userDao")
private UserDao userDao;
Bean其他的属性的配置
配置Bean初始化方法和销毁方法:init-method 和 destroy-method
@PostConstruct 初始化
@PreDestroy 销毁
配置Bean的作用范围:@Scope("singleton")
整体的举例:
@Service(value="userService")
@Scope("singleton")
public class UserService {
@Value(value="你好")
private String info; @Resource(name="userDao")
private UserDao userDao; public void sayHello(){
System.out.println("Hello Spring Annotation..."+info);
} @PostConstruct
public void setup(){
System.out.println("初始化...");
} @PreDestroy
public void teardown(){
System.out.println("销毁...");
}
}
Spring3.0提供使用Java类定义Bean信息的方法(用的很少)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /*
* car和product领域对象类和相关属性我这里就不写了
*/
@Configuration
public class BeanConfig { //就相当于得到了当前的car对象
@Bean(name="car")
public Car initCar(){
Car car =new Car();
car.setName("法拉利");
car.setPrice(6000000);
return car;
}
//同样就相当于得到了product对象
@Bean(name="product")
public Product showProduct(){
Product product =new Product();
product.setPname("空调");
product.setPnum(200);
return product;
}
}
测试类
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.study.spring.b_di.Car;
import com.study.spring.b_di.Product;
import com.study.spring.b_di.ScopeBean;
import com.study.spring.c_di.CustomerService; public class SpringTest { //测试
@Test
public void demo2(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml"); Car car =(Car) applicationContext.getBean("car");
System.out.println(car); Product product =(Product) applicationContext.getBean("product");
System.out.println(product);
/*
* 输出结果:
* Car [name=法拉利, price=6000000.0]
* Product [pname=空调, pnum=200]
*/
//说明已经获得了方法返回的两个对象
}
}
(3)实际开发中使用XML还是注解?
XML:有利于bean管理
注解:注入属性的时候比较方便
两种方式结合:一般使用XML注册Bean,使用注解进行属性的注入
最后我找来一张图,用这张图来结尾本篇文章

有不足之处,欢迎大家指点,谢谢!
Spring框架(3)---IOC装配Bean(注解方式)的更多相关文章
- Spring框架(2)---IOC装配Bean(xml配置方式)
IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...
- Spring 框架 详解 (四)------IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...
- spring IOC装配Bean(注解方式)
1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...
- 05_IOC容器装配Bean(注解方式)
IOC容器装配Bean(注解方式) 1.使用注解方式进行Bean注册 xml 方式: <bean id="" class=""> spring2.5 ...
- Spring框架几种创建bean的方式
Spring框架下,Bean的创建和装配非常的灵活,提供了三种主要的方式,并且相互见可以互相看见,也就是你可以随意地采用你喜欢且合适的方式创建Bean,而不用担心他们之间的兼容问题. 一.使用XML显 ...
- Spring框架的AOP技术(注解方式)
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...
- Spring框架中的AOP技术----注解方式
利用AOP技术注解的方式对功能进行增强 CustomerDao接口 package com.alphajuns.demo1; public interface CustomerDao { public ...
- Spring实战2:装配bean—依赖注入的本质
主要内容 Spring的配置方法概览 自动装配bean 基于Java配置文件装配bean 控制bean的创建和销毁 任何一个成功的应用都是由多个为了实现某个业务目标而相互协作的组件构成的,这些组件必须 ...
- Spring 框架 详解 (三)-----IOC装配Bean
IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...
随机推荐
- GWAS
GWAS的数据形式:SNP数据,即各个SNP位点的aa,Aa,AA基因型与疾病状态(0正常,1患病)的样例-对照数据. 在遗传流行病学上,全基因组关联研究(Genome Wide Associatio ...
- 超强JavaScript编辑器WebStorm代码提示迟缓问题及其它想到的
去D2后发现 Hedger 的js编辑器是webStorm,便下载来试试,结果发现: 虽然WebStorm的js的提示远远不如aptana那么强悍(输入字母d不提示document),但是做为 Int ...
- jquery 组合键键盘事件
jQuery处理键盘事件,比如小说网站中常见的按左右键来实现上一篇文章和下一篇文章,按ctrl+回车实现表单提交,google reader和有道阅读中的全快捷键操作... 本文讲述jQuery处理按 ...
- oracle语句总结(一)
1,建外键 新建两个表来实现外键的关联. create table main_tab ( id number, name varchar2(30) ); create table sub_tab ( ...
- I/O流
转自:http://www.cnblogs.com/dolphin0520/p/3791327.html 一.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的 ...
- JS属性操作
一.属性读操作:元素.属性名 ( 获取.找到属性值 ) 属性写操作:元素.属性名 = 新的值 ( 替换.修改属性值 ) 二.没有属性名的,也可进行读.写操作: 读操作:元素.innerHTML ...
- FMS+NGINX打造高带宽利用率的流媒体(音频+视频)环境
fms自身已经拥有了httpd,用来给客户端访问用,例如通过http的音频播放.众所周知,非专业的httpd自然有不专业之处,例如我遇到的情况就是经常http服务假死,或者在访问量庞大的时候会无缘无故 ...
- Paxos 实现日志复制同步(Multi-Paxos)
Paxos 实现日志复制同步 这篇文章以一种易于理解的方式来解释 Multi-Paxos 的机制. Multi-Paxos 的是为了创建日志复制 一种实现方式是用一组基础 Paxos 实例,每条记录都 ...
- Python实现字典的key和values的交换
#encoding=utf-8 #反转字典 即key和val互换 dict1={1:2,3:4,6:7,9:10} print '---生成器表达式' def invert_dict(d): retu ...
- Vmware虚拟机安装Ubuntu 16.04 LTS(长期支持)版本+VMware tools安装
今天用VMware虚拟机安装了Ubuntu 16.04,过程用点繁琐,顺便又安装了VMware tools,这样就可以全屏显示Ubuntu系统了,下面是具体的安装过程! 第一步:准备工作 1.下载Ub ...