1
1 这种在WebServlet 的代码中的doget 或 dopost WebApplicationContext webApplicationContext=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); CartService cartService =(CartService)webApplicationContext.getBean("cartService"); 2 可以在其他任何代码中 ApplicationContext applicationContext= new ClassPathXmlApplicationContext("spring-config.xml"); CartService cartService=(CartService) applicationContext.getBean("cartService");
1注入方式
2自动注入方式 autowire
<!--<bean id="cartService" class="com.neuedu.Service.CartService" scope="prototype" init-method="init" destroy-method="destroy" autowire="byName">-->
<!--</bean>-->
<!--<bean id="cartService" class="com.neuedu.Service.CartService" scope="prototype" init-method="init" destroy-method="destroy" autowire="constructor">-->
<!--<property name="name" value="feixiaobo"></property>-->
<!--</bean>-->