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. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...
随机推荐
- Java 获取url参数
1. 方式一:使用HttpServletRequest对象 String id = arg0.getParameter("id"); mv.addObject("id&q ...
- 读取SQLServer数据库存储过程列表及参数信息
得到数据库存储过程列表: select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name ...
- XHTML 是以 XML 格式编写的 HTML
什么是 XHTML? XHTML 指的是可扩展超文本标记语言 XHTML 与 HTML 4.01 几乎是相同的 XHTML 是更严格更纯净的 HTML 版本 XHTML 是以 XML 应用的方式定义的 ...
- 使用IDEA开发及测试Spark的环境搭建及简单测试
一.安装JDK(具体安装省略) 二.安装Scala(具体安装省略) 三.安装IDEA 1.打开后会看到如下,然后点击OK
- jQuery 在Table中选择input之类的东西注意事项
jQuery 在Table中选择input之类的东西注意事项: 如果不在td标签中,是不能进行正确选择的: <table id="tblFormId"> <tr& ...
- MAC的VIMRC
set nocompatible " 关闭 vi 兼容模式 syntax on " 自动语法高亮 " color ...
- SQL Server 2008 sp3启用1433端口的方法
SQL Server 2008 SP3一般的默认端口是1433,但有时候会遇到一些问题导致1433端口无法启用.这是笔者给大家指导如何开启1433端口 工具/原料 SQL Server 2008 ...
- Vmware 中安装 Ubuntu Server (或者ubuntu 以文本界面登陆时) 分辨率无法全屏问题
Vmware 中安装 Ubuntu Server/Ubuntu 分辨率,无法全屏问题 需要更改grub设置 在终端或者文本界面按下列步骤进行设置: 第一步: 输入命令 sudo vim /etc/de ...
- ubuntu-14.10下,安装gcc交叉编译器遇到问题及解决方法
一 下载gcc-arm-none-eabi-4_9 安装成功后上报错误: ./gcc-arm-none-linux-gnueabi-gcc: No such file or directory 网上查 ...
- 《JAVASCRIPT高级程序设计》事件处理程序和事件类型
一.事件流 谈到事件,首要要理解事件流的概念:事件流是指从页面接受事件的顺序:“DOM2级事件”规定事件流包括三个阶段:事件捕获阶段.处于目标阶段和事件冒泡阶段.目前大部分的浏览器的事件流是事件冒泡, ...