Spring复习笔记

面向抽象的编程,好处就是灵活。


Properties可以通过流来加载属性集,具体用法如下:
Properties props = new Properties();
try{

    props.load(User.class.getResourceAsStream("a.properties"));
    System.out.println(props.getString("username"));
}catch(IOException ex){

    ex.printStackTrace();
}

IoC的好处:将自己new东西,改为由容器提供。
xsd文件指定了xml文件中能够写什么,不能够写什么。

Spring是一个IoC容器,它能够实例化具体的bean,能够动态装配。

注入类型:1.setter 2.构造方法
setter注入:
<bean id="" class="">
    <property name="" ref=""/>
</bean>
constructer注入:
<constructor-arg>
    <ref bean=""/>
</constructor-arg>

ref是参考已经初始化的对象,而<bean class=""/>则是new一个新的

<constructor-arg type="" index="" value=""/>

bean里面配置,id和name一样。name里面可以含特殊字符。

简单属性注入:
<bean id="" class="">
    <property name="" value=""/>
</bean>

bean的生命周期:
<bean id="" class="" scope=""></bean>
singleton,prototype
request,session,gloable session=====>与WEB相关
默认的都是singleton,不论拿多少次,都是拿的同一个bean
prototype每次拿都是个新的
如果是和struts2一起用,那么scope推荐配为prototype

集合注入:
把一系列的组装为properties对象,然后注入进入adminEmails
map注入:
list注入:

<bean id="" class="" scope="" autowire="">
</bean>


autowire自动装配,byName根据名字自动匹配,byType根据类型自动匹配。
<beans autowire="byName">
    <bean id="" class="" autowire="default"/>
</beans>
即这个bean的自动装配默认与beans中指定的一致。

lazy-init是指这个bean在容器初始化时,并不进行初始化,在需要时,才进行初始化。
<bean id="" class="" lazy-init="true"/>

<bean id="" class="" init-method="init" destory-method="destory"/>
即在初始化时调用init,在destory时调用destory方法
如果scope是singleton时,由于两次取bean都是同一个引用,故init和destory方法就只调用一次
而scope是prototype时,会调用两次。

数据库连接池应该写上
<bean id="dataSource" class="" destory-method="close"/>
因为用完之后,应该记得关闭。

Annotation也可以起到注入的功能,使用@Autowired(byType)或者@Required或者@Resource(byName,最好@Resource(name=""))

AOP的核心,动态代理技术:
动态代理与InvocationHandler和Proxy有关,假设有一个Function接口:
package com.SpringLearn.AOP;
public interface Function {

public void print();
}
然后用一个FunctionImp实现此接口:
package com.SpringLearn.AOP;
public class FunctionImp implements Function{

public void print() {
System.out.println("execute FunctionImp");
}
}
再然后编写一个ProcessHander类实现InvocationHandler接口,并添加切面代码:
package com.SpringLearn.AOP;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class ProcessHandler implements InvocationHandler{

private Object myObject = null;
public ProcessHandler(Object obj){
this.myObject = obj;
}
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
System.out.println("start execute");
//arg1.invoke(arg0, arg2);
arg1.invoke(this.myObject,arg2);
System.out.println("end execute");
return null;
}
}
最后一个测试类:
package com.SpringLearn.AOP;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class TestAOP {

public static void main(String[] args){
Function func = new FunctionImp();
InvocationHandler handler = new ProcessHandler(func);
Function proxy = (Function) Proxy.newProxyInstance(func.getClass().getClassLoader(),func.getClass().getInterfaces(),handler);
proxy.print();
}
}

上图为AOP切面编程的配置示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值