采用Spring管理Bean和依赖注入
2014-06-17 12:39
615 查看
1.实例化spring容器 和 从容器获取Bean目标实例化Spring容器常用的两种办法:办法一:在类途径下寻觅配置文件来实例化容器 [引荐运用]ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});办法二:在文件体系途径下寻觅配置文件来实例化容器 [这种办法能够在开发期间运用]ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});Spring的配置文件能够指定多个,能够经过String数组传入。
当spring容器启动后,由于spring容器能够办理bean目标的创立,毁掉等生命周期,所以咱们只需从容器直接获取Bean目标就行,而不必编写一句代码来创立bean目标。从容器获取bean目标的代码如下:ApplicationContext ctx = new ClassPathXmlApplicationContext(“beans.xml”);OrderService service = (OrderService)ctx.getBean("personService");
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=" http://www.metabase.cn/schema/beans"<beans xmlns=" http://www.jinanwuliangye.com/schema/beans"<beans xmlns=" http://www.tongxinglong.com/schema/beans"<beans xmlns=" http://www.qclchina.com/schema/beans"<beans xmlns=" http://www.vipfuxin.com//schema/beans"xsi:schemaLocation=" http://www.minnan888.net/schema/mvc/spring-mvc-3.2.xsd http://www.lcsyt.com/schema/mvc/spring-mvc-3.2.xsd http://lf.yunnanw.cn//schema/mvc/spring-mvc-3.2.xsd http://www.ceocbs.com/schema/mvc/spring-mvc-3.2.xsd http://www.yzbljp.com/schema/mvc/spring-mvc-3.2.xsd http://www.hyyfscl.com//schema/mvc/spring-mvc-3.2.xsdhttp://www.shoudashou.com//schema/mvc/spring-mvc-3.2.xsdhttp://www.shuoma.com.cn//schema/mvc/spring-mvc-3.2.xsd "> <!-- @Controller, @Service, @Configuration, etc. --> <!-- 启动Spring注解功能 --> <context:component-scan base-package="com.yeeeueyf.controller" />
<mvc:annotation-driven /><mvc:default-servlet-handler />
当spring容器启动后,由于spring容器能够办理bean目标的创立,毁掉等生命周期,所以咱们只需从容器直接获取Bean目标就行,而不必编写一句代码来创立bean目标。从容器获取bean目标的代码如下:ApplicationContext ctx = new ClassPathXmlApplicationContext(“beans.xml”);OrderService service = (OrderService)ctx.getBean("personService");
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=" http://www.metabase.cn/schema/beans"<beans xmlns=" http://www.jinanwuliangye.com/schema/beans"<beans xmlns=" http://www.tongxinglong.com/schema/beans"<beans xmlns=" http://www.qclchina.com/schema/beans"<beans xmlns=" http://www.vipfuxin.com//schema/beans"xsi:schemaLocation=" http://www.minnan888.net/schema/mvc/spring-mvc-3.2.xsd http://www.lcsyt.com/schema/mvc/spring-mvc-3.2.xsd http://lf.yunnanw.cn//schema/mvc/spring-mvc-3.2.xsd http://www.ceocbs.com/schema/mvc/spring-mvc-3.2.xsd http://www.yzbljp.com/schema/mvc/spring-mvc-3.2.xsd http://www.hyyfscl.com//schema/mvc/spring-mvc-3.2.xsdhttp://www.shoudashou.com//schema/mvc/spring-mvc-3.2.xsdhttp://www.shuoma.com.cn//schema/mvc/spring-mvc-3.2.xsd "> <!-- @Controller, @Service, @Configuration, etc. --> <!-- 启动Spring注解功能 --> <context:component-scan base-package="com.yeeeueyf.controller" />
<mvc:annotation-driven /><mvc:default-servlet-handler />
相关文章推荐
- 框架学习之Spring 第二节 采用Spring管理Bean和依赖注入
- 详细介绍采用Spring管理Bean和依赖注入方式
- 框架学习之Spring 第二节 采用Spring管理Bean和依赖注入
- 采用Spring管理Bean和依赖注入
- 采用Spring管理Bean和依赖注入
- 采用Spring管理Bean和依赖注入
- 框架学习之Spring 第二节 采用Spring管理Bean和依赖注入
- 采用Spring管理Bean和依赖注入
- 框架学习之Spring 第二节 采用Spring管理Bean和依赖注入
- 采用Spring管理Bean和依赖注入
- spring boot 多模块管理时,依赖注入bean失败
- Spring笔记——模拟spring的bean管理原理以及依赖注入原理
- Spring的依赖注入和管理Bean
- spring boot 多模块管理时,依赖注入bean失败
- 俩句话说清楚spring管理bean和依赖注入的原理
- spring的管理bean和依赖注入的原理剖析
- Spring中管理Bean依赖注入之后和Bean销毁之前的行为
- 【第十二章】零配置 之 12.1 概述 12.2 注解实现Bean依赖注入 ——跟我学spring3
- 一个和spring整合的依赖注入的servlet,子类可自动装配依赖Bean
- spring依赖注入单元测试:expected single matching bean but found 2