12、(知识篇)Spring使用xml配置bean01

来源:互联网 发布:雌雄嵌合体 知乎 编辑:程序博客网 时间:2024/06/05 18:13


//Spring IOC - Inversion of Control(控制反轉)
//DI Dependency Injection 依賴注入
//XML版表現形式


//1、property形式  :<property name="equirementNaame" value="盔甲"></property>
//2、constructor形式:<constructor-arg name="attack" value="10000"></constructor-arg>
//3、p形式(需要引入命名空間) <bean id="skill3" class="com.spring.beans.Skill" p:name="劍擊" p:attack="7000">
//4、特殊字符的處理(CDATA)<property name="sex" ><value><![CDATA[男]]></value></property>
//5、自動裝配 bean設置自動裝配autowire  <bean id="person" class="com.spring.beans.Person" autowire="byType"/>
//6、作用于Scope 默認singleton <bean id="person" class="com.spring.beans.Person" scope="singleton">
//7、讀取外部文件(如讀取數據源)
// 引入 <context:property-placeholder location="classpath:database.properties"/>
// 然後使用${xxx}(類似EL)  <property name="driverClass" value="${driverclass}"></property>
//8、SpringEL 使用#{} 可以運算/調用靜態方法 <property name="field1" value="#{'field1'}"></property>
//9、list、map、Properties的使用
/*<list>
<ref bean="skill"/>
<ref bean="skill2"/>
<ref bean="skill3"/>
<bean class="com.spring.beans.Skill">
<property name="name" value="天賦技能"></property>
<property name="attack" value="20000"></property>
</bean>
</list>

*/



//测试类

