29Spring_Autowriter的一些疑惑(很重要)
我用一个Autowriter去注解一个属性,而且我没有在Spring的配置文件中的bean.xml中注册bean(<bean id=""...);那么这个注解有用吗?答案是不行。也就是说要用Autowriter注解时,其实必须要保证在bean容器中注册过这个bean.
bean在bean容器中的注册分为两种:
1.手动:就是在spring.xml配置文件中,写上
<bean id="configa" class="com.guiugu.shen.bean2.HelloService">
</bean>
2.自动注册,那就是要用自动扫描了。在spring.xml配置文件中写上:
<context:component-scan base-package="com.guiugu.shen.bean2"/>
自动扫描时:会去com.guiugu.shen.bean2这个包下面去寻找有没有被
@Service用于标注业务层组件
@Controller用于标注控制层组件
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
这些注解修饰过,有的话就会被自动注册到bean容器中。
只有用以上其中一个方法注册过才会用@Autowriter是有用。
案例如下:
案例结构:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUkAAADuCAIAAAA7hmIdAAAeK0lEQVR4nO2db3Ac5X3Hzy+YvMlMPQMzhBed0BkGRJjiohDP9IY6NE2RUzp1GsYZM1MmLkmuKCYTcEkjAgqSqIhytptNIjnOOAi7iTWLiIvBycWX4NqCsTAdlcohCqo1KjKya3GWFAX/O1lOn77Yvd3nz+/Z2/u/+9z3Mzsa3d6zzz57t597nt3b/V7ixBzzpp/95/8+8PATrBG0PLizIesFwFQSdXP7xP7HvUl9tmFuT1nJRNKaqstSkWM025meZozlbLuzfSzX6OYIzI71t2UnGt2KOFMnt8d/8rj4sEMqULHbmVTCJZUpZblIuj093DbQ2TYwPOrs4gOEeKPZzraBfnuRrsBZypvS03SxKrs9Pey02WVxpJ1vofSwGHBbwN0lSngBa+z2QPe3Brq/NdDdd+W9wSvvDV56Z+f8iW2zI53//fKjA919jLEN33jeKVmZ25mUr/SUlSzN7ggyPezt1rNj/W12f7s9MssXWBxpD3ynQ4pRcLtaTKS5JsmfStPDbdJWBGK826VvoPDyFqcKbh85cqSvr4986ltP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的想法是:只要在applicationContext.xml中配一个自动扫描包,就可以了。如下
applicationContext.xml:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-lazy-init="true">
<context:component-scan base-package="com.guiugu.shen.bean2"/> </beans>
HelloService.java:
package com.guiugu.shen.bean2; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; public class HelloService { public void sayHello()
{
System.out.print("hello"); } }
TestJavaSpring.java代码:
package com.guiugu.shen.bean2; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContextt.xml") public class TestJavaSpring {
@Autowired
private HelloService helloService;
@Test
public void testSayhell()
{
helloService.sayHello(); } }
运行结果:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.guiugu.shen.bean2.HelloService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
很明显:上面的结果是找不到对应的bean.因为没有在bean容器中注册bean.
两个解决办法:1.手动在applicationContext.xml注册对应的bean.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-lazy-init="true">
<!-- 手动注册这个bean >
<bean id="configa" class="com.guiugu.shen.bean2.HelloService"> </bean>
</beans>
运行结果:正确。输出为hello。因为手动注册过了这个bean。
第二种解决办法:在applicationContext.xml用自动扫描器去扫描包。并且在HelloService上用@Component,因为这样自动扫描包才能识别。自动扫描包能识别的注解是:
@Service@Controller@Repository@Component。
ApplicationContext.xml代码:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-lazy-init="true">
<--扫描到之后会自动会去注册bean-->
<context:component-scan base-package="com.guiugu.shen.bean2"/> </beans>
HelloService代码:
package com.guiugu.shen.bean2; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; @Component
public class HelloService { public void sayHello()
{
System.out.print("hello"); } }
TestJavaSpring.java:
package com.guiugu.shen.bean2; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContextt.xml") public class TestJavaSpring {
@Autowired
private HelloService helloService;
@Test
public void testSayhell()
{
helloService.sayHello(); } }
运行结果正确。
上面的执行流程是:
Spring启动,然后根据<context:component-scan base-package="com.guiugu.shen.bean2"/>去这个包下面去寻找被@Service@Controller@Repository@Component注册类,找到的话就把他自动注册到bean容器中。
这样当我们执行
@Autowired
private HelloService helloService;
就会从bean容器中把对应的类取出来实例化(因为之前已经注册进去了)
29Spring_Autowriter的一些疑惑(很重要)的更多相关文章
- 关于Unicode和URL encoding入门的一切以及注意事项
本文同时也发表在我另一篇独立博客 <关于Unicode和URL encoding入门的一切以及注意事项>(管理员请注意!这两个都是我自己的原创博客!不要踢出首页!不是转载!已经误会三次了! ...
- BAT批处理(一)
本文摘自博文<BAT批处理文件教程> 这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,只要你识字就能看懂,就能学到知识.写这篇教程的目的,是让每一个看过这些文字的朋友记住一句话:如 ...
- HTML5_用语义化标记重新定义博客
HTML5文档的第一行便是文档类型声明,文档类型声明的作用有两个 一:验证器依据文档类型来判断采用何种验证规则去验证代码 二:文档类型声明能够强制IE6,IE7,IE8以“标准模式”渲染页面 1: & ...
- Bootstrap后台使用问题汇总(一)
第一次自己汇总写博客啊,不懂规矩,大家包涵~~ 最近进行的项目中需要一个后台,于是在网上Down了许多Bootstrap后台源码.精挑细选决定用“ACE后台管理系统”(因为是中文的,英文三级狗的小鹿还 ...
- Struts2技术内幕 读书笔记三 表示层的困惑
表示层能有什么疑惑?很简单,我们暂时忘记所有的框架,就写一个注册的servlet来看看. index.jsp <form id="form1" name="form ...
- 批处理最完整人性化教程(.bat文件语法)
原文链接:http://www.cnitblog.com/seeyeah/archive/2009/01/15/53808.html 这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,你要你识字 ...
- BAT编程
echo 表示显示此命令后的字符 echo off 表示在此语句后所有运行的命令都不显示命令行本身 @与echo off相象,但它是加在每个命令行的最前面,表示运行时不显示这一行的命令行(只能影响 ...
- 快速定位oracle故障-恩墨
首先我们要明白一点,所谓的故障,意味着相对来讲比较严重.也就是可能比不同的问题要严重一些,比如锁等待. 要能够快速的定位和解决问题,恢复业务正常:首先我们需要了解Oracle的一些常见的故障有哪些. ...
- django modelform中的self.instance
在stackoverflow上看到一个问题,正好是我疑惑很久的相关问题. [原问题地址]https://stackoverflow.com/questions/18265023/self-instan ...
随机推荐
- Java浮点运算-BigDecimal
package com.hsun.test; import static java.lang.System.out; import java.math.BigDecimal; public class ...
- Struts2漏洞分析,漏洞波及全系版本
Struts漏洞分析 Apache Struts团队已经发布了Struts 2.3.15.1安全更新版本.在Struts2.3.15.1版本之前,存在着严重的安全漏洞,如果现在一些比较大的网站是 ...
- UVALive 4857 Halloween Costumes
区间dp.对于最左边的点: 1.在该点穿的衣服只有该点用的到,即穿上就脱下.所以dp[ l ][ r ] = min(dp[ l + 1][ r ] + 1, dp[ l ][ r ]). 2.衣服仍 ...
- tftp的安装、设置以及put、get传输实验
tftp安装流程如下: (1)首先挂载Linux系统镜像文件到/media/cdrom/的(cdrom为在/media/下自己创建的目录) (2)进入/media/cdrom/Server/可以找到t ...
- 算法笔记_023:拓扑排序(Java)
目录 1 问题描述 2 解决方案 2.1 基于减治法实现 2.2 基于深度优先查找实现 1 问题描述 给定一个有向图,求取此图的拓扑排序序列. 那么,何为拓扑排序? 定义:将有向图中的顶点以线性方式进 ...
- openvpn mac客户端tunnelblick连接后自动添加路由
在openvpn 的服务器配置文件添加配置设置客户端连接后自动添加一条路由 示例: vim /etc/openvpn/server.conf # Push routes to the client t ...
- PHP-密码学算法及其应用-对称密码算法
转自:http://www.smatrix.org/bbs/simple/index.php?t5662.html ////////////////////////////////////////// ...
- “好奇号”火星车和它搭载的软件(来自Erlang程序员的观点)
http://www.aqee.net/on-curiosity-and-its-software/
- window.open()函数
http://hi.baidu.com/gagahjt/blog/item/7b76e0dee61b20aecd11661c.html open函数详解: window.open("sUrl ...
- 用Jedis操作redis示例一,Key,value HashMap
简单的key,value形式 public class RedisDemo { public static void main(String[] args) { Jedis jedis = new J ...