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. 1. 初次尝试Core Data 应用程序(Core Data 应用开发实践指南)

    本书以实践的方式讲解 Core Data,不会过早讲一些难懂的话题.从入门知识入手,演示如何为范例程序添加Core Data 支持,后续章节再依次讨论更复杂的话题. 1.1. Core Data 是什 ...

  2. 导航栏项目滑过时子菜单显示/隐藏jquery代码

    ;(function(window){    $('.menuitem').hover(function(){        $('>a',this).css('background-color ...

  3. Extjs换肤+cookie皮肤记忆功能

    http://www.myext.cn/kaifa/a_102.html    Ext之家 <title>无标题页</title>    <link rel=" ...

  4. jQuery addClass removeClass toggleClass hasClass is(.class)用法

    jQuery addClass removeClass toggleClass hasClass is(.class)用法 <%@ page language="java" ...

  5. CodeForces462B

    Appleman and Card Game Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  6. webSocket错误收集

    关于 使用WebSocket报如下错误, Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': already in ...

  7. tbl.js div实现的表格控件,完全免费,不依赖jquery

    html上现在有比较好用的表格控件是datatable,但是编辑.按钮等部分是收费的,只有基础功能免费.而且尺寸发生变化时需要手工刷新等繁琐操作较多.所以我开发一个免费的供大家使用. 本项目已用于&q ...

  8. [CSS3] 学习笔记-CSS动画特效

    在CSS3中,出现了很多出彩的效果,例如2D.3D以及过度.动画和多列等.这些效果为页面设计添加了很多的可选设计. 1.2D.3D转换 转换,是使元素改变尺寸.形状.位置的一种效果:通过CSS3转换, ...

  9. Salesforce删除数据时出现Insufficient privileges的可能原因

    遇到一个诡异的情况,用户通过界面删除一条自定义Object的数据的时候出现了Insufficient privileges.按理说,如果用户的Profile没有此Object的删除权限的话,应该连删除 ...

  10. java_ 集合

    集合类说明及区别Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ...