如何配置Bean
来源:互联网 发布:直播间源码下载 编辑:程序博客网 时间:2024/05/18 00:07
配置Bean
-配置形式:基于XML文件的方式;
-Bean的配置方式:通过全类名(反射)
-IOC容器BeanFactory&ApplicationContext
-依赖注入的方式:属性注入;构造器注入
ApplicationContext.xml文件
<!-- 通过setter方法来配置bean属性 class:bean的全类名,通过反射的方式在IOC容器中建立Bean。所以要求Bean中必须有无参的构造器 id:标识容器中的bean,id唯一--><bean id="helloworld" class=""> <property name="name" value="Spring"></property></bean><!-- 通过构造方法来配置bean属性 --><bean id="XXX" class=""> <constructor-arg value="" index="0" ></constructor-arg> <constructor-arg value="" index="1"></constructor-arg> <constructor-arg value="" index="2"></constructor-arg></bean><!--使用构造器注入属性值可以指定参数的位置和参数的类型以区分重载的构造器!--><pre name="code" class="html"><bean id="XXX" class=""> <constructor-arg value="" type="java.lang.String" ></constructor-arg> <constructor-arg value="" type="java.lang.String"></constructor-arg> <constructor-arg value="" type="java.lang.double"></constructor-arg></bean>
Spring提供了两种类型的IOC容器实现。
-BeanFactory:IOC容器的基本实现。
-ApplicationContext:提供了更多的高级特性是BeanFactory的子接口。
-BeanFactory是Spring框架的基础设施,面向Spring本身;
ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext而非底层的BeanFactory
-无论使用何种形式,配置文件是相同的。
Main.java
//1.创建Spring的IOC容器对象//ApplicationContext代表容器,实际上是一个接口//ClassPathxmlApplicationContext是ApplicationContext接口的实现类,该实现类从类路径下来加载配置文件ApplicationContext ctx = new ClassPathxmlApplicationContext("applicationContext.xml");//2.从IOC容器中获取Bean实例//利用id定位到IOC容器的BeanHelloWorld helloworld =(HelloWorld) ctx.getBean("helloWorld");//利用类型返回IOC容器中的Bean,但要求IOC容器中必须只用有一个该类型的Bean//HelloWorld helloworld =(HelloWorld) ctx.getBean("HelloWorld.class");Car car = (Car) ctx.betBean("XXX");//3.调用HelloWorld类中的方法
0 0
- 如何配置Bean
- Spring如何配置bean属性
- Spring 如何在xml中配置Bean?
- Bean 配置
- 配置bean
- tomcat下如何配置jsp、servlet和bean的问题
- Tomcat安装及如何配置jsp、servlet、bean
- tomcat下如何配置jsp、servlet和bean
- Java中如何获取Spring中配置的bean
- 如何配置EJB3消息处理并发数[Message Driven Bean]
- 解析Java中如何获取Spring中配置的bean
- Java中如何获取Spring中配置的bean
- 如何把webservice的endpoint配置成一个spring bean
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例
- 继承bean配置,依赖bean配置
- Bean配置示例
- spring bean的配置
- 配置连接bean
- 测试一个config server 服务器挂机后,集群是否能读写数据
- C、C++、C#、Java、php、python语言的内在特性及区别
- xcode使用svn
- hdu5335 多校联合第四场1009 搜索
- Android自定义文字闪烁渐变色的跑马灯
- 如何配置Bean
- Navigation 简单自动寻路
- PropertyPlaceholderConfigurer的使用
- request.getScheme()的使用方法
- SOA平台架构解析
- iOS UItableView通讯录
- MySQL一些常用的时间函数 current_timestamp()
- win7+mongodb
- 进程切换