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. 样式(Style)和主题(Theme)资源——样式资源

    样式和主题资源都是用于对Android应用进行“美化”的,只要充分利用Android应用的样式和主题资源,开发者可以开发出各种风格的Android应用. 样式资源:         如果我们经常需要对 ...

  2. input框的默认bug解决办法

    input框的默认bug是在没干掉边框的情况下是不能设置背景颜色的,否则边框会变成内边框(黑色)效果,很难看. 解决办法是: none掉input框的边框:border:none; 再设置其背景色为任 ...

  3. JS 工具 构建工具

    1.gruntjs http://www.gruntjs.net/ 2.bootstrap http://www.bootcss.com/ 3.

  4. --@ui-router——$state服务原版详解

    $state service in module ui.router.state Description $state service is responsible for representing ...

  5. java解析XML,并生成文档

    一.Java中XML的四种方法 Java学习者须知:Java中XML的四种方法 java xml学习总结(4中方法的例子介绍) JDOM解析XML Dom4j解析XML

  6. 第一部分 记事本搞定第一个C#程序和编译过程剖析

    记事本搞定第一个C#程序 进行下面三个步骤:编码,编译和托管运行. 1.记事本进行编码: using System; class Program{ public static void Main() ...

  7. EF 4.1 学习资源汇总

    微软发布了EF 4.1以后,结合asp.net mvc3,网站的开发可谓是非常方便.但是作为一种新技术,如何开始你的学习之路呢? 首先是关于 EF 4.1的安装和介绍,以及nuget的使用. http ...

  8. MyBatis 延迟加载

    在sqlMapConfig中进行设置 <configuration> <settings> <!--延迟加载的总开关--> <setting name=&qu ...

  9. secureCRT自动化脚本

    http://cysnow.iteye.com/blog/1698791 cd \crt "C:\Program Files\VanDyke Software\Clients\SecureC ...

  10. 实现过程全纪录——自己写一个“微信朋友圈”(包括移动端与PC端)

    一.朋友圈的基本单元--动态 首先定义一个自定义控件用来显示每条动态. 二.运行效果 三.核心解读 PushedMessage 有个PushIndex属性,表示发送消息的index,从0开始递增.每个 ...