三Spring中Bean的基础配置1
来源:互联网 发布:html转xml python 编辑:程序博客网 时间:2024/06/07 21:02
配置bean的方式通常有2种:
1.通过XML方式进行配置
2.通过注解的方式进行配置
还是举上边的那个例子:
<bean id="cat1" class="beans.Cat"> <property name="name" value="狗子"></property> </bean>class:bean的全类名,通过反射的方式在IOC容器中创建Bean.
id:表示容器中的bean,id值唯一
Spring 提供了两种类型的 IOC 容器实现.
BeanFactory: IOC 容器的基本实现.
ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口.
BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的开发者,
几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
无论使用何种方式, 配置文件是相同的.
ApplicationContext是一个接口,有两个主要实现类:
ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力。
ApplicationContext 在初始化上下文时就实例化所有单例的 Bean。
WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作。
通过WebApplicationContext 的getBean()方法可以从 IOC 容器中获取 到Bean,这里主要有2中重载的方法:
1.getBean("id号")
2.getBean("类名")-要求IOC容器中只能有一个这个Bean
-属性注入(Bean总必须有无参数的构造器)
属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值
属性注入是实际应用中最常用的注入方式
-构造器注入
构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性
public class Dog { private String name; private int age; private double weigth; private int bitenums; public Dog(String name, int age, double weigth) { this.name = name; this.age = age; this.weigth = weigth; } public Dog(String name, int age, int bitenums) { this.name = name; this.age = age; this.bitenums = bitenums; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + ", weigth=" + weigth + ", bitenums=" + bitenums + '}'; }}有俩个构造方法,我们打算分别使用这俩个构造方法来给属性赋值:
下面是Bean配置:
<bean id="dog1" class="beans.Dog"> <constructor-arg value="dogge" index="0"></constructor-arg> <constructor-arg value="20" index="1"></constructor-arg> <constructor-arg value="200" index="2"></constructor-arg> </bean> <!--这里打算使用第二个构造方法--> <bean id="dog2" class="beans.Dog"> <constructor-arg value="ZZD" ></constructor-arg> <constructor-arg value="20" ></constructor-arg> <constructor-arg value="10" ></constructor-arg> </bean>主程序中进行调用:
Dog dog1= (Dog) ctx.getBean("dog1"); System.out.println(dog1.toString()); Dog dog2= (Dog) ctx.getBean("dog2"); System.out.println(dog2.toString());
输出结果:
可以看到第二个构造函数其实并没有起到作用,都匹配到第一个构造函数上去了,
正确的配置方式:
<bean id="dog1" class="beans.Dog"> <constructor-arg value="dogge" type="java.lang.String"></constructor-arg> <constructor-arg value="20" type="int"></constructor-arg> <constructor-arg value="200" type="double"></constructor-arg> </bean> <!--这里打算使用第二个构造方法--> <bean id="dog2" class="beans.Dog"> <constructor-arg value="ZZD" type="java.lang.String" ></constructor-arg> <constructor-arg value="20" type="int"></constructor-arg> <constructor-arg value="10" type="int"></constructor-arg> </bean>
-工厂方法注入(很少使用,不推荐)
- 三Spring中Bean的基础配置1
- 三Spring中的Bean配置基础2
- J2EE之Spring中Bean的基础配置
- Spring中Bean的配置
- spring中bean的配置
- Spring中bean的配置
- Spring中bean的配置
- Spring中Bean的配置
- Spring中Bean的配置
- spring中bean的配置
- Spring中Bean的配置
- (三)spring配置bean
- Spring-bean基础配置
- Spring - Bean的概念及其基础配置
- Spring Bean 的配置(1)
- Spring中bean工厂创建bean的三种方式
- Spring中Bean的生命周期配置bean的后置处理器
- Spring中bean的基本xml配置
- linux系统安装java环境
- cmake工具使用简明教程(基于命令行和gui,编译到windows和linux双平台)
- Android中如何搭建一个WebServer
- Linux学习笔记 --- 更换yum源
- [BZOJ2338][HNOI2011]数矩形
- 三Spring中Bean的基础配置1
- 无线网中一些约定俗成的时延估量
- “约会”的总结
- 记人生第一天实习的前一天
- numpy 基础 —— np.linalg
- tomcat 内存溢出
- 百度地图的使用(2)
- mybatis中怎么使用like形成动态模糊查询,错误使用CONCAT拼接sql的看过来
- [LintCode]Swap Two Nodes in Linked List