Spring
一.Spring是一個輕量級的Ioc( DI )和AOP的簡化企業(yè)開發(fā)的容器框架。
二.set注入方式
1.屬性注入:a. public class Message {private String title;private String content; getter/setter…}
b.public interface Printer { public void startup(); public void print();}
c. public class PrinterImpl implements Printer { private Message message;
public void setMessage(Message message){ …} //通過ser方法設置值
public void print() {System.out.println(message.getTitle()); }…….//需要值的存在
d.屬性注入:<bean id="message" class="assemble.Message"> //將控制交給容器注入
<property name="title" value="china title"></property>
<property name="content" value="control content"></property> </bean>

e.通過底層的BeanFactory創(chuàng)建和分發(fā):
XmlBeanFactory factory =new XmlBeanFactory(new ClassPathResource(“mypck/xxx.xml”));
ClassName name=( ClassName) factory.getBean("className "); //get此時延時加載,功能有限
2.引用其他類:<bean id="printer" class="assemble.PrinterImpl"
init-method="startup" singleton="true" > //容器參與了生命周期管理
<property name="message" ref="message"></property> </bean>
singleton="true":默認對象可以復用,false是不會加載的,因為創(chuàng)建多個實例就不需要了
初始化的順序:構(gòu)造函數(shù)(沒有就默認-àsetter-àinit-method
3.內(nèi)部類:<bean id="printer2" class="assemble.PrinterImpl" init-method="startup">
<property name="message"> <bean class="assemble.Message">
<property name="title" value="china title2"></property> </bean>
</property> </bean>
4.裝配集合:a.public class Foo { private List list; private String[ ] array;
private Set set; private Map map; private Properties props; setter/getter }
b. <property name="list">//value值的類型spring會自動區(qū)分
<list> <value>spring</value> <ref bean="message"/> </list> </property>
<property name="array"> <list> <value>ejb</value> </list> </property>
5.裝配set: <property name="set"> //set集合值是沒有重復的
<set> <value>jave ee</value> <value>.net</value> </set> </property>
6.裝配map: <property name="map">
<map> <entry key="orm"><value>hibernate</value> </entry> </map> </property>
7.裝配properties: <property name="props"> //props的key和value都是string所以可以簡寫
<props> <prop key="mvc">struts</prop> </props> </property>
8.裝配null:<property name=”onename”> <null/> </property>
三.構(gòu)造函數(shù)的注入方式:a. public class Address { private String street; }
b.public class Person { private String name;
private Address address; private Integer age; public Person() { }
public Person(String name, Address address) {
this.name = name; this.address = address; }
c. <bean id="person" class="assemble.Person">
<constructor-arg index="0" value="liming"></constructor-arg>
<constructor-arg index="1">
<bean class="assemble.Address">
<property name="street" value="hefei"></property> </bean>
</constructor-arg>
<property name="age" value="22"></property> </bean> // 可混合使用
d:通過ApplicationContext進行查找 //預先注入方式
ApplicationContext ctx=new ClassPathXmlApplicationContext(“mypack/ClassName”);
ClassName name=( ClassName) ctx.getBean("className ");
四.自動裝配方式:主要是byName和byType, 其他如constructor較少
1.a.如果存在: <bean id="message" class="assemble.Message">
<property name="content" value="control content"></property> </bean>
b.byName: <bean id="printer" class="assemble.PrinterImpl" >
<property name="message" ref="message"></property> </bean>
因為需要注入的屬性名和存在的要引用bean的名稱相同,所以可以用byName自動查找
<bean id="printer" class="assemble.PrinterImpl" autowire="byName"/>
2.byType:在容器中尋找一個與需要自動裝配的屬性類型相同的bean,如果找到超過一個相符的bean就會拋出異常.
注:如果配置文件中所有的bean的自動裝配方式相同,可以在<beans>根節(jié)點聲明以減少代碼,如果在自動裝配方式中再通過set方式注入,則set方式會覆蓋autowire.
五:spring持久化支持:為整合如jdbc會拋出SQLException,Hibernate拋出HibernateException
1.spring的DateAccessException:a.與特定持久化技術(shù)和實現(xiàn)無關
b.應用與持久層框架解藕 c.是RuntimeException,不需要強制捕獲