srping依赖注入的原理是根据sprin.xml配置文件中的bean生成容器,然后在使用时从容器中生成类的实例而不需要在内存中new出来。
一般方法是写<bean>在配置文件中,然后就可以在要调用该类,声明时,使用@Resource(name="xx")生成该类的实例了
例如:
@Resource(name = "test") private Test test;
还可以使用注解,不用在配置中写<bean>,但是配置文件有所改动:(这是整合了cxf的)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
关键是添加这句:
<context:component-scan base-package="com" />
那么就可以在需要注入的类上使用
@Component("test") public class Test {
即可
相关推荐
spring依赖注入底层详解,很不错的资源,欢迎大家来下载学习。
javaEE 开发中 现在最成熟的框架之一应该就是spring了 spring框架最强大的地方就是实现了依赖注入 也叫控制反转 最近的一个项目中用的就是 spring框架 spring框架是工厂模式的应用结合了MVC的设计思想 大家可以...
java 无需数据库 简单实例 如何实现Spring依赖注入 AOP
该资源包含了一个利用spring实现的依赖注入的案例,简单易懂。
Spring依赖注入使用构造设注入demo。完整可运行。
Spring依赖注入检查,适合初学者进一步了解Spring框架。
模仿Spring依赖注入,代码详细,简单,明了
spring 依赖注入三种方式测试源码,测试细节参考博文http://blog.csdn.net/u010679383/article/details/71305400
详细阐明spring依赖注入工作原理,基于注解的一个完整例子,当然数据库需要自己去创建
Spring依赖注入使用实例工厂设注入demo。完整可运行。
Spring依赖注入使用setter设注入demo。完整可运行。
NULL 博文链接:https://zhangyulong.iteye.com/blog/856986
Spring依赖注入使用静态工厂设注入demo。完整可运行。
揭秘spring核心机制, 模拟spring依赖注入
NULL 博文链接:https://matchless1688.iteye.com/blog/1038302
Spring依赖注入——java项目中使用spring注解方式进行注入.rar
NULL 博文链接:https://huangminwen.iteye.com/blog/1041743
Spring Ioc 注解 依赖注入
NULL 博文链接:https://shmilyaw-hotmail-com.iteye.com/blog/2169569
本项目为Java项目,使用Spring实现依赖注入,实现采用ApplicationContext,不再直接使用XmlBeanFactory方式。适合初学者了解Spring依赖注入的实现。