Spring_Bean配置(2)
自动装配
SpringIOC容器可以自动装配Bean,只需要在<bean>的autowire属性中指定自动装配的模式
byType:(根据类型自动装配)若IOC容器中存在多个与目标Bean一致的Bean,该种情况下Spring将无法判断。
byName:(根据名称自动装配)必须将目标Bean的名称和属性名设置的完全相同
constructor:(通过构造器自动装配)当Bean中存在多个构造器时,会很复杂,不推荐。
原来写法:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="autowire.Person" p:name="Jack" p:address-ref="address" p:car-ref="car"/> <bean id="address" class="autowire.Address" p:city="北京" p:street="长安街"/> <bean id="car" class="autowire.Car" p:brand="大众" p:price="300000"/> </beans>
自动装配:byName、byType
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 可以使用autowire属性指定自动装配的方式, byName根据当前Bean的名字和Bean的setter风格的属性名自动装配,若匹配则装配,否则报错 byType根据当前Bean的类型和属性的类型进行自动装配 --> <bean id="person" class="autowire.Person" p:name="Jack" autowire="byName" /> <bean id="address" class="autowire.Address" p:city="北京" p:street="长安街"/> <bean id="car" class="autowire.Car" p:brand="大众" p:price="300000"/> </beans>
自动装配缺点:
1、自动装配会将Bean的所有属性都装配,当希望装配个别时,则显得不够灵活
2、自动装配的两种方式只能二选一,不能兼而有之
3、实际开发中很少使用自动装配
配置Bean之间的关系
一、继承
<!-- 繁琐:需要重复写 --> <!--<bean id="address" class="autowire.Address" p:city="北京" p:street="长安街"/> <bean id="address2" class="autowire.Address" p:city="广州" p:street="市桥"/>--> <!-- 类似Java中的继承—— 配置的继承:使用bean的parent属性指定继承哪一个 父bean可以作为模板,也可以作为实例 --> <!-- abstract="true":表示该bean只能用于模板被继承,不能被SpringIOC容器实例化 若bean的class属性没有被指定,则必须为抽象的bean --> <bean id="address" class="autowire.Address" p:city="北京" p:street="长安街" abstract="true"/> <bean id="address2" parent="address" p:city="广州" p:street="市桥"/>
二、依赖
<!-- 要求在配置person的时候必须有一个关联的car 也就是在person这个Bean依赖于Car这个Bean --> <bean id="person" class="autowire.Person" p:name="CC" p:address-ref="address2" depends-on="car"/> <bean id="car" class="autowire.Car" p:brand="奥迪" p:price="90000"/>
Bean的作用域
sigleton作用域:默认,整个容器的生命周期内只创建一个bean
<!-- 使用bean的scope属性来配置bean的作用域 scope="singleton":默认值,容器初始化时,创建bean的实例,在整个容器的生命周期内只创建这一个bean,单例 --> <bean id="car" class="autowire.Car" scope="singleton" p:price="303030" p:brand="qq"/>
prototype:原型的,容器初始化不创建,每次请求的时候创建
<!-- 使用bean的scope属性来配置bean的作用域 scope="singleton":默认值,容器初始化时,创建bean的实例,在整个容器的生命周期内只创建这一个bean,单例 scope="prototype":原型的,容器初始化时,不创建bean的实例,而在每次请求的时候创建一个新的bean的实例,并返回 --> <bean id="car" class="autowire.Car" scope="prototype" p:price="303030" p:brand="qq"/>
引入外部配置文件
在配置文件中配置Bean的时候,有时需要在Bean的配置中混入系统部署的细节信息。(如:文件路径、数据源),而这些东西实际需要和Bean的配置相分离。
Spring提供了一个PropertyPlaceholderConfigurer的BeanFacotory后置处理器,这个处理器允许用户将Bean配置的部分内容外移到属性文件中,可以在Bean配置文件中使用 ${var} 的形式引用进来
以前的写法:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:driverClass="com.mysql.jdbc.Driver" p:password="123456" p:jdbcUrl="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=true" p:user="root"/>
引入外部文件:
<!--导入配置文件--> <context:property-placeholder location="db.properties"/> <!--使用外部属性文件--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:driverClass="${driver}" p:password="${password}" p:jdbcUrl="${url}" p:user="${user}"/>
db.properties:
user=root password=123456 url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=true driver=com.mysql.jdbc.Driver