Java 如何使用Spring context.xml实例化类
我真的不知道从哪里开始,因为我在春天是非常新的 目前我正在实例化一个类似于normal的类Java 如何使用Spring context.xml实例化类,java,spring,spring-mvc,Java,Spring,Spring Mvc,我真的不知道从哪里开始,因为我在春天是非常新的 目前我正在实例化一个类似于normal的类 ClassImpl newImpl = new ClassImpl(); 我希望通过Spring的context.xml实现这一点,因此我将类作为bean加载- <bean id="ClassId" class="ClassImpl"></bean> 如何利用我已将ClassImpl作为bean传入的事实来知道它的实例化?也就是说,我如何让Spring注入一个新的Class
ClassImpl newImpl = new ClassImpl();
我希望通过Spring的context.xml实现这一点,因此我将类作为bean加载-
<bean id="ClassId" class="ClassImpl"></bean>
如何利用我已将ClassImpl作为bean传入的事实来知道它的实例化?也就是说,我如何让Spring注入一个新的ClassImpl来给newImpl一个新的ClassImpl 您可以执行以下操作:
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:" + [package_name/context.xml]);
ClassImpl newImpl = (ClassImpl) ctx.getBean("ClassId")
如果您是从项目外部的context.xml加载它,我相信您可以做到
ApplicationContext ctx = new FileSystemXmlApplicationContext(path_to_context.xml);
ClassImpl newImpl = (ClassImpl) ctx.getBean("ClassId")