一、Java集合框架
1、找不到Java包下的xml文件的解決辦法
這種錯(cuò)誤是由于沒(méi)有把xml文件放到CLASSPATH目錄下導(dǎo)致的,一種解決辦法是將xml文件放到resources目錄下,另外一種解決辦法是在pom.xml文件build標(biāo)簽中和plugins標(biāo)簽平級(jí)添加如下代碼:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
2、接口里面的變量必須是公開(kāi)、靜態(tài)的常量(public static final)
3、權(quán)限修飾符、static、final這三個(gè)修飾符的位置可以任意交換
4、集合框架


各種集合框架的關(guān)系如上圖所示,List有序可重復(fù),Set無(wú)序不重復(fù),ArrayList的父類是AbstractList,關(guān)于三種集合添加元素返回值:
- List調(diào)用add()永遠(yuǎn)返回的是true
- Set調(diào)用add()如果是第一次添加返回的是true,否則返回false
- map的鍵值都可以為null,調(diào)用put方法如果是覆蓋的話返回上一個(gè)值,如果是新添加的話返回null。所以返回null有兩種情況,一種是之前沒(méi)有這個(gè)key,另外一種是這個(gè)key對(duì)應(yīng)的value是null。
List與Set都實(shí)現(xiàn)了Collection接口,在Collection接口中對(duì)add()方法的返回值是這樣定義的:如果集合改變了就返回true,如果集合沒(méi)有改變就返回false,Set集合添加元素的時(shí)候會(huì)進(jìn)行判斷,如果存在直接丟棄,集合沒(méi)有改變,所以返回的是true。對(duì)于HashSet來(lái)說(shuō),會(huì)先創(chuàng)建一個(gè)Hash表,如果存放的是Integer類型,hashCode()方法直接返回這個(gè)值作為下標(biāo),所以這種情況下,HashSet仍然是有序的,不能用HashSet存放隨機(jī)的不重復(fù)的Integet類型的值,因?yàn)橛肋h(yuǎn)都是有序的。
二、“集合”注入
Spring對(duì)于數(shù)組、List、Set、Map與Properties五種數(shù)據(jù)類型的注入比較特別,需要特別說(shuō)明。其中Properties類之前用的比較少,是專門(mén)用來(lái)讀取配置文件的,繼承自java.util.Hashtable,從配置文件中讀取配置信息形成鍵值對(duì)存放,有點(diǎn)類似Map集合。下面這段代碼定義了一個(gè)叫做MyCollection的類,有五個(gè)成員,這五個(gè)成員的而數(shù)據(jù)類型分別是數(shù)組、List、Set、Map與Properties,為每個(gè)成員都提供了setter方法。
package com.qianfeng.bean;
import java.util.*;
public class MyCollection {
private String[] arrays;
private List<Object> list;
private Set<String> set;
private Map<String,String> map;
private Properties prop;
public String[] getArrays() {
return arrays;
}
public void setArrays(String[] arrays) {
this.arrays = arrays;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("MyCollection{");
sb.append("arrays=").append(Arrays.toString(arrays));
sb.append(", list=").append(list);
sb.append(", set=").append(set);
sb.append(", map=").append(map);
sb.append(", prop=").append(prop);
sb.append('}');
return sb.toString();
}
}
<?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="s1" class="com.qianfeng.bean.Birthday">
<property name="year" value="2000"></property>
<property name="day" value="22"></property>
<property name="month" value="1"></property>
</bean>
<bean id="p1" class="com.qianfeng.bean.Student">
<property name="name" value="zhangjiaxiang"/>
<property name="birthday" ref="s1"/>
</bean>
<bean id="p2" class="com.qianfeng.bean.Student" p:birthday-ref="s1" p:name="aaaa" />
<bean id="mycoll" class="com.qianfeng.bean.MyCollection">
<property name="arrays">
<array>
<value>java</value>
<value>html5</value>
<value>java</value>
</array>
</property>
<property name="list">
<list>
<value>jack</value>
<ref bean="p1"/>
</list>
</property>
<property name="set">
<set>
<value>hobert</value>
<value>null</value>
</set>
</property>
<property name="map">
<map>
<entry key="mike" value="male"/>
<entry key="null" value="null"/>
</map>
</property>
<property name="prop">
<props>
<prop key="url">jdbc:mysql://localhost:3306/hello</prop>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
使用property標(biāo)簽對(duì)對(duì)象成員進(jìn)行賦值都是調(diào)用這個(gè)類的setter,所以我們?cè)贘ava類中提供了setter以免報(bào)錯(cuò)。constructor-arg標(biāo)簽的賦值方式是使用構(gòu)造方法。八個(gè)基本數(shù)據(jù)類型加上String都是使用value進(jìn)行賦值的,其他類型的引用數(shù)據(jù)類型都是使用ref方式進(jìn)行賦值。
三、Spring AOP續(xù)
1、第四種方式
第四種方式也是基于xml文件,和第三種方式有點(diǎn)類似,不過(guò)更加靈活,可以根據(jù)方法返回值、包、類、方法與方法參數(shù)進(jìn)行匹配,讓符合條件的類或者方法進(jìn)行動(dòng)態(tài)代理,可以指定代理方式:采用Java原生的動(dòng)態(tài)代理還是cglib的動(dòng)態(tài)代理。需要注意的是,我們首先要添加一個(gè)依賴,否則會(huì)報(bào)異常:
Caused by: java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>com.springsource.org.aspectj.weaver</artifactId>
<version>1.6.8.RELEASE</version>
</dependency>
除了xml文件稍有區(qū)別,其余和第三種使用方式一樣:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="st" class="com.qianfeng.aop04.Student" />
<bean id="my" class="com.qianfeng.aop04.MyAspect" />
<aop:config proxy-target-class="true">
<aop:pointcut id="pt" expression="execution(* com.qianfeng.aop04.*.*(..))"/>
<aop:advisor advice-ref="my" pointcut-ref="pt"/>
</aop:config>
</beans>
第四行、第七行與第八行是新添加進(jìn)來(lái)的。關(guān)于aop:config這個(gè)標(biāo)簽:proxy-target-class="true"強(qiáng)制使用cglib的方式進(jìn)行動(dòng)態(tài)代理,如果是false就使用Java原生的動(dòng)態(tài)代理,aop:pointcut這個(gè)標(biāo)簽定義了一個(gè)切點(diǎn),可以理解為需要代理的方法,expression進(jìn)行過(guò)濾,第一個(gè)*匹配返回值,第二個(gè)*匹配所有類,第三個(gè)*匹配所有方法,第四個(gè)..匹配所有參數(shù),過(guò)濾條件,只代理滿足條件的類的滿足條件的方法。當(dāng)然*只是用來(lái)占位,填其他的值也可以。
2、第五種方式
第五種方式比第四種方式更加簡(jiǎn)單,代理類不需要實(shí)現(xiàn)接口,還可以更加方便的指定在目標(biāo)方法前還是后調(diào)用特定的方法。
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="st" class="com.qianfeng.aop05.Student" />
<bean id="my" class="com.qianfeng.aop05.MyAspect" />
<aop:config proxy-target-class="true">
<aop:aspect ref="my">
<aop:pointcut id="pt" expression="execution(* com.qianfeng.aop05.*.*(..))"/>
<aop:before method="before" pointcut-ref="pt"/>
<aop:after method="after" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>
</beans>
aspect是切面,pointcut是切點(diǎn),也就是需要代理的方法,許多需要代理的方法切點(diǎn)形成了一個(gè)切面。