Spring4 notes-three methods of injecting collaborator Bean using assembly, spring4bean

Source: Internet
Author: User

Spring4 notes-three methods of injecting collaborator Bean using assembly, spring4bean

 

An exception is thrown when the two names are the same:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name 'd' is already used in this <beans> elementOffending resource: class path resource [beans.xml]    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:301)    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.checkNameUniqueness(BeanDefinitionParserDelegate.java:511)    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:457)    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseBeanDefinitionElement(BeanDefinitionParserDelegate.java:429)    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:299)    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:190)    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:169)    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:612)    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:513)    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)    at com.ij34.bean.test.main(test.java:12)
View Code

Interface

package com.ij34.interfaces;public interface dog { public String run();}
View Code
package com.ij34.interfaces;public interface people {   public void test();}
View Code

Object

Package com. ij34.servce; import com. ij34.interfaces. dog; public class Dog implements dog {private String name; public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Overridepublic String run () {// TODO Auto-generated method stub return "I'm an Alaska Dog named:" + getName ();}}
View Code
Package com. ij34.servce; import com. ij34.interfaces. dog; public class Dog2 implements dog {private String name; public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Overridepublic String run () {// TODO Auto-generated method stub return "I'm a husky dog named: "+ getName ();}}
View Code
Package com. ij34.servce; import com. ij34.interfaces. dog; import com. ij34.interfaces. people; public class People implements people {private dog d; public dog getD () {return d;} public void setD (dog d) {this. d = d ;}@ Override public void test () {// TODO Auto-generated method stub System. out. println ("I Am a programmer:" + getD (). run ());}}
View Code
<? Xml version = "1.0" encoding = "GBK"?> <Beans xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://www.springframework.org/schema/beans" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id = "chinese" class = "com. ij34.servce. people "autowire =" byName "/> <! -- Or byType --> <bean name = "d" class = "com. ij34.servce. dog "> <property name =" name "value =" cowboy 1 "> </property> </bean> <bean name =" d1 "class =" com. ij34.servce. dog2 "> <! -- Add multiple, and add the following content: autowire-candidate = "false" --> <property name = "name" value = "Daha 2 dog"> </property> </bean> </beans>

 

 

Run

package com.ij34.bean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ij34.servce.People;public class test {    public static void main(String[] args) {        // TODO Auto-generated method stub          @SuppressWarnings("resource")        ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");        People p=app.getBean("chinese", People.class);        p.test();    }}
View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.