Spring注解方式bean容器管理
1.通过在配置文件中配置spring组件注入
<context:component-scan base-package="com"/><!---通过该语句可以搜索com及com下子包中的类-> <mvc:annotation-driven/>
2.为Spring编写简单bean类,一般对应接口与具体实现类
例如:
a.在com包下建立如下接口:HessianHelloWorld
b.com.impl包下新建如下实现类:HessianHelloWorldImpl
3.在此处使用注解形式进行来支持bean的管理
在具体需要托管的bean的类名上添加注解:@Controller
完整代码如下:
@Controller public class HessianHelloWorldImpl implements HessianHelloWorld{}
4.在业务类中调用上面的托管bean,以动态代理的形式引入;
private HessianHelloWorld hello;
并在属性上添加注解
@Autowired
完整代码如下:
@Autowired private HessianHelloWorld hello;
注解方式结束
Spring配置文件形式:
1.去除上面的所有注解
在Spring配置文件中添加类的配置
<bean id="hessianHelloWorldImpl" class="com.impl.HessianHelloWorldImpl"></bean>
2.在Spring配置文件中添加业务类的配置
业务类名为Test,在业务类中配置属性(helloworld)及属性指向托管bean的id,其中属性helloworld在业务类中命名必须一致,且有该属性的get/set方法
<bean id="test" class="com.test.Test"> <property name="helloWorld" ref="hessianHelloWorldImpl"></property> </bean>
3.在Test.java中添加
private HessianHelloWorld helloWorld;
与get/set方法
private HessianHelloWorld helloWorld; public HessianHelloWorld getHelloWorld() { return helloWorld; } public void setHelloWorld(HessianHelloWorld helloWorld) { this.helloWorld = helloWorld; }
配置文件方式结束
注意事项
在此过程中需要注意注解方式与配置文件配置方式混合使用(由于业务需求的不同,例如注解方式的情况下使用定时器时就存在混合使用的情况)时的命名规范,当使用注解方式时bean在spring bean容器中的id首字母小写的类全名,而通过配置文件配置时id为自定义。
如下实例为同时使用了注解方式与配置方式:
1.在spring配置文件中添加如下配置:
<bean id="helloWorld" class="com.impl.HessianHelloWorldImpl" />
2.在托管bean(HessianHelloWorldImpl)上添加注解
@Controller public class HessianHelloWorldImpl implements HessianHelloWorld{}
这样HessianHelloWorldImpl在Spring的bean容器中注册了两个实例。即(helloWorld与hessianHelloWorldImpl)
3.在业务类(调用类)的属性上添加标注,即不通过在配置文件中配置hello对应的类,而通过标签自动匹配。
@Autowired private HessianHelloWorld hello;
启动时会报
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.HessianHelloWorld] is defined: expected single matching bean but found 2: [helloWorld, hessianHelloWorldImpl]
由于此处的指针名为hello,通过自动匹配的方式无法确认具体需要用到哪个实例
在混用的情况下需要对bean实例的命名与bean的名称管理。
上述情况不使用@Controller,直接在配置文件中注册bean(bean的id不为hello),即一个bean在配置文件中注册了两次。
<bean id="hessianHelloWorldImpl" class="com.remote.impl.HessianHelloWorldImpl"></bean>
也会出现同样的效果。
如果必须使用混用,可以在业务类(调用类)的属性名与bean容器中的名称相同
@Autowired private HessianHelloWorld helloWorld;
或者去除@Autowired直接在spring的bean配置文件中指定业务类属性对应的实例名称
<bean id="test" class="com.test.Test"> <property name="hello" ref="hessianHelloWorldImpl"></property> </bean>
常见错误:
通过配置文件配置bean的使用时
项目启动时报错:
Cannot resolve reference to bean 'hessianHelloWorldImpl' while setting bean property 'hello'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hessianHelloWorldImpl' is defined
spring找不到该类名,需要检查spring配置文件中bean的配置,若使用标注的则需要检查
<context:component-scan base-package="com"/>
<mvc:annotation-driven/>
是否已经添加
当业务调用类中使用标签时(@Autowired),可能在启动时不会报错,但是调用时会出现空指针异常,也可能是因为和上面的情况一样未指定bean的缘故
相关推荐
此工具自动生成非注解时Spring整合MyBatis时需要的一些配置文件,对于使用较新版本的spring,可以忽视生成的dao的配置文件,建议采用spring的扫描方式。生成的GeneratorConfigXML修改jdbc驱动位置后可直接用使用...
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
Spring框架xml注解配置方式实例,包括Maven配置文件pom.xml、Springxml配置文件ApplicationContext-1.xml,以及类源码
spring+springMVC+BlazeDS注解方式的配置文件.
NULL 博文链接:https://wenkaixuan.iteye.com/blog/906290
配置文件:jndi+spring注解配置
声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,更清爽。spring注解事务实现demo
SPRING注解json传值,界面出现中文乱码!
spring boot 纯注解方法事务控制回滚,注解+简单配置文件使用多线程demo
spring注入配置文件,使用注解注入的
对ssm需要用到的注解进行详细描述,以及分页的写法
简单的描述了spring mvc的配置示例,不包括注解方法和示例代码
引入属性文件,注解扫描包,配置阿里数据连接池,性能配置,配置sqlSessionFactory,将spring与mybatis结合,指定mybatis核心控制文件,事务控制
spring注解的使用,不用再写配置文件
spring常用注解,省去配置文件的麻烦,使用简洁的注解自动完成spring的配置
在xml配置文件中,这样加载properties文件 <bean id="propertyConfigurer" class="com.better517na.propertiesComponent.business.ExtendedPropertyPlaceholderConfigurer"> <value>classpath:...
n 高级特性-自定义PropertySourceFactory实现解析yaml配置文件 n 源码分析-BeanFactory类视图和常用工厂说明 n 源码分析-AnnotationConfigApplicationContext的register方法 n 源码分析-...
在Spring3中配置声明式事务比早期版本显得更加简便。只需要几行配置文件+注解就可以实现面向切面的AOP事务
在Spring Boot中,可以使用配置文件来管理应用程序的配置。这些配置文件可以根据不同的环境进行分离和管理,Spring Boot提供了多种灵活的方式来实现多环境配置。 1. 配置文件的命名 默认配置文件:application....
Spring 2.0当中为缩减XML配置文件数量引入定制命名空间功能,从此它便深深植根于核心Spring框架(aop、context、jee、jms、 lang、tx和util命名空间)、Spring Portfolio项目(例如Spring Security)和非Spring项目...