一.概述
在Spring中IOC是一个非常重要的概念,在平时写程序时经常会出现一个对象依赖另外一个对象的,其实这种依赖关系在程序中不是很重要,重要的是在使用依赖对象的时候依赖对象是正常的。Spring容器管理了这种依赖关系,保证了依赖的对象在使用的时候是正常的,加入A要依赖B,我们只要把这种依赖关用一种Spring中提供的方式表达出来,这时候Spring容器就能识别出这种依赖关系,同时会把B给注入到A,保证在A中使用B的时候,B是正常的,在Spring中提供了一种基于包扫描的注入方式,我们只需要用一些注解就可以轻松实现对象的注入。详见下面的代码。
二.代码实现
package ioc.component;
public interface IFXNewsPersister {
}
package ioc.component;
public interface IFXNewsListener {
}
package ioc.component;
import org.springframework.stereotype.Component;
@Component
public class DowJonesNewsListener implements IFXNewsListener {
}
package ioc.component;
import org.springframework.stereotype.Component;
@Component
public class DowJonesNewsPersister implements IFXNewsPersister {
}
package ioc.component;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("...component-bean.xml");
FXNewsProvider newsProvider = (FXNewsProvider)ctx.getBean("FXNewsProvider");
newsProvider.getNewsListener();
System.out.println(newsProvider.info());
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 基于package包的扫描注入 -->
<context:component-scan base-package="ioc.component" />
</beans>
三.注意点
1.Autowire按照类型注入
2.Resource按照名字注入,如果找不到名字再按照类型注入,推荐使用@Reource(name="xx"),强制使用名字为xx的bean来注入,如果没有名字为xx的bean的话,注入就是失败的,但是要是遇见同类型的bean,也能注入,这样就会事与愿违。
3.要想被Spring容器扫描,就得现在xml文件中指定要扫描的包,同时也要在需要被扫描的类上面打上@Component标签,不打的话,不会被扫描到。
分享到:
相关推荐
<context:component-scan base-package="Mode"></context:component-scan> //表示在包mode下面的类将扫描带有@Component,@Controller,@Service,@Repository标识符的类并为之注入对象。 据说是因为XML配置太烦锁而...
-- 自动扫描bean,把作了注解的类转换为bean --> <context:component-scan base-package="com.test.controller" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.spring...
-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.baobaotao.web"/> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> ...
这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。...
在User.xml 的配置文件中,mapper namespace="com.yihaomen.mybatis.inter.IUserOperation" ,命名空间非常重要,不能有错,必须与我们定义的package 和 接口一致。 运行这个测试程序,就可以看到结果了。 除非...
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
5、依赖注入 (DI)自动给IOC容器中的对象,需要自动赋值的属性赋值 6、HandlerMapping 将一个url对应一个Method,将这样的关系保存起来, 暂时理解成是Map 3、运行阶段、等待请求 1、doGet、doP
-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能--> <context:component-scan base-package="com.ccc"/> <bean class="org.springframework.web.servlet.mvc.annotation....
对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 mvc:annotation-driven --> <!-- 扫描包 --> <context:annotation-config/> <context:component-scan base-package="com.org.*" /> ...
微信接口服务类位于com.github.jweixin.jwx.weixin.service包中,在spring配置文件中通过扫描包载入服务,在web mvc框架和微信控制器类中都可以通过@Autowired注解注入,与其他spring普通的服务类主键使用方式一致,...
-- 1 配置注解的自动扫描--> <context:component-scan base-package="cn.itcast.crm"/> <!--3 配置本地化代理工程bean,这是spring整合hibernate的入口 --> ...