`
大玉儿
  • 浏览: 20892 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

spring bean id和bean name的区别

阅读更多

1. Spring bean defination

一般情况下,我们定义和应用bean都使用bean id,但是今天我遇到了这样的定义

<bean id = "writeService" class = "aServiceImp, bServiceImp, cServiceImp"
           name = "aService, bService, cService"  init-method = "init">
           <property name = "interfaceName">
                     <value>baseService></value>
           </property>
           ............
</bean>

 

这样的定义首先表明aService, bService, cService都implements interface baseService

用spring IOC注入的时候,既可以用id writeService,也可以用name aService/bService/cService

 

bean id 和 bean name到底有什么区别呢?

Google和百度都找不到我想要的答案,最后还是在spring的官网上找到了如下的解释,我自己理解为:bean name的定义是合法的,但是一个bean name不能唯一的代表一个bean,也就是多个bean可以拥有相同的bean name,但是bean id是唯一标示一个bean的,所以我们应该尽量避免用bean name来引用bean。

 

原文解释:

For a fallback match, the bean name is considered as a default qualifier value. This means that the bean may be defined with an id "main" instead of the nested qualifier element, leading to the same matching result. However, note that while this can be used to refer to specific beans by name, @Autowired  is fundamentally about type-driven injection with optional semantic qualifiers. This means that qualifier values, even when using the bean name fallback, always have narrowing semantics within the set of type matches; they do not semantically express a reference to a unique bean id. Good qualifier values would be "main" or "EMEA" or "persistent", expressing characteristics of a specific component - independent from the bean id (which may be auto-generated in case of an anonymous bean definition like the one above).

 

 

2
0
分享到:
评论

相关推荐

    Spring如何获取Bean

    对Spring如何获取Bean做一下代码明细

    通过@Autowired注解注入bean的顺序,以及@bean注入.rar

    通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合

    Spring Boot: Bean definition overriding

    因此,我们有2种配置,它们使用名称beanName实例化bean,在主应用程序中,我们仅打印该bean的值(非常重要的是,它们都具有相同的名称)。 那么您认为将要打印什么? 示例1 @SpringBootApplication public class ...

    Spring高版本对ibatis的支持

    &lt;bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"&gt; &lt;property name="configLocation"&gt; &lt;value&gt;classpath:SqlMapConfig.xml &lt;/bean&gt; &lt;bean id="dao" class="org....

    spring自动装配例子

    &lt;bean id="xxx" class="xxx" &gt;&lt;/bean&gt; (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 &lt;bean class="xxx" &gt;&lt;/bean&gt; (4)constructor:与 byType 方式类似,不同之处是构造注入 &lt;bean ...

    Spring3.2.4+Quartz2.2.0 Demo

    &lt;bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="triggers"&gt; &lt;ref bean="myJobTrigger" /&gt; ...

    spring-xmemcached

    &lt;bean id="cacheProxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"&gt; &lt;property name="interceptorNames"&gt; &lt;value&gt;cacheBeforeAdvice &lt;value&gt;cacheAfterAdvice &lt;value&gt;...

    spring applicationContext 配置文件

    &lt;bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" p:dataSource-ref="dataSourceProxy"&gt; &lt;property name="configLocation"&gt; &lt;value&gt;classpath:SqlMapConfig.xml...

    spring定时任务

    &lt;bean id="sayHelloJob" class="org.springframework.scheduling.quartz.JobDetailBean"&gt; &lt;property name="jobClass"&gt; &lt;value&gt;com.test.Test&lt;/value&gt; &lt;/bean&gt; &lt;!-- 关键在如下两个触发器的配置 --&gt; &lt;!-...

    springjdbc

    &lt;bean id="querytestController" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt; &lt;property name="targetObject" ref="testController" /&gt; &lt;property name="targetMethod" ...

    spring-agent.jar

    &lt;bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="loadTimeWeaver"&gt; &lt;bean class="org.springframework.instrument.classloading....

    springweb-Jackson

    &lt;bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /&gt; &lt;bean id="stringHttpMessageConverter" class="org.spring...

    Spring中的Bean的管理_Bean的装配方式_基于注解的装配_项目

    运行环境eclipse。其Dynamic Web Project,Target Runtime为Apache Tomcat v8.5,...User [id=1, name=张三, password=123] userDao say hello world! UserService say hello world userController say Hello world

    Spring循环依赖报错Bean with name ‘**’ has been injected into other beans [**] in its raw version as part

    Bean with name ‘commonService’ has been injected into other beans [] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use ...

    spring和hibernate整合

    &lt;param-value&gt;/WEB-INF/spring/*.bean.xml org.springframework.web.context.ContextLoaderListener &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.request....

    spring源码解决

    //这里根据bean的名字和Class类型来得到bean实例,和上面的方法不同在于它会抛出异常:如果根据名字取得的bean实例的Class类型和需要 Object getBean(String name, Class requiredType) throws BeansException; //...

    Spring MVC 入门实例

    11 &lt;bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt; 12 &lt;property name="mappings"&gt; 13 14 &lt;prop key="/hello.do"&gt;helloController 15 16 17 &lt;/...

    jdbc-ldap spring

    &lt;bean id="ldapDataSource" class="org.spring... &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt; &lt;property name="dataSource"&gt; &lt;ref bean="ldapDataSource" /&gt; &lt;/bean&gt;

    spring-scopes:自定义 Spring Bean 范围

    自定义 Spring Bean Scopes 用于默​​认设置不起作用的情况 可用范围 Route Scope - 提供每个路由执行范围的范围 页面范围 - 提供每页范围的范围 线程范围 - 提供每个线程范围的范围 继承的线程范围 - 提供每个线程...

Global site tag (gtag.js) - Google Analytics