Spring之利用autowire属性进行自动装配
2017-05-03 22:16
295 查看
spring的自动属性装配,其实就是说,对于bean的属性,不用使用手工显示装配,可以让spring自己通过在xml文件中按照一定的规则查找相关的符合条件的bean,装配为bean的属性。
这样说起来有点绕口,直接上代码。
首先,写一个辅助类。
package com.cmm;
public class Phone {
private String brand;
private int price;
@Override
public String toString() {
return "Phone [brand=" + brand + ", price=" + price + "]";
}
}
(一)通过类型进行自动装配。
<?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-4.3.xsd">
<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000">
</bean>
<!-- 使用autowire属性进行自动装配 -->
<!--
autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
byType: 表示按照bean的类型进行装配,也就是按照属性的类型。
这里person只有一个属性,就是Phone phone.
-->
<bean id="person" class="com.cmm.Person" autowire="byType"></bean>
</beans>
但是,需要注意两点。
第一:如果同一个xml中有两个或两个以上的同一类型的bean,就会装配失败,抛出异常。
第二:如果没有找到匹配的bean,则该属性会被设置为null。
<?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-4.3.xsd">
<!-- 装配两个同一类型的bean -->
<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
<bean id="oppo" class="com.cmm.Phone" p:brand="oppo" p:price="2700"></bean>
<!-- 使用autowire属性进行自动装配 -->
<!--
autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
byType: 表示按照bean的类型进行装配,也就是按照属性的类型。
这里person只有一个属性,就是Phone phone.
-->
<bean id="person" class="com.cmm.Person" autowire="byType"></bean>
</beans>
添加一个属性
配置如下:
<?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-4.3.xsd">
<!-- 装配两个同一类型的bean -->
<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
<!-- 使用autowire属性进行自动装配 -->
<!--
autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
byType: 表示按照bean的类型进行装配,也就是按照属性的类型。
这里person只有一个属性,就是Phone phone.
-->
<bean id="person" class="com.cmm.Person" autowire="byType"></bean>
</beans>
结果如下:
name属性为:null。
(二)通过属性名字进行装配。
<?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-4.3.xsd">
<!-- id设置为Person的属性phone, -->
<bean id="phone" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
<!-- 使用autowire属性进行自动装配 -->
<!--
autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
byType: 表示按照bean的类型进行装配,也就是按照属性的类型。
这里person只有一个属性,就是Phone phone.
-->
<!-- Person 有setPhone的方法,使用byname方式进行自动装配 -->
<bean id="person" class="com.cmm.Person" p:name="Tom" autowire="byName"></bean>
</beans>
使用名字进行自动装配,实际上就是在xml中寻找与类属性同名的bean,如XXX,然后调用该类的setXXX方法来设置属性。
总结:自动装配,就是使用bean的autowire属性按照不同的方式,调用类的setter方法,设置bean属性。常用的:byType与byName。
这样说起来有点绕口,直接上代码。
首先,写一个辅助类。
package com.cmm;
public class Phone {
private String brand;
private int price;
@Override
public String toString() {
return "Phone [brand=" + brand + ", price=" + price + "]";
}
}
package com.cmm; public class Person { private Phone phone; @Override public String toString() { return "Person [phone=" + phone + "]"; } }
(一)通过类型进行自动装配。
<?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-4.3.xsd">
<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000">
</bean>
<!-- 使用autowire属性进行自动装配 -->
<!--
autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
byType: 表示按照bean的类型进行装配,也就是按照属性的类型。
这里person只有一个属性,就是Phone phone.
-->
<bean id="person" class="com.cmm.Person" autowire="byType"></bean>
</beans>
但是,需要注意两点。
第一:如果同一个xml中有两个或两个以上的同一类型的bean,就会装配失败,抛出异常。
第二:如果没有找到匹配的bean,则该属性会被设置为null。
<?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-4.3.xsd">
<!-- 装配两个同一类型的bean -->
<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
<bean id="oppo" class="com.cmm.Phone" p:brand="oppo" p:price="2700"></bean>
<!-- 使用autowire属性进行自动装配 -->
<!--
autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
byType: 表示按照bean的类型进行装配,也就是按照属性的类型。
这里person只有一个属性,就是Phone phone.
-->
<bean id="person" class="com.cmm.Person" autowire="byType"></bean>
</beans>
添加一个属性
package com.cmm; public class Person { private Phone phone; private String name; public void setPhone(Phone phone) { this.phone = phone; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [phone=" + phone + ", name=" + name + "]"; } }
配置如下:
<?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-4.3.xsd">
<!-- 装配两个同一类型的bean -->
<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
<!-- 使用autowire属性进行自动装配 -->
<!--
autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
byType: 表示按照bean的类型进行装配,也就是按照属性的类型。
这里person只有一个属性,就是Phone phone.
-->
<bean id="person" class="com.cmm.Person" autowire="byType"></bean>
</beans>
结果如下:
name属性为:null。
(二)通过属性名字进行装配。
<?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-4.3.xsd">
<!-- id设置为Person的属性phone, -->
<bean id="phone" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
<!-- 使用autowire属性进行自动装配 -->
<!--
autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
byType: 表示按照bean的类型进行装配,也就是按照属性的类型。
这里person只有一个属性,就是Phone phone.
-->
<!-- Person 有setPhone的方法,使用byname方式进行自动装配 -->
<bean id="person" class="com.cmm.Person" p:name="Tom" autowire="byName"></bean>
</beans>
使用名字进行自动装配,实际上就是在xml中寻找与类属性同名的bean,如XXX,然后调用该类的setXXX方法来设置属性。
总结:自动装配,就是使用bean的autowire属性按照不同的方式,调用类的setter方法,设置bean属性。常用的:byType与byName。
相关文章推荐
- Spring - bean的autowire属性(自动装配)
- Spring <bean>标签属性 Autowire自动装配
- Spring - bean的autowire属性(自动装配)
- Spring - bean的autowire属性(自动装配)
- Spring <bean>标签属性 Autowire自动装配
- Spring <bean>标签属性 Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- spring 自动装配 default-autowire="byName/byType"
- Spring Autowire自动装配
- Spring的自动装配 default-autowire="byName"
- spring的自动装配(default-autowire="byName")
- Spring2.5中使用注解装配属性and组件自动扫描
- spring 的自动装配 autowire
- spring的自动装配(default-autowire="byName")
- Spring依赖注入 — 属性自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配 ---残梦追月原创