SpringMVC注入Spring的bean
一、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的更多相关文章
- Java(多)线程中注入Spring的Bean
问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入 ...
- 【转】Java(多)线程中注入Spring的Bean
问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入 ...
- 【转】spring 装配Bean中构造参数的注入
转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...
- spring-从普通java类取得注入spring Ioc容器的对象的方案
1.启动服务时通过spring容器的监听器(继承ContextLoaderListener 监听器的方法) public class ListenerSpringContext extends Con ...
- 【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean
Dao层代码: package com.it.dao; public interface SayHell { public void sayHello(); } Dao的Impl实现层: packag ...
- 在Spring的bean中注入HttpServletRequest解密
我们可以在Spring的bean中轻松的注入HttpServletRequest,使用@Autowired HttpServletRequest request;就可以了. 但是,为什么我们可以直接这 ...
- spring中bean配置和bean注入
1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...
- 非spring组件servlet、filter、interceptor中注入spring bean
问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个be ...
- Spring中bean的注入方式
首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入.依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖. Spring ...
随机推荐
- Visual Studio 2015在.NET Core RC2项目中的一个错误。
更新了.NET Core RC2 之后,VS的Web Tools更新为“Preview 1”了. 这个版本有一个问题,害我折腾了一个下午. 就是在项目界面的“依赖项 - NPM”上面错误地显示了不必要 ...
- Java代码优化(长期更新)
前言 2016年3月修改,结合自己的工作和平时学习的体验重新谈一下为什么要进行代码优化.在修改之前,我的说法是这样的: 就像鲸鱼吃虾米一样,也许吃一个两个虾米对于鲸鱼来说作用不大,但是吃的虾米多了,鲸 ...
- C++笔记 之 基础回顾(一)
1 exe 程序
- Linux 定时任务crontab
crontab定时任务格式 1 * * * * * command 2 第1列表示分钟1-59 每分钟用*或者 */1表示 3 第2列表示小时1-23(0表示0点) 4 第3列表示日期1-31 5 第 ...
- file_put_contents 错误:failed to open stream: Invalid argument 一种原因
今天在测试nilcms系统的时候,出现了一个报错,导致缓存无法更新: file_put_contents(C:\UPUPW_AP5.4\vhosts\d.tv\NilCMS_APP\include_r ...
- 火狐下多个span连在一起和换行写存在差异
当父元素的宽度确定,多个span换行写,span加起来占的宽度比预设的大
- 浅谈网站web框架的本质
一.web框架的本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. import socket def handle_reques ...
- iOS PresentViewControlle后,直接返回根视图
在开发中:用[self presentViewController:VC animated:YES completion:nil];实现跳转,多次跳转后,直接返回第一个. 例如: A presentV ...
- Android细笔记--ContentProvider
Provider的不常见访问方式 Batch access:访问ContentProvider的一中模式,使用该模式可以同时对provider进行多个操作,且支持同时操作多个表.使用时首先构建一个Co ...
- Html--表单练习
<!--文档定义一定要带上,因为浏览器在解析的时候先按照文档定义的格式解析, 如果没有就按照浏览器默认的格式解析,可能会出问题.--> <html> & ...