http://tom-seed.iteye.com/blog/1584632

Spring注解方式bean容器管理

1.通过在配置文件中配置spring组件注入

  1. <context:component-scan base-package="com"/><!---通过该语句可以搜索com及com下子包中的类->
  2. <mvc:annotation-driven/>

2.为Spring编写简单bean类,一般对应接口与具体实现类

例如:

a.在com包下建立如下接口:HessianHelloWorld

b.com.impl包下新建如下实现类:HessianHelloWorldImpl

3.在此处使用注解形式进行来支持bean的管理

在具体需要托管的bean的类名上添加注解:@Controller

完整代码如下:

  1. @Controller
  2. public class HessianHelloWorldImpl implements HessianHelloWorld{}

4.在业务类中调用上面的托管bean,以动态代理的形式引入;

private HessianHelloWorld hello;

并在属性上添加注解

@Autowired

完整代码如下:

  1. @Autowired
  2. private HessianHelloWorld hello;

注解方式结束

Spring配置文件形式:

1.去除上面的所有注解

在Spring配置文件中添加类的配置

  1. <bean id="hessianHelloWorldImpl" class="com.impl.HessianHelloWorldImpl"></bean>

2.在Spring配置文件中添加业务类的配置

业务类名为Test,在业务类中配置属性(helloworld)及属性指向托管bean的id,其中属性helloworld在业务类中命名必须一致,且有该属性的get/set方法

  1. <bean id="test" class="com.test.Test">
  2. <property name="helloWorld" ref="hessianHelloWorldImpl"></property>
  3. </bean>

3.在Test.java中添加

private HessianHelloWorld helloWorld;

与get/set方法

  1. private HessianHelloWorld helloWorld;
  2. public HessianHelloWorld getHelloWorld() {
  3. return helloWorld;
  4. }
  5. public void setHelloWorld(HessianHelloWorld helloWorld) {
  6. this.helloWorld = helloWorld;
  7. }

配置文件方式结束

注意事项

在此过程中需要注意注解方式与配置文件配置方式混合使用(由于业务需求的不同,例如注解方式的情况下使用定时器时就存在混合使用的情况)时的命名规范,当使用注解方式时bean在spring bean容器中的id首字母小写的类全名,而通过配置文件配置时id为自定义。

如下实例为同时使用了注解方式与配置方式:

1.在spring配置文件中添加如下配置:

  1. <bean id="helloWorld" class="com.impl.HessianHelloWorldImpl" />

2.在托管bean(HessianHelloWorldImpl)上添加注解

  1. @Controller
  2. public class HessianHelloWorldImpl implements HessianHelloWorld{}

这样HessianHelloWorldImpl在Spring的bean容器中注册了两个实例。即(helloWorld与hessianHelloWorldImpl)

3.在业务类(调用类)的属性上添加标注,即不通过在配置文件中配置hello对应的类,而通过标签自动匹配。

  1. @Autowired
  2. private HessianHelloWorld hello;

启动时会报

  1. nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
  2. No unique bean of type [com.HessianHelloWorld] is defined:
  3. expected single matching bean but found 2: [helloWorld, hessianHelloWorldImpl]

由于此处的指针名为hello,通过自动匹配的方式无法确认具体需要用到哪个实例

在混用的情况下需要对bean实例的命名与bean的名称管理。

上述情况不使用@Controller,直接在配置文件中注册bean(bean的id不为hello),即一个bean在配置文件中注册了两次。

  1. <bean id="hessianHelloWorldImpl" class="com.remote.impl.HessianHelloWorldImpl"></bean>

也会出现同样的效果。

如果必须使用混用,可以在业务类(调用类)的属性名与bean容器中的名称相同

  1. @Autowired
  2. private HessianHelloWorld helloWorld;

或者去除@Autowired直接在spring的bean配置文件中指定业务类属性对应的实例名称

  1. <bean id="test" class="com.test.Test">
  2. <property name="hello" ref="hessianHelloWorldImpl"></property>
  3. </bean>

常见错误:

通过配置文件配置bean的使用时

项目启动时报错:

  1. Cannot resolve reference to bean 'hessianHelloWorldImpl' while setting bean property 'hello';
  2. nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
  3. No bean named 'hessianHelloWorldImpl' is defined

spring找不到该类名,需要检查spring配置文件中bean的配置,若使用标注的则需要检查

<context:component-scan base-package="com"/>
<mvc:annotation-driven/>

是否已经添加

当业务调用类中使用标签时(@Autowired),可能在启动时不会报错,但是调用时会出现空指针异常,也可能是因为和上面的情况一样未指定bean的缘故

