Spring回顾(二)注解实现IoC和DI
2017-09-03 10:03
302 查看
注解实现DI
@Resource
为JavaEE拓展包提供。代替xml配置,注入对象,按照bean的id和类型匹配。具体写法如下:配置文件:
<!—配置注解解析器--> <context:annotation-config></context:annotation-config> <bean id="car" class="com.spring.annotation.di.Car"></bean> <bean id="wheel" class="com.spring.annotation.di.Wheel"></bean>
Car.java
package com.spring.annotation.di; import javax.annotation.Resource; public class Car { @Resource private Wheel wheel; public void show(){ System.out.println("I'm a Car !"); wheel.show(); } }
Wheel.java
package com.spring.annotation.di; public class Wheel { public void show(){ System.out.println("I'm a wheel !"); } }
测试类:Annotation_di_test.java
package com.spring.annotation.di; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Annotation_di_test { @Test public void test_Annotation_Di(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Car car = (Car) context.getBean("car"); car.show(); }
Tips:这里@Resource注解还能添加name属性。
如果@Resource的注解的name属性的值为”“
则把@Resource所在的属性的名称和spring容器中的id作匹配
如果匹配成功,则赋值
如果匹配不成功,则会按照类型进行匹配
如果匹配成功,则赋值,匹配不成功,报错
如果@Resource的注解的name属性的值不为”“
则解析@Resource注解name属性的值,把值和spring容器中的ID进行匹配
如果匹配成功,则赋值
如果匹配不成功,则报错
@Autowired
代替xml配置,注入对象,按照bean的类型匹配。为spring框架提供。与Qualifier注解组合。可以代替@Resource注解的功能。例子如下:Car.java
package com.spring.annotation.di; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class Car { //这两行注解相当于@Resource(name="wheel") @Autowired @Qualifier("wheel") private Wheel wheel; public void show(){ System.out.println("I'm a Car !"); wheel.show(); } }
IoC
@Component
将类放到Spring容器中,我们称这个类为一个bean,也称作一个component。在定义类类时使用注解@Component,能替代在配置文件applicationContext.xml中配置bean。从而减少配置文件的书写。首先我们要在配置文件中声明哪个包的bean注解需要被扫描:
<context:component-scan base-package="com.spring.annotation.ioc"></context:component-scan>
在定义bean类时使用@Component注解,可使用value属性,即指定bean的id。若不指定value属性,则会以第一个字母小写的类名作为bean的id
package com.spring.annotation.ioc; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component(value="helloWorld") public class HelloWorld { public void hello(String name){ System.out.println("Hello, " + name); }
@PostConstructor & @PreDestroy
xml中可以配置一个bean的init方法和destroy方法,分别在bean被实例化和销毁的时候调用。我们除了通过xml配置之外,还能通过注解来声明哪个方法为init方法,哪个方法为destroy方法。使用的注解分别为:
@PostConstructor
@PreDestroy
例子:
package com.spring.annotation.di; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Car { 4000 //这两行注解相当于@Resource(name="wheel") @Autowired @Qualifier("wheel") private Wheel wheel; public void show(){ System.out.println("I'm a Car !"); wheel.show(); } @PostConstruct public void init (){ System.out.println("Car init !"); } @PreDestroy public void destroy(){ System.out.println("Car destroy !"); } }
相关文章推荐
- Spring知识点总结(三)之注解方式实现IOC和DI
- 依赖注入(DI)和控制反转(IOC)的详细讲解 spring容器(spring注解实现,而不是xml配置文件)
- spring注解方式实现DI和IOC
- Spring 使用注解的方式实现IOC和DI(控制反转和依赖注入)
- 通过注解方式配置Spring实现Ioc
- Mvc4中使用Spring.Net实现IOC和DI
- Spring--IoC--基于注解的DI-组件扫描器的base-package
- Spring--IoC--基于注解的DI-使用Spring的JUnit4测试
- spring 框架中的依赖注入(IOC--设值注入)--使用注解--的具体实例的简单实现
- ######【spring属性注入(Ioc的DI)总结】:注解方式属性注入,属性名任意.=for理解:Aop注入代理对象时,注入被增强类对象时,属性名为proxy(自定义)。
- Servlet用Spring ioc di 注解创建对象初始化
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- spring 底层实现IOC DI简单依赖反射
- Spring笔记(一)——xml方式实现IoC和DI
- 使用Spring.Net 1.3.2实现Container(IoC)(一)IoC、DI、Spring.NET、Ioc Container、 ClassicContainer
- Spring IOC、DI、AOP原理和实现
- Spring_Spring与IoC_基于注解的DI
- Spring学习(1):控制反转(IoC)和依赖注入(DI)的详解以及注解(annotation)开发入门案例
- Spring(四)基于注解配置IOC容器&基于注解实现声明式事务
- Spring(10):使用注解实现IoC配置与示例