依赖注入方式:手动装配 和 自动装配
手动装配:一般进行配置信息都采用手动
基于xml装配:构造方法、setter方法
基于注解装配:
自动装配:struts和spring 整合可以自动装配
byType:按类型装配
byName:按名称装配
constructor构造装配,
auto: 不确定装配
1.构造方法
User.java
package com.jd.xml.constructor; /** * @author weihu * @date 2018/8/13/013 21:55 */ public class User { private Integer uid; private String username; private Integer age; public User(String username, Integer age) { this.username = username; this.age = age; } public User(Integer uid, String username) { this.uid = uid; this.username = username; } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "uid=" + uid + ", username='" + username + '\'' + ", age=" + age + '}'; } }
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 构造方法注入 * <constructor-arg> 用于配置构造方法一个参数argument name :参数的名称 value:设置普通数据 ref:引用数据,一般是另一个bean id值 index :参数的索引号,从0开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。 type :确定参数类型 例如:使用名称name <constructor-arg name="username" value="jack"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg> 例如2:类型type 和 索引 index <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg> <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg> --> <bean id="userId" class="com.jd.xml.constructor.User" > <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg> <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg> </bean> </beans>
TestConstruct.java
package com.jd.xml.constructor; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author weihu * @date 2018/8/13/013 21:59 */ public class TestConstruct { @Test public void testConstruct(){ String xmlPath="com/jd/xml/constructor/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); User user = applicationContext.getBean("userId", User.class); System.out.println(user); } }
2. setter方法
Pserson.java
package com.jd.xml.setter; /** * @author weihu * @date 2018/8/13/013 22:02 */ public class Pserson { private String pname; private Integer age; private Address homeAddr; //家庭地址 private Address companyAddr; //公司地址 public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Address getHomeAddr() { return homeAddr; } public void setHomeAddr(Address homeAddr) { this.homeAddr = homeAddr; } public Address getCompanyAddr() { return companyAddr; } public void setCompanyAddr(Address companyAddr) { this.companyAddr = companyAddr; } @Override public String toString() { return "Pserson{" + "pname='" + pname + '\'' + ", age=" + age + ", homeAddr=" + homeAddr + ", companyAddr=" + companyAddr + '}'; } }
Address.java
package com.jd.xml.setter; /** * @author weihu * @date 2018/8/13/013 22:03 */ public class Address { private String addr; private String tel; public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } }
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- setter方法注入 * 普通数据 <property name="" value="值"> 等效 <property name=""> <value>值 * 引用数据 <property name="" ref="另一个bean"> 等效 <property name=""> <ref bean="另一个bean"/> --> <bean id="personId" class="com.jd.xml.setter.Pserson"> <property name="pname" value="湖哥"></property> <property name="age"> <value>28</value> </property> <property name="homeAddr" ref="homeAddrId"></property> <property name="companyAddr"> <ref bean="companyAddrId"/> </property> </bean> <bean id="homeAddrId" class="com.jd.xml.setter.Address"> <property name="addr" value="湖南"></property> <property name="tel" value="110"></property> </bean> <bean id="companyAddrId" class="com.jd.xml.setter.Address"> <property name="addr" value="北京"></property> <property name="tel" value="120"></property> </bean> </beans>
TestSetter.java
package com.jd.xml.setter; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author weihu * @date 2018/8/13/013 22:03 */ public class TestSetter { @Test public void testSetter(){ String xmlPath="com/jd/xml/setter/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); Pserson pserson = applicationContext.getBean("personId", Pserson.class); System.out.println(pserson); } }
3. P命令空间[了解]
l 对“setter方法注入”进行简化,替换<property name="属性名">,而是在
<bean p:属性名="普通值" p:属性名-ref="引用值">
l p命名空间使用前提,必须添加命名空间
Person.java
package com.jd.xml.pnamespace; public class Person { private String pname; private Integer age; private Address homeAddr; //家庭地址 private Address companyAddr; //公司地址 public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Address getHomeAddr() { return homeAddr; } public void setHomeAddr(Address homeAddr) { this.homeAddr = homeAddr; } public Address getCompanyAddr() { return companyAddr; } public void setCompanyAddr(Address companyAddr) { this.companyAddr = companyAddr; } @Override public String toString() { return "Person [pname=" + pname + ", age=" + age + ", homeAddr=" + homeAddr + ", companyAddr=" + companyAddr + "]"; } }
Address.java
package com.jd.xml.pnamespace; public class Address { private String addr; //地址信息 private String tel; //电话 public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } @Override public String toString() { return "Address [addr=" + addr + ", tel=" + tel + "]"; } }
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personId" class="com.jd.xml.pnamespace.Person" p:pname="禹太璞" p:age="22" p:homeAddr-ref="homeAddrId" p:companyAddr-ref="companyAddrId"> </bean> <bean id="homeAddrId" class="com.jd.xml.pnamespace.Address" p:addr="DG" p:tel="东莞"> </bean> <bean id="companyAddrId" class="com.jd.xml.pnamespace.Address" p:addr="DG" p:tel="岛国"> </bean> </beans>
TestP.java
package com.jd.xml.pnamespace; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class TestP { @Test public void testPname(){ //从spring容器获得 String xmlPath = "com/jd/xml/pnamespace/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); Person person = (Person) applicationContext.getBean("personId"); System.out.println(person); } }
4. SpEL[了解]
l 对<property>进行统一编程,所有的内容都使用value
<property name="" value="#{表达式}">
#{123}、#{'jack'} : 数字、字符串
#{beanId} :另一个bean引用
#{beanId.propName} :操作数据
#{beanId.toString()} :执行方法
#{T(类).字段|方法} :静态方法或字段
Customer.java
package com.jd.xml.spel; public class Customer { private String cname = "jack"; private Double pi ;// = Math.PI; public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Double getPi() { return pi; } public void setPi(Double pi) { this.pi = pi; } @Override public String toString() { return "Customer [cname=" + cname + ", pi=" + pi + "]"; } }
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <property name="cname" value="#{'jack'}"></property> <property name="cname" value="#{customerId.cname.toUpperCase()}"></property> 通过另一个bean,获得属性,调用的方法 <property name="cname" value="#{customerId.cname?.toUpperCase()}"></property> ?. 如果对象不为null,将调用方法 --> <bean id="customerId" class="com.jd.xml.spel.Customer" > <property name="cname" value="#{customerId.cname?.toUpperCase()}"></property> <property name="pi" value="#{T(java.lang.Math).PI}"></property> </bean> </beans>
TestSpEL.java
package com.jd.xml.spel; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpEL { @Test public void testSpel() throws Exception{ //spring 工厂 String xmlPath = "com/jd/xml/spel/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); Customer customer = (Customer) applicationContext.getBean("customerId"); System.out.println(customer); } }
5. 集合注入
CollData.java
package com.jd.xml.coll; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollData { private String[] arrayData; private List<String> listData; private Set<String> setData; private Map<String, String> mapData; private Properties propsData; public String[] getArrayData() { return arrayData; } public void setArrayData(String[] arrayData) { this.arrayData = arrayData; } public List<String> getListData() { return listData; } public void setListData(List<String> listData) { this.listData = listData; } public Set<String> getSetData() { return setData; } public void setSetData(Set<String> setData) { this.setData = setData; } public Map<String, String> getMapData() { return mapData; } public void setMapData(Map<String, String> mapData) { this.mapData = mapData; } public Properties getPropsData() { return propsData; } public void setPropsData(Properties propsData) { this.propsData = propsData; } @Override public String toString() { return "CollData [\narrayData=" + Arrays.toString(arrayData) + ", \nlistData=" + listData + ", \nsetData=" + setData + ", \nmapData=" + mapData + ", \npropsData=" + propsData + "\n]"; } }
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 集合的注入都是给<property>添加子标签 数组:<array> List:<list> Set:<set> Map:<map> ,map存放k/v 键值对,使用<entry>描述 Properties:<props> <prop key=""></prop> 【】 普通数据:<value> 引用数据:<ref> --> <bean id="collDataId" class="com.jd.xml.coll.CollData" > <property name="arrayData"> <array> <value>DS</value> <value>DZD</value> <value>屌丝</value> <value>屌中屌</value> </array> </property> <property name="listData"> <list> <value>于嵩楠</value> <value>曾卫</value> <value>杨煜</value> <value>曾小贤</value> </list> </property> <property name="setData"> <set> <value>停封</value> <value>薄纸</value> <value>关系</value> </set> </property> <property name="mapData"> <map> <entry key="jack" value="杰克"></entry> <entry> <key><value>rose</value></key> <value>肉丝</value> </entry> </map> </property> <property name="propsData"> <props> <prop key="高富帅">嫐</prop> <prop key="白富美">嬲</prop> <prop key="男屌丝">挊</prop> </props> </property> </bean> </beans>
TestColl.java
package com.jd.xml.coll; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestColl { @Test public void testColl() throws Exception{ //spring 工厂 String xmlPath = "com/jd/xml/coll/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); CollData collData = (CollData) applicationContext.getBean("collDataId"); System.out.println(collData); } }