一、Spring和SpringMVC两个IOC容器有什么关系呢?

Spring的IOC容器包含了SpringMVC的IOC,即SpringMVC配置的bean可以调用Spring配置好的bean,反之则不可以。

如果SpringMVC想通过@Autowired注入Spring容器里的属性,即使Spring配置文件已经配置好了。

<context:component-scan base-package="com.wzy"></context:component-scan>

或者<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> ,

SpringMVC配置文件中也得需要从新配置

二、把bean放入IOC的方式

1·在配置文件中手动配置,此方式配置比较清晰明了

如:<bean id="test" class="com.wzy.controller.Test"></bean>

就把com.wzy.controller.Test放入了IOC容器里啦

2·两种可以使用自动扫描的方式配置,这种比较省事

<context:component-scan base-package="com.wzy"></context:component-scan>

自动扫描com.wzy包下的所有文件,如果类上标记了

@Controller(控制层);@Service(服务层);@Repository(持久层dao);@Component(普通组件)

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

会自动把这个类放入IOC容器。

注意:@Controller@Repository@Service这3个注解都是基于@Component。
不信看源码: @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component { /**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
public abstract String value() default ""; } @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component //看这里
public @interface Controller { /**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default ""; } 其他两个注解类似,不一一列出了

开启了context:component-scan后会自动开启

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

之后可以通过@Autowired自动注入

三、从IOC中获取bean

1·手动获取,配置比较清晰

在类里这样写

class Test{

  private Person person;
  public void setPerson(Person person) {
  this.person = person;
  }

}

配置文件里

<bean id="test" class="com.wzy.controller.Test">
<property name="person" ref="person"></property>
</bean>

这样就把person属性注入进去了

2·自动注入(@Autowired)

类里这样写:

@Autowired

private Person person;

这样如果IOC里有Person ,就会自动注入进去

使用@Autowired的前提是开启

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

如果不开启的话,自动注入不起作用

如果配置文件中已经配置了context:component-scan的话,那么AutowiredAnnotationBeanPostProcessor会自动开启

SpringMVC注入Spring的bean的更多相关文章

  1. Java(多)线程中注入Spring的Bean

    问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入 ...

  2. 【转】Java(多)线程中注入Spring的Bean

    问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入 ...

  3. 【转】spring 装配Bean中构造参数的注入

    转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...

  4. spring-从普通java类取得注入spring Ioc容器的对象的方案

    1.启动服务时通过spring容器的监听器(继承ContextLoaderListener 监听器的方法) public class ListenerSpringContext extends Con ...

  5. 【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean

    Dao层代码: package com.it.dao; public interface SayHell { public void sayHello(); } Dao的Impl实现层: packag ...

  6. 在Spring的bean中注入HttpServletRequest解密

    我们可以在Spring的bean中轻松的注入HttpServletRequest,使用@Autowired HttpServletRequest request;就可以了. 但是,为什么我们可以直接这 ...

  7. spring中bean配置和bean注入

    1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...

  8. 非spring组件servlet、filter、interceptor中注入spring bean

    问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个be ...

  9. Spring中bean的注入方式

    首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入.依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖. Spring ...

随机推荐

  1. Windows 10 下mysql 安装后无法启动问题

    安装过程: 1. 官网下载5.15.7, http://dev.mysql.com/downloads/, 选择开源社区版:MySQL Community Server (GPL) 2. 我解压后放在 ...

  2. [moka同学笔记]八、Yii2.0课程笔记(魏曦老师教程)[授权]

    数据库表创建 在执行yii rbac/init之前,需要在 \console\controllers\RbacController.php中 <?php namespace console\co ...

  3. 谷歌浏览器下载地址 chrome最新版本 百度云地址

    每次下载更新谷歌浏览器是一件很蛋疼的事情.百度搜索"谷歌浏览器下载地址",居然有很多骗子网站,相信有很多不知所以的人中招了.收集了一些chrome的安装包,放在了百度云里面(打不开 ...

  4. tomcat远程调试javaweb

    当把一个本地项目部署到远程服务器后有可能出现意想不到错误,这个时候通过远程调试能够更清楚的找到bug所在位置. 目前百度tomcat绝大多数方法都是在startup.sh或者catalina.sh增加 ...

  5. 移动AD的计算机到对应的OU的powershell脚本

    #//************************************************************* #//编辑人: #//编辑单位: #//编辑作用:移动计算机到对应的O ...

  6. SharePoint 2013 图文开发系列之Visual Studio 创建母版页

    一直以来,对于SharePoint母版页的创建,都是使用SharePoint Designer来创建和修改的,而后接触了SharePoint 2013,发现可以使用Html文件,通过设计管理器导入,然 ...

  7. android SQLite 批量插入数据慢的解决方案 (针对于不同的android api 版本)

    原地址 :http://www.cnblogs.com/wangmars/p/3914090.html SQLite,是一款轻型的数据库,被广泛的运用到很多嵌入式的产品中,因为占用的资源非常少,二其中 ...

  8. PHP(第一天)

    <?php // $name='lisi'; // $age =18; //$bol =true; //$bol =false; // echo ($bol); //echo ('name is ...

  9. 第一课 ionic 日志输出

    写程序的首要问题就是要打印日志,因为只有将日志输出才能真正了解程序的运行状态. 日志输出有两种方式 1.console输出 console.log("测试一下") console. ...

  10. 我的第一个React Native App

    我用了三天时间实现了一个相对比较完整的React Native 新闻发布类型的示例.应用做得很简单,但大多React Native的组件都有用到,今天做一个分享(由于我电脑是Windows系统,所以只 ...