标签:edit ready ini null cto end 结果 source enc
???? ??????
共五种值可供选择,属于全局的设置
- 复用的BeanA类
public class BeanA {
private BeanB beanB;
private BeanC beanC;
public BeanB getBeanB() {
return beanB;
}
public BeanA setBeanB(BeanB beanB) {
this.beanB = beanB;
return this;
}
public BeanC getBeanC() {
return beanC;
}
public BeanA setBeanC(BeanC beanC) {
this.beanC = beanC;
return this;
}
}
- 复用的Test 类
import com.vi.springbean.BeanA;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Test {
@org.junit.Test
public void test() {
String path = "spring-config.xml";
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(path));
BeanA bean = beanFactory.getBean(BeanA.class);
System.out.println("beanB "+bean.getBeanB());
System.out.println("beanC "+bean.getBeanC());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byType">
<bean id="beanA" class="com.vi.springbean.BeanA"></bean>
<bean id="beanBB" class="com.vi.springbean.BeanB"></bean>
<bean id="beanC" class="com.vi.springbean.BeanD"></bean>
</beans>
运行结果:
beanB com.vi.springbean.BeanB@568bf312
beanC null
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byType">
<bean id="beanA" class="com.vi.springbean.BeanA"></bean>
<bean id="beanBB" class="com.vi.springbean.BeanB"></bean>
<bean id="beanCC" class="com.vi.springbean.BeanB"></bean>
</beans>
说明可以找不到,但是不能存在多个类型一样的,匹配时与bean的id无关
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<bean id="beanA" class="com.vi.springbean.BeanA"></bean>
<bean id="beanB" class="com.vi.springbean.BeanB"></bean>
<bean id="beanC2" class="com.vi.springbean.BeanB"></bean>
</beans>
运行结果:
beanB com.vi.springbean.BeanB@5d47c63f
beanC null
可以找不到,且可以存在多个同类型的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<bean id="beanA" class="com.vi.springbean.BeanA"></bean>
<bean id="beanB" class="com.vi.springbean.BeanB"></bean>
<bean id="beanC" class="com.vi.springbean.BeanD"></bean>
</beans>
运行结果:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘beanA‘ defined in class path resource [spring-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type ‘com.vi.springbean.BeanD‘ to required type ‘com.vi.springbean.BeanC‘ for property ‘beanC‘; nested exception is java.lang.IllegalStateException: Cannot convert value of type ‘com.vi.springbean.BeanD‘ to required type ‘com.vi.springbean.BeanC‘ for property ‘beanC‘: no matching editors or conversion strategy found
说明当通过name匹配到后,类型不能不符也是错误的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<bean id="beanA" class="com.vi.springbean.BeanA"></bean>
<bean id="beanB" class="com.vi.springbean.BeanB"></bean>
<bean id="beanB" class="com.vi.springbean.BeanC"></bean>
</beans>
运行结果:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name ‘beanB‘ is already used in this
Offending resource: class path resource [spring-config.xml]
此时按照name去匹配的,bean的id和BeanA的中的属性 name一致即可注入,可以找不到,找到之后类型需要一致,xml中bean id的值需保证唯一性
// TODO
// TODO
// TODO
标签:edit ready ini null cto end 结果 source enc
原文地址:https://www.cnblogs.com/VVII/p/12109779.html