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(注解方式)的更多相关文章

  1. Spring框架(2)---IOC装配Bean(xml配置方式)

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

  2. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  3. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  4. 05_IOC容器装配Bean(注解方式)

    IOC容器装配Bean(注解方式) 1.使用注解方式进行Bean注册 xml 方式: <bean id="" class=""> spring2.5 ...

  5. Spring框架几种创建bean的方式

    Spring框架下,Bean的创建和装配非常的灵活,提供了三种主要的方式,并且相互见可以互相看见,也就是你可以随意地采用你喜欢且合适的方式创建Bean,而不用担心他们之间的兼容问题. 一.使用XML显 ...

  6. Spring框架的AOP技术(注解方式)

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...

  7. Spring框架中的AOP技术----注解方式

    利用AOP技术注解的方式对功能进行增强 CustomerDao接口 package com.alphajuns.demo1; public interface CustomerDao { public ...

  8. Spring实战2:装配bean—依赖注入的本质

    主要内容 Spring的配置方法概览 自动装配bean 基于Java配置文件装配bean 控制bean的创建和销毁 任何一个成功的应用都是由多个为了实现某个业务目标而相互协作的组件构成的,这些组件必须 ...

  9. Spring 框架 详解 (三)-----IOC装配Bean

    IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...

随机推荐

  1. iOS 之 文件缓存

    对于信息量不是太大的数据,可以使用文件缓存来处理.文件缓存可以缓存字典和数组. 步骤一:创建路径 路径要一级一级往下创建,基本不用考虑创建失败的情况.但是如果创建失败了要怎么做呢?按道理应该提示出来. ...

  2. Xcode 添加前缀

    项目导航栏>Targets>右侧的Utilities>Class Prefix

  3. 2.2. 添加托管对象模型(Core Data 应用程序实践指南)

    右键分组Grocery Dude > New Group > Data Model New File > Core Data > 创建新的模板文件

  4. EF Code First:实体映射

    二.实体映射 实体与数据库的映射可以通过DataAnnotation与FluentAPI两种方式来进行映射: (一) DataAnnotation DataAnnotation 特性由.NET 3.5 ...

  5. 排名前10的H5、Js 3D游戏引擎和框架

    由于很多人都在用JavaScript.HTML5和WebGL技术创建基于浏览器的3D游戏,所有JavaScript 3D游戏引擎是一个人们主题.基于浏览器的游戏最棒的地方是平台独立,它们能在iOS.A ...

  6. Apache的.htaccess到Nginx的转换

    今天项目要求从Apache转到Nginx,遇到了要将原来的rewrite规则移过来的问题,找了半天资源,居然有一个转换工具,地址如下: http://www.anilcetin.com/convert ...

  7. 抓包分析YY音频

    YY的音频数据传输是P2P协议,音频的编码为AAC,下面抓去的音频编码的信息和频谱信息. 音频编码为AAC,采样为44K,码率24kb/s.音频编码在24kb/s码率能达到15K的音质.值得大家学习啊 ...

  8. 【Scala】Scala之Numbers

    一.前言 前面已经学习了Scala中的String,接着学习Scala的Numbers. 二.Numbers 在Scala中,所有的数字类型,如Byte,Char,Double,Float,Int,L ...

  9. 记一次企业级爬虫系统升级改造(五):基于JieBaNet+Lucene.Net实现全文搜索

    实现效果: 上一篇文章有附全文搜索结果的设计图,下面截一张开发完成上线后的实图: 基本风格是模仿的百度搜索结果,绿色的分页略显小清新. 目前已采集并创建索引的文章约3W多篇,索引文件不算太大,查询速度 ...

  10. EmitMapper 和TinyMapper 两者简单对比

    EmitMapper 和TinyMapper 两者的性能都是很高,相比autoMapper 速度不知道快了多少倍,因为使用的最多EmitMapper,所在业余时间做了一下测试对比. 测试数据:10万条 ...