Spring食行记之配置bean

【bean配置】

在XML文件中通过bean节点来配置bean

1 <!--
2        配置bean
3        class: bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求bean中必须有无参的构造器。
4        id:标识容器中的bean,id唯一。
5 -->
6    <bean id="helloWorld" class="com.hk.beans.HelloWorld">
7        <property name="name" value="Spring"></property>
8    </bean>

id:bean的名称。

--在IOC容器中必须是唯一的。

--若id没有指定,Spring自动将对应类名作为bean的名字。

--id可以指定多个名字,名字之间可用逗号,分号,或空格分隔。

【注】:

当bean中不存在无参构造器,存在有参构造器时,运行程序,会出现以下错误:

【Spring容器】

--ApplicationContext代表容器,但实际上它是一个接口。

--在Spring IOC容器读取bean配置创建bean实例之前,必须对它进行实例化。只有在容器实例化后,才可以从IOC容器中获取bean实例并使用。

--Spring提供了两种类型的IOC容器实现。

(1)BeanFactory:IOC容器的基本实现。

(2)ApplicationContext:提供了更多的高级特性。是BeanFactory的子接口。

BeanFactory是Spring框架的基础设施,面向Spring本身;

ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext而非底层的BeanFactory。

无论使用何种方式,配置文件是相同的。

【ApplicationContext】

1.ApplicationContext的主要实现类:

--ClassPathXmlApplicationContext:从类路径下加载配置文件。

--FileSystemXmlApplicationContext:从文件系统中加载配置文件。

2.ConfigurableApplicationContext(子接口)扩展于ApplicationContext,新增加两个主要方法:refresh()和close(),让ApplicationContext具有启动、刷新和关闭上下文的能力。

3.ApplicationContext在初始化上下文的时候就实例化所有单例的bean。

4.WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作。

【从IOC容器中获取bean】

1.调用ApplicationContext的getBean()方法。该方法是在ApplicationContext的父接口BeanFactory中定义实现的。

由图中可知,还可以由类型获取bean。

1 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
2 HelloWorld helloWorld = ctx.getBean(HelloWorld.class); 

缺点!!:如果在IOC容器中此类型不唯一(有两个或以上相同类型的bean时),会出错。

---小结:

1 //2. 从IOC容器中获取Bean实例
2 //方式一:利用id定位到IOC容器中的bean
3 //HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
4 //方式二:利用类型返回IOC容器中的bean,但要求IOC容器中必须只能有一个该类型的bean
5 HelloWorld helloWorld = ctx.getBean(HelloWorld.class); 

【依赖注入的方式】

Spring支持3种依赖注入的方式:

--属性输入

--构造器注入

--工厂方式(很少使用,不推荐)

1.属性注入

(1)概述:属性注入即通过setter方法注入Bean的属性值或以来的对象。

(2)属性注入使用<property>元素,使用name属性指定Bean的属性名称,value属性或<value>子节点指定属性值。

(3)属性注入是实际应用中最常用到的注入方式。

1    <bean id="helloWorld" class="com.hk.beans.HelloWorld">
2        <property name="name" value="Spring"></property>
3    </bean>

2.构造方法注入

(1)通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean实例在实例化后就可以使用。

(2)构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>中没有name属性。

Car.java:

 1 package com.hk.beans;
 2
 3 public class Car {
 4     private String brand;
 5     private String corp;
 6     private double price;
 7     private int maxSpeed;
 8
 9     public Car(String brand, String corp, double price) {
10         super();
11         this.brand = brand;
12         this.corp = corp;
13         this.price = price;
14     }
15
16     @Override
17     public String toString() {
18         return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
19                 + ", maxSpeed=" + maxSpeed + "]";
20     }
21
22     public Car(String brand, String corp, int maxSpeed) {
23         super();
24         this.brand = brand;
25         this.corp = corp;
26         this.maxSpeed = maxSpeed;
27     }
28 }

配置文件代码:

 1 <!-- 通过构造方法来配置bean的属性 -->
 2    <bean id="car" class="com.hk.beans.Car">
 3       <constructor-arg value="BWM" index="0"></constructor-arg>
 4       <constructor-arg value="ShangHai" index="1"></constructor-arg>
 5       <constructor-arg value="100000" type="double"></constructor-arg>
 6    </bean>
 7
 8    <!-- 使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器! -->
 9    <bean id="car2" class="com.hk.beans.Car">
10       <constructor-arg value="bieke" type="java.lang.String"></constructor-arg>
11       <constructor-arg value="BeiJing" type="java.lang.String"></constructor-arg>
12       <constructor-arg value="150" type="int"></constructor-arg>
13    </bean>

(测试)Main.java:

1         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
2
3         Car car = (Car) ctx.getBean("car");
4         System.out.println(car);
5
6         car = (Car) ctx.getBean("car2");
7         System.out.println(car);

运行结果;

原文地址:https://www.cnblogs.com/zhzcode/p/9580063.html

时间: 2024-08-09 10:03:10

Spring食行记之配置bean的相关文章

spring在xml文件中配置bean的三种方法

一.最常见,也是缺省,是调用spring的缺省工厂类 spring缺省工厂类:org.springframework.beans.factory.support.DefaultListableBeanFactory使用其静态方法preInstantiateSingletons() 配置文件中最普通最基本的定义一个普通bean<bean id="DvdTypeDAOBean" class="com.machome.dvd.impl.DvdTypeDAO" >

跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参的构造器 2.依赖注入的方式 1)属性注入:通过setter方法注入Bean的属性值或依赖的对象 属性注入使用<Property>元素,使用name指定Bean的属性名称,使用value指定Bean的属

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Respository:标识持久层组件 3.@Service:标识业务层组件 4.@Controller:标识表现层组件 Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称 当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件

spring笔记--通过注解(annotation)配置Bean

Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基本注解,标识了一个受spring管理的组件.   @Repository:标识持久层组件 @Service:标识服务层(业务层)组件 @Controller:标识表现层组件 对于扫描上的组件,Spring有默认的命名策略,使用非限定类名,第一个字母小写,也可以在注解中通过value属性值表示组件的名

Spring系列【11】配置Bean的初始化行为

对某个Bean添加lazy-init属性:lazy-init 设置只对scop属性为singleton的bean起作用 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins

[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

尚硅谷Spring整合Hibernate基于xml配置

描述:这是一个最简单网上书城demo. 下载地址:http://download.csdn.net/detail/u013488580/8370899 1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory 2). 让 Hibernate 使用上 Spring 的声明式事务 2. 整合步骤: 1). 加入 hibernate ①. jar 包 ②. 添加 hibernate 的配置文件: hibernate

Spring中基于Java的配置@Configuration和@Bean用法 (转)

spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: [java] view plain copy print? <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmln

spring 配置bean

Main(测试方法) public class Main { public static void main(String[] args) { //1.创建Spring 的IOC容器对象: //spring提供了两种IOC容器的实现 //1.BeanFactory:面向spring本身,,(就是底层的东西) //2.ApplicationContext: 面向使用spring框架的开发者,她提供了更多高级特性,是BeanFactory的子接口 // ----ConfigurableApplica