文章目录
spring获取bean对象的3钟方式
bean.xml:
css
<?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">
<!--创建user对象
id唯一标识
class:类的全路径
-->
<bean id="user" class="com.guigu.User"></bean>
</beans>
1.根据id获取bean对象
java
import com.guigu.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
public static void main(String[] args) {
// 获取spring配置文件bean.xml
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//获取bean.xml中创建的对象
//第一中获取方式:根据id获取bean对象
//<bean id="user" class="com.guigu.User"></bean>
User user = (User) context.getBean("user");
System.out.println("根据id获取对象" + user);
// 第二中方式:根据类型获取bean对象
User user2 = context.getBean(User.class);
System.out.println("根据类型获取对象" + user2);
// 根据id和类型获取bean对象
User user3 = context.getBean("user", User.class);
System.out.println("根据id和类型获取对象" + user2);
}
}
2.根据类型获取bean对象
javascript
import com.guigu.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
public static void main(String[] args) {
// 获取spring配置文件bean.xml
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//获取bean.xml中创建的对象
//第一中获取方式:根据id获取bean对象
//<bean id="user" class="com.guigu.User"></bean>
User user = (User) context.getBean("user");
System.out.println("根据id获取对象" + user);
// 第二中方式:根据类型获取bean对象
User user2 = context.getBean(User.class);
System.out.println("根据类型获取对象" + user2);
// 根据id和类型获取bean对象
User user3 = context.getBean("user", User.class);
System.out.println("根据id和类型获取对象" + user2);
}
}
3.根据id和类型获取bean对象
kotlin
import com.guigu.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
public static void main(String[] args) {
// 获取spring配置文件bean.xml
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//获取bean.xml中创建的对象
//第一中获取方式:根据id获取bean对象
//<bean id="user" class="com.guigu.User"></bean>
User user = (User) context.getBean("user");
System.out.println("根据id获取对象" + user);
// 第二中方式:根据类型获取bean对象
User user2 = context.getBean(User.class);
System.out.println("根据类型获取对象" + user2);
// 根据id和类型获取bean对象
User user3 = context.getBean("user", User.class);
System.out.println("根据id和类型获取对象" + user2);
}
}
@注意
根据类型获取对象时候springIoc中bean对象有且只有一个,否则异常。
bean.xml
xml
<?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">
<!--创建user对象
id唯一标识
class:类的全路径
-->
<bean id="user1" class="com.guigu.User"></bean>
<bean id="user" class="com.guigu.User"></bean>
</beans>
测试:
java
// 第二中方式:根据类型获取bean对象
User user2 = context.getBean(User.class);
System.out.println("根据类型获取对象" + user2);
结果:
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.guigu.User' available: expected single matching bean but found 2: user1,user
当bean对象class同名的情况下可以根据id、id和类型等两种方式进行获取bean对象即可,