我用一个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/W12f7s9MssXWBxpD3ynQ4pRcLtaTKS5JsmfStPDbdJWBGK826VvoPDyFqcKbh85cqSvr4986ltP7Oj9znH2u1fpiVO6IrenrKQB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0EtpTzhJ1UtlSLf5klKQmng+ij9vZWn7bWGIXIvINPkUJPFCKU0HESW6329HPC8NgIgT3evSIp2XBkDkia7bAIBKgNsAmAncBsBM4DYAZgK3ATATuA2AmcBtAMwEbgNgJrF0uwZ5aSKIFgLxJ5Zuh8hLY4wF3t7FXfFNEN5t3Z0dADSa6F5PHkCIvDTGWAVuC+UCbouYspLJJLp4EEmiex9YZXlpjLGau+3UEa2sNQAKmJqXxhgL6TaVc+YazYWPqPrq8xcAiAKm5qUxxsK7reScaSNN+OWJYDEAooOpeWmMsVL6bSlToZjbUmIT3AYRxNS8NMYEAxVFae1Dui1GoCCHBEQSU/PSmPRFlhApJsQEl9Nv86DfBtEkuufJK8lLc0+CiVLysbxi6H+A24U+Wq8v3AbRJLrfbyMvDYBKiO51achLA6ASous2AKAS4DYAZgK3ATATuA2AmcBtAMwEbgNgJnAbADOB2wCYSZO7nUn5v0LvXDmKX5YGhhB1t1/bttabalA93AbGEt3ryRlj+x++KeBhNSi47QO3gSFE9z6w13rX5ud25ed2LU1ac+N9M0c7Jg888lrvWn6quC1wGxhLdPPSXultzS8dD5he6W0NWk3Otjvbx3LBbeHH5PwN2/79oPAcxJTo5qW98lRrfu5AwDT4uRuLrWp2rL9toLNtoDM9TReg3U7wgUmwG8ST6Oal/fSp1vzMHm/av3WNM6klyZkCOdvubMtOqE/o+m2vAIboIK5ENy9t/9bb8pOWM+3fuiY/s8uZGGPXPj2RWLUjsWrHtU9PsCC3y+y34TYwgOjmpe17+Kb8ZF9+sm//1jXOP87EGPOsTqzaoV2+guNtz2YxZQ2AOBHd8+SDn/vD/HjH/q1r8uNd/OQVSFy3+4NfebOytmj67RROpYHYE93vt7/9mRv2b12TH++QJudZr+sGAJBE+rq0wdQt6tToRgEQDyLtdgCJVTsS1+9tdCsAiC5xdRsAEAzcBsBM4DYAZgK3ATCT/wcqBKVsVciCCwAAAABJRU5ErkJggg==" alt="" />

先给出错误案例:

我最先的想法是:只要在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的一些疑惑(很重要)的更多相关文章

  1. 关于Unicode和URL encoding入门的一切以及注意事项

    本文同时也发表在我另一篇独立博客 <关于Unicode和URL encoding入门的一切以及注意事项>(管理员请注意!这两个都是我自己的原创博客!不要踢出首页!不是转载!已经误会三次了! ...

  2. BAT批处理(一)

    本文摘自博文<BAT批处理文件教程> 这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,只要你识字就能看懂,就能学到知识.写这篇教程的目的,是让每一个看过这些文字的朋友记住一句话:如 ...

  3. HTML5_用语义化标记重新定义博客

    HTML5文档的第一行便是文档类型声明,文档类型声明的作用有两个 一:验证器依据文档类型来判断采用何种验证规则去验证代码 二:文档类型声明能够强制IE6,IE7,IE8以“标准模式”渲染页面 1: & ...

  4. Bootstrap后台使用问题汇总(一)

    第一次自己汇总写博客啊,不懂规矩,大家包涵~~ 最近进行的项目中需要一个后台,于是在网上Down了许多Bootstrap后台源码.精挑细选决定用“ACE后台管理系统”(因为是中文的,英文三级狗的小鹿还 ...

  5. Struts2技术内幕 读书笔记三 表示层的困惑

    表示层能有什么疑惑?很简单,我们暂时忘记所有的框架,就写一个注册的servlet来看看. index.jsp <form id="form1" name="form ...

  6. 批处理最完整人性化教程(.bat文件语法)

    原文链接:http://www.cnitblog.com/seeyeah/archive/2009/01/15/53808.html 这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,你要你识字 ...

  7. BAT编程

    echo 表示显示此命令后的字符  echo off 表示在此语句后所有运行的命令都不显示命令行本身  @与echo off相象,但它是加在每个命令行的最前面,表示运行时不显示这一行的命令行(只能影响 ...

  8. 快速定位oracle故障-恩墨

    首先我们要明白一点,所谓的故障,意味着相对来讲比较严重.也就是可能比不同的问题要严重一些,比如锁等待. 要能够快速的定位和解决问题,恢复业务正常:首先我们需要了解Oracle的一些常见的故障有哪些. ...

  9. django modelform中的self.instance

    在stackoverflow上看到一个问题,正好是我疑惑很久的相关问题. [原问题地址]https://stackoverflow.com/questions/18265023/self-instan ...

随机推荐

  1. hdu 4287Intelligent IME(简单hash)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. Java基础语法(第1章变量)

    今日内容介绍 1.变量 2. 运算符   变量 1.1.变量概述 前面我们已经学习了常量,接下来我们要学习变量.在Java中变量的应用比常量的应用要多很多.所以变量也是尤为重要的知识点! 什么是变量? ...

  3. MySQL联合多表更新和删除

    多表更新 在 MySQL 3.23 中,你能够使用 LIMIT # 来确保仅仅有给定的记录行数目被更改. 假设一个 ORDER BY 子句被使用(从 MySQL 4.0.0 開始支持),记录行将以指定 ...

  4. hdu 1087 简单dp

    思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<strin ...

  5. CentOS 开机启动

    # vim /etc/rc.local 加入你想在开机后执行的脚本 source /etc/profile /usr/local/xx.sh 写第一句是想保证在执行脚本前系统环境变量有效,第二句才是真 ...

  6. Coreseek安装测试配置指南(转)

    Sphinx--强大的开源全文检索引擎,Coreseek--免费开源的中文全文检索引擎 软件版本:coreseek-4.1 mmseg-3.2.14 autoconf-2.64 老版本的coresee ...

  7. 工作总结 Rezor 里面的一些小知识----自定义类型 放在标签值中 会直接跳过去

    0 的时候不报错 1 的时候 报错了 原因 是 imagesname[i]  索引超出了 为什么在 上面 报错呢?  不在这里报错呢? 说明了  Rezor 对于 自定义的变量 放在标签值里的时候,调 ...

  8. const与#define、结构体对齐、函数重载name mangling、new/delete 等

    一.bool 类型 逻辑型也称布尔型,其取值为true(逻辑真)和false(逻辑假),存储字节数在不同编译系统中可能有所不同,VC++中为1个字节. 声明方式:bool result; result ...

  9. Linux下connect超时处理

    1.前言 最近在写一个测试工具,要求快速的高效率的扫描出各个服务器开放了哪些端口.当时想了一下,ping只能检测ip,判断服务器的网络是连通的,而不能判断是否开放了端口.我们知道端口属于网络的传输层, ...

  10. CSS之千变万化的Div

    厂址:http://www.cnblogs.com/yunfeifei/p/4671930.html 一.div和css3在一起 .box1 { width: 100px; height: 100px ...