Spring框架(3)---IOC装配Bean(注解方式)

来源:互联网 发布:战地4优化 编辑:程序博客网 时间:2024/05/18 12:03

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 测试类
复制代码
 1 public class HelloTest { 2     /* 3      * 除了搭建环境和xml有点区别,其它方式一样 4      */ 5     @Test 6     public void demo1() { 7          8         ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml"); 9         10         HelloService helloService=(HelloService) applicationContext.getBean("helloService");11         12         System.out.println(helloService);13     }14     /*15      * 输出一个内存地址:com.study.spring.a_beandefinition.HelloService@1895c4c16      */17 }
复制代码

 上面是一个最基本的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")

整体的举例:

复制代码
 1 @Service(value="userService") 2 @Scope("singleton") 3 public class UserService { 4   @Value(value="你好") 5   private String info; 6  7   @Resource(name="userDao") 8   private UserDao userDao; 9 10   public void sayHello(){11     System.out.println("Hello Spring Annotation..."+info);12   }13 14   @PostConstruct15   public void setup(){16     System.out.println("初始化...");17   }18 19   @PreDestroy20   public void teardown(){21     System.out.println("销毁...");22   }23 }
复制代码

Spring3.0提供使用Java类定义Bean信息的方法(用的很少)

复制代码
 1 import org.springframework.context.annotation.Bean; 2 import org.springframework.context.annotation.Configuration; 3  4 /* 5  * car和product领域对象类和相关属性我这里就不写了 6  */ 7 @Configuration 8 public class BeanConfig { 9         10     //就相当于得到了当前的car对象11     @Bean(name="car")12     public Car initCar(){13         Car car =new Car();14         car.setName("法拉利");15         car.setPrice(6000000);16         return car;17     }18     //同样就相当于得到了product对象19     @Bean(name="product")20     public Product showProduct(){21         Product product =new Product();22         product.setPname("空调");23         product.setPnum(200);24         return product;25     }26 }
复制代码

测试类

复制代码
 1 import org.junit.Test; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4  5 import com.study.spring.b_di.Car; 6 import com.study.spring.b_di.Product; 7 import com.study.spring.b_di.ScopeBean; 8 import com.study.spring.c_di.CustomerService; 9 10 public class SpringTest {11    12     //测试13     @Test14     public void demo2(){15         ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");16         17         Car car =(Car) applicationContext.getBean("car");18         System.out.println(car);19         20         Product product =(Product) applicationContext.getBean("product");21         System.out.println(product);22      /*23       * 输出结果:  24       *  Car [name=法拉利, price=6000000.0]25       *  Product [pname=空调, pnum=200]26       */27        //说明已经获得了方法返回的两个对象 28     }29 }
复制代码

(3)实际开发中使用XML还是注解?

  XML:有利于bean管理
   注解:注入属性的时候比较方便
   两种方式结合:一般使用XML注册Bean,使用注解进行属性的注入

最后我找来一张图,用这张图来结尾本篇文章

0 0
原创粉丝点击