目录
1.基础环境搭建
1)建立Maven模块
2)导入相关的依赖
<dependencies>
<!--导入spring-context依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.12</version>
</dependency>
<!--导入测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--导入日志框架-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.23.1</version>
</dependency>
</dependencies>
注:点击这里刷新一下,否则依赖不导入(右边栏Maven图标点击一下就出来这个界面了)
3)创建并编写springConfig.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">
<!-- id 定义bean的id -->
<!-- class (包名+类名) 即Users类所在的路径 -->
<bean id="user" class="org.xiji.User">
</bean>
</beans>
4)创建并log4j2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<!--配置控制台输出日志-->
<Console name="Console" target="SYSTEM_OUT">
<!--日志的输出格式-->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<!--配置文件输出日志-->
<File name="File" fileName="d:logs/app.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<!--日志级别为debug-->
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
5)创建user对象
package org.xiji;
public class User {
private String name;
private int age;
private String address;
public void eat(){
System.out.println("正在吃饭");
}
}
6)结构如下
2.springIoc获取Bean的三种方式
1)通过id获取bean
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiji.User;
public class Tests {
/**
* 通过id获取bean
*/
@Test
public void getBeanById(){
/**
* 通过加载MyConfig.xml配置文件创建
*/
ApplicationContext context = new ClassPathXmlApplicationContext("MyConfig.xml");
User user = (User) context.getBean("user");
System.out.println(user);
user.eat();
}
}
测试结果
2)通过类型获取bean
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiji.User;
public class Test2 {
/**
* 通过类型获取bean
*/
@Test
public void getBeanByType(){
/**
* 通过加载myConfig.xml配置文件创建
*/
ApplicationContext context = new ClassPathXmlApplicationContext("MyConfig.xml");
/**
* 通过类型获取bean
*/
User user = context.getBean(User.class);
System.out.println("通过类型获取bean"+user);
user.eat();
}
}
3)通过id+类型获取bean
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiji.User;
public class Test3 {
/**
* 通过类型+id获取bean
*/
@Test
public void getBeanByType(){
/**
* 通过加载myConfig.xml配置文件创建
*/
ApplicationContext context = new ClassPathXmlApplicationContext("MyConfig.xml");
/**
* 通过类型+id获取bean
*/
User user = context.getBean("user",User.class);
System.out.println("通过 类型+id 获取bean"+user);
user.eat();
}
}
测试结果
3.通过类型获取id的两种错误方式
基础准备
Food类
package org.xiji;
public interface Food {
void getFood();
}
Apple类
package org.xiji;
public class Apple implements Food{
@Override
public void getFood() {
System.out.println("苹果");
}
}
Banana类
package org.xiji;
public class Banana implements Food{
@Override
public void getFood() {
System.out.println("香蕉");
}
}
MyConfig.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">
<!-- id 定义bean的id -->
<!-- class (包名+类名) 即Users类所在的路径 -->
<bean id="user" class="org.xiji.User">
</bean>
<!--
错误类型1:一个类两个实例化 甚至是多个实例化
-->
<bean id="user2" class="org.xiji.User">
</bean>
<!--
错误类型2:一个接口两个实现类 甚至是多个实现类
-->
<bean id="food" class="org.xiji.Banana">
</bean>
<bean id="food2" class="org.xiji.Apple">
</bean>
</beans>
错误展示以及解决方案
1)错误类型1:一个类,两个或两个以上实例化
测试类
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiji.User;
public class TestError1 {
@Test
public void test()
{
/**
* 通过类型获取Bean
* 错误类型1:一个类,两个或两个以上实例化
*/
ApplicationContext context = new ClassPathXmlApplicationContext("MyConfig.xml");
User user = context.getBean(User.class);
user.eat();
}
}
运行结果
错误收集
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.xiji.User' available: expected single matching bean but found 2: user,user2
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1312)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:341)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1261)
at TestError1.test(TestError1.java:16)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
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:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
解决方案:
一 通过id获取Bean
二 通过id+该类 类型的方式获取bean
错误类型2:通过接口获取Bean,但是该接口有多个实现类
测试类
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiji.Food;
public class TestError2 {
/**
* 通过接口获取类
* 错误类型2:一个接口两个实现类 甚至是多个实现类
*/
@Test
public void test()
{
ApplicationContext context = new ClassPathXmlApplicationContext("MyConfig.xml");
Food bean = context.getBean(Food.class);
System.out.println("通过接口获取类"+bean);
}
}
运行结果
错误收集
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.xiji.Food' available: expected single matching bean but found 2: food,food2
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1312)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:341)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1261)
at TestError2.test(TestError2.java:17)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
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:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)