package com.spring.test;import javax.sql.DataSource;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.beans.Equirement;import com.spring.beans.Person;import com.spring.beans.Skill;import com.spring.beans.SpringEL;public class Test {public static void main(String[] args) {//Spring IOC - Inversion of Control(控制反轉)//DI Dependency Injection 依賴注入//XML版表現形式//1、property形式  :<property name="equirementNaame" value="盔甲"></property>//2、constructor形式:<constructor-arg name="attack" value="10000"></constructor-arg>//3、p形式(需要引入命名空間) <bean id="skill3" class="com.spring.beans.Skill" p:name="劍擊" p:attack="7000">//4、特殊字符的處理(CDATA)<property name="sex" ><value><![CDATA[男]]></value></property>//5、自動裝配 bean設置自動裝配autowire  <bean id="person" class="com.spring.beans.Person" autowire="byType"/>//6、作用于Scope 默認singleton <bean id="person" class="com.spring.beans.Person" scope="singleton">//7、讀取外部文件(如讀取數據源)// 引入 <context:property-placeholder location="classpath:database.properties"/>// 然後使用${xxx}(類似EL)  <property name="driverClass" value="${driverclass}"></property>//8、SpringEL 使用#{} 可以運算/調用靜態方法 <property name="field1" value="#{'field1'}"></property>//9、list、map、Properties的使用/*<list><ref bean="skill"/><ref bean="skill2"/><ref bean="skill3"/><bean class="com.spring.beans.Skill"><property name="name" value="天賦技能"></property><property name="attack" value="20000"></property></bean></list>*/ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");// 初始化裝備Equirement equirement = (Equirement) ctx.getBean("equirement");System.out.println(equirement);// 初始化技能1Skill skill = (Skill) ctx.getBean("skill");System.out.println(skill);// 初始化技能2Skill skill2 = (Skill) ctx.getBean("skill2");System.out.println(skill2);//人物Person person = ctx.getBean(Person.class);System.out.println(person);DataSource ds = (DataSource) ctx.getBean("db");System.out.println(ds);SpringEL  sprintel = (SpringEL) ctx.getBean("el");System.out.println(sprintel);}}


bean部分:

Person类package com.spring.beans;import java.util.List;import java.util.Map;public class Person {private String name;private String sex;private Equirement equirement;private List<Skill> skills;private Map<String,Integer> items;public Person() {super();System.out.println("人物正在初始化...");// TODO Auto-generated constructor stub}public Person(String name, String sex, Equirement equirement, List<Skill> skills, Map<String, Integer> items) {super();this.name = name;this.sex = sex;this.equirement = equirement;this.skills = skills;this.items = items;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Equirement getEquirement() {return equirement;}public void setEquirement(Equirement equirement) {this.equirement = equirement;}public List<Skill> getSkills() {return skills;}public void setSkills(List<Skill> skills) {this.skills = skills;}public Map<String, Integer> getItems() {return items;}public void setItems(Map<String, Integer> items) {this.items = items;}@Overridepublic String toString() {return "Person [name=" + name + ", sex=" + sex + ", equirement=" + equirement + ", skills=" + skills+ ", items=" + items + "]";}}//Equirement装备类package com.spring.beans;public class Equirement {private String equirementNaame;private int defense;public Equirement() {super();System.out.println("裝備正在初始化...");// TODO Auto-generated constructor stub}public Equirement(String equirementNaame, int defense) {super();this.equirementNaame = equirementNaame;this.defense = defense;}public String getEquirementNaame() {return equirementNaame;}public void setEquirementNaame(String equirementNaame) {this.equirementNaame = equirementNaame;}public int getDefense() {return defense;}public void setDefense(int defense) {this.defense = defense;}@Overridepublic String toString() {return "Equirement [equirementNaame=" + equirementNaame + ", defense=" + defense + "]";}}//技能类package com.spring.beans;public class Skill {private String name;private int attack;public Skill() {super();System.out.println("技能正在初始化...");// TODO Auto-generated constructor stub}public Skill(String name, int attack) {super();System.out.println("技能正在初始化帶參數...");this.name = name;this.attack = attack;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAttack() {return attack;}public void setAttack(int attack) {this.attack = attack;}@Overridepublic String toString() {return "Skill [name=" + name + ", attack=" + attack + "]";}}SpringEL类package com.spring.beans;public class SpringEL {private String field1;private int field2;private String field3;private String field4;private boolean field5;public SpringEL() {super();// TODO Auto-generated constructor stub}public SpringEL(String field1, int field2, String field3, String field4, boolean field5) {super();this.field1 = field1;this.field2 = field2;this.field3 = field3;this.field4 = field4;this.field5 = field5;}public String getField1() {return field1;}public void setField1(String field1) {this.field1 = field1;}public int getField2() {return field2;}public void setField2(int field2) {this.field2 = field2;}public String getField3() {return field3;}public void setField3(String field3) {this.field3 = field3;}public String getField4() {return field4;}public void setField4(String field4) {this.field4 = field4;}public boolean isField5() {return field5;}public void setField5(boolean field5) {this.field5 = field5;}@Overridepublic String toString() {return "SpringEL [field1=" + field1 + ", field2=" + field2 + ", field3=" + field3 + ", field4=" + field4+ ", field5=" + field5 + "]";}}


applicationContext.xml文件

<?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"xmlns:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"><!-- 裝備類 --><bean id="equirement" class="com.spring.beans.Equirement"><property name="equirementNaame" value="盔甲"></property><property name="defense" value="5000"></property></bean><!-- 技能 1--><bean id="skill" class="com.spring.beans.Skill"><constructor-arg name="attack" value="10000"></constructor-arg><constructor-arg name="name" value="一拳"></constructor-arg></bean><!-- 技能 2--><bean id="skill2" class="com.spring.beans.Skill"><property name="name" value="普通攻擊"></property><property name="attack" value="5000"></property></bean><!-- 技能3 --><bean id="skill3" class="com.spring.beans.Skill" p:name="劍擊" p:attack="7000"></bean><!-- 物品 --><util:map id="items"><entry key="山草頭藥" value="500"></entry><entry key="回氣丸" value="300"></entry></util:map><!-- 角色 --><bean id="person" class="com.spring.beans.Person" autowire="byType" scope="prototype"><property name="name" value="超人"></property><property name="sex" ><value><![CDATA[男]]></value></property><!-- <property name="equirement" ref="equirement"></property> --><property name="skills"><list><ref bean="skill"/><ref bean="skill2"/><ref bean="skill3"/><bean class="com.spring.beans.Skill"><property name="name" value="天賦技能"></property><property name="attack" value="20000"></property></bean></list></property><property name="items" ref="items"></property></bean><!-- 引用外部文件 --><context:property-placeholder location="classpath:database.properties"/><bean id="db" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${driverclass}"></property><property name="jdbcUrl" value="${jdbcurl}"></property><property name="user" value="${username}"></property><property name="password" value="${password}"></property></bean><!-- SpringEL --><bean id="el" class="com.spring.beans.SpringEL" ><property name="field1" value="#{'field1'}"></property><property name="field2" value="#{1 + 10}"></property><property name="field3" value="#{person.name}"></property><property name="field4" value="#{T(java.lang.Math).PI}"></property><property name="field5" value="#{1>0?true:false}"></property></bean></beans>


0 0
原创粉丝点击