1、获取bean对象的三种基本方式
0、前提准备
<?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">
<!--使用spring创建对象
id:名称一般采用类名的首字母小写
class:要创建对象的全限定类名
property:属性
name:对象的属性名称
value:想要赋值的值
创建完成之后会将其存放在ioc容器中
-->
<bean id="person" class="cn.simplelife.domain.Person">
<property name="id" value="1"/>
<property name="name" value="李四"/>
</bean>
</beans>
1、按照名称查找
ApplicationContext context = new ClassPathXmlApplicationContext("bean的配置文件名称")
context.getBean("person")
- 注意:返回值是Object需要强转为我们需要的类型。
2、按照类型查找
ApplicationContext context = new ClassPathXmlApplicationContext("bean的配置文件名称")
context.getBean(Person.class)
- 注意:如果在xml中配置了多个该类型的对象,就会报错。
3、按照名称和类型查找
ApplicationContext context = new ClassPathXmlApplicationContext("bean的配置文件名称")
context.getBean("person",Person.class)
2、常见异常
1、org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘person’ available
【错误示例】:
【错误原因】: 此时的错误是告诉我们在spring框架中没有叫做person的bean对象,也就是我们在xml配置中没有配置该对象。
【解决方案】: 在配置文件中配置该名称的bean对象。
2、No qualifying bean of type ‘cn.simplelife.domain.Person’ available: expected single matching bean but found 2: person,person1
【错误提示】:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.simplelife.domain.Person' available: expected single matching bean but found 2: person,person1
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1036)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:338)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:333)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1107)
at PersonTest.testPersonBySpring(PersonTest.java:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
【错误原因】: 我们根据类型获取bean对象的时候,如果我们在配置文件中配置了两个该类型的对象就会报此错误。
【解决方法】:
- 使用名称加,类型获取的方式进行获取。
- 修改配置文件,纸保留一个该类型的bean