
Empty Project–next











Application context not configured for this file警告处理
spring配置文件中时常会出现这个提示,翻译过来大概意思就是没有配置该文件到项目中
进入到File →Project Structure中查看



配置文件名称:applicationContext.xml【beans.xml或spring.xml】
配置文件路径:src/main/resources



ApplicationContext iocObj = new ClassPathXmlApplicationContext("applicationContext.xml");getBean(String beanId,Clazz clazz):通过beanId和Class获取对象(推荐用这个)import com.atguigu.spring.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring {@Testpublic void testSpring(){
// 使用Spring之前
// Student student = new Student();// 使用Spring之后
// 创建容器对象ApplicationContext iocObj = new ClassPathXmlApplicationContext("applicationContext.xml");// 通过容器对象,获取需要对象// 方式一:getBean(String beanId):通过beanId获取对象
// - 不足:需要强制类型转换,不灵活
// Student stuZhenzhong = (Student)iocObj.getBean("stuZhenzhong");
// System.out.println("stuZhenzhong = " + stuZhenzhong);// 方式二:getBean(Class clazz):通过Class方式获取对象
// - 不足:容器中有多个相同类型bean的时候,会报如下错误:expected single matching bean but found 2: stuZhenzhong,stuZhouxu
// Student bean = iocObj.getBean(Student.class);
// System.out.println("bean = " + bean);// 方式三:getBean(String beanId,Clazz clazz):通过beanId和Class获取对象(推荐用这个)Student stuZhenzhong = iocObj.getBean("stuZhenzhong",Student.class);System.out.println("stuZhenzhong = " + stuZhenzhong);}
}
上一篇:【论文翻译】ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation
下一篇:JDK动态代理CGLIB动态代理