Spring注解式与配置文件式的更多相关文章

  1. Spring 注解方式引入配置文件

    配置文件,我以两种为例,一种是引入Spring的XML文件,另外一种是.properties的键值对文件: 一.引入Spring XML的注解是@ImportResource @Retention(R ...

  2. spring注解注入properties配置文件

    早期,如果需要通过spring读取properties文件中的配置信息,都需要在XML文件中配置文件读取方式. 基于XML的读取方式: <bean class="org.springf ...

  3. Spring注解式事务解析

    #Spring注解式事务解析 增加一个Advisor 首先往Spring容器新增一个Advisor,BeanFactoryTransactionAttributeSourceAdvisor,它包含了T ...

  4. spring注解式参数校验

    很痛苦遇到大量的参数进行校验,在业务中还要抛出异常或者返回异常时的校验信息,在代码中相当冗长,今天我们就来学习spring注解式参数校验. 其实就是:hibernate的validator. 开始啦. ...

  5. MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式

    MVC的验证(模型注解和非侵入式脚本的结合使用)   @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...

  6. hibernate3整合spring2时hibernate即用注解又用配置文件情况时spring配置文件的配置写法

    hibernate只用注解时,spring的配置文件的配置如下 <bean id="dataSource" class="org.apache.commons.db ...

  7. Spring注解使用和与配置文件的关系

      Spring注解使用和与配置文件的关系 1 注解概述与容器管理机制 Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repositor ...

  8. 【spring boot】使用注解@ConfigurationProperties读取配置文件时候 报错 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rocketmqAutoConfiguration': Unsatisfied dependenc

    如题,配置文件如下: #注册中心配置 eureka: instance: instanceId: ${spring.application.name}:${random.int} hostname: ...

  9. 【Spring注解驱动开发】使用@PropertySource加载配置文件,我只看这一篇!!

    写在前面 很多小伙伴都在问:冰河,你的Spring专题更新完了吗?怎么感觉像是写了一半啊?我:没有更新完呀,整个专题预计会有70多篇.那怎么更新了一半就去写别的了呢?那是因为有很多其他的小伙伴在后台留 ...

随机推荐

  1. 倍福TwinCAT(贝福Beckhoff)基础教程5.1 TwinCAT-1 获取和设置系统时间

    使用功能块NT_GetTime,NETID填写两个单引号表示本机,START就是一个触发信号,一般的功能块都需要一个上升沿触发执行,最后的输出类型都是让系统自己决定,然后统一把这些变量放到全局变量中( ...

  2. 【转】oracle建立本地数据库

    来到公司的这段时间,在项目的开发过程中一直连接的是远程的oracle数据库,现在开始轮到自己做毕业设计了,自己想采用Oracle数据库,就想建立本地的数据库. 当然了,首先是自己先装Oracle客户端 ...

  3. 基于Saltstatck实现页面实时显示tomcat启动日志(17)

    一.相关文件 master端: /srv/salt/tomcat/start.sls               #tomcat启动服务state.sls,须要自己创建 /srv/salt/tomca ...

  4. python调度框架APScheduler使用详解

    # coding=utf-8 """ Demonstrates how to use the background scheduler to schedule a job ...

  5. iOS开发之状态栏隐藏(问题篇)

    一.基本应用 相信基本的隐藏办法网上很多,这里只简单说明一下 1⃣️改变全局状态栏 1.在项目的Info.plist文件里设置UIViewControllerBasedStatusBarAppeara ...

  6. C#自动切换Windows窗口程序,如何才能调出主窗口?

      namespace AutoChangeWindow { partial class Form1 { /// <summary> /// 必需的设计器变量. /// </summ ...

  7. 下拉刷新Listview(8.30)

    Android-PullToRefresh 1项目托管地址: https://github.com/bavariama1/Android-PullToRefresh 2 快速开始教程:https:// ...

  8. thinkphp5.0极速搭建restful风格接口层实例

    作为国内最流行的php框架thinkphp,很快就会发布v5.0正式版了,现在还是rc4版本,但已经很强大了下面是基于ThinkPHP V5.0 RC4框架,以restful风格完成的新闻查询(get ...

  9. 高盛CEO致大学毕业生:要与有野心的人为伍

    我认为讲的非常棒.年轻人就要这样. 高盛集团首席运行官(CEO)劳尔德-贝兰克梵(Lloyd Blankfein)周四在曼哈顿贾维茨中心參加了拉瓜迪亚社区大学的第41届毕业典礼并发表演讲.在面向约10 ...

  10. 使用第三方工具Cornerstone搭建本地SVNserver

    一.加入版本号资源库 点击Cornerstone左下角REPOSITORIES栏右边的加号button.在弹出的视图中选择File Repository,然后选择Create a New Reposi ...