spring开发
一、spring介绍
spring是一个开源项目,主要目的是为了解决企业应用开发复杂而设计的,主要优势是其分层架构,同时为J2EE开发应用提供集成框架。
二、spring的xml文件配置
1、spring的声明信息
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

2、bean里面的配置
这里scope默认的是Singleton(默认情况下spring提供的bean是共享的这里将scope设为prototype就变成非共享模式了),id里面是一个别名,class里面是实体类的具体地址
constructor-arg :通过构造函数的方式注入值(按构造函数的先后顺序的),下面还有map、set的数据注入

3、自动装配+扫描:简化spring里bean对象的开发,他会自动到你给的路径里面去找属性字段名与自己的容器里面一样的,如果有就就进行自动匹配。

在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean 注意:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者

三、延迟加载

在程序一开始运行的时候spring会把所有配置过的类都加载一边,而通过配置@Lazy(延迟加载就可以避免这样的情况)
也可以在bean里面进行配置

在声明上面配置

建议使用注解的方式配置最好
四、ioc容器的理解
控制反转
将控制对象的主动权交给spring
五、Di注入
通过在创建bean对象的时候将数据注入进去,所谓的依赖注入就是对象里面的数据有可能是通过其它的bean对象得到的
六、动态代理
1、代理模式一般分为三个部分
抽象主题角色:真实主题和代理主题的共同接口。
真实主题角色:定义了代理角色所代表的真实对象。
代理主题角色:含有对真实主题角色的引用。代理角色通常在将客户端调用传递给真实主题对象之前或者之后执行某些操作。
例:
以收快递的案例:
收快递并签字,这个是抽象主题。
买家,货物的主人,这个是真实主题。
门卫代理收货并签字,这个是代理主题。
代理模式的类图:
实现步骤:
1).定义一个抽象的主题。
package com.jinglin.staticproxy;
/**
* 定义一个抽象的主题,这个主题就规定了方法
* @author sony
*
*/
public interface IGetSomething {
void getsomething();
}
2)实现的是真实主题的类,这个类继承自抽象主题:
public class RealGetSomething implements IGetSomething {
@Override
public void getsomething() {
// TODO Auto-generated method stub
System.out.println("主人亲自收货并签字了");
}
3)实现的是代理主题的类,这个类继承自抽象主题,但是这个类关联了所代理的主题对象。
package com.jinglin.staticproxy;
/**
* 这是一个代理主题,也需要继承自抽象主题
* @author sony
*
*/
public class ProxyGetSomething implements IGetSomething {
private RealGetSomething realGetSomething;//依赖关系
public RealGetSomething getRealGetSomething() {
return realGetSomething;
}
public void setRealGetSomething(RealGetSomething realGetSomething) {
this.realGetSomething = realGetSomething;
}
public ProxyGetSomething(RealGetSomething _realGetSomething){
this.realGetSomething=_realGetSomething;
}
@Override
public void getsomething() {
// TODO Auto-generated method stub
getbefore();
realGetSomething.getsomething();
getafter();
}
private void getbefore(){
System.out.println("检查快递是否是本小区的");
}
private void getafter(){
System.out.println("快递签收完毕");
}
}
4)编写客户端调用的类(对外部开放的):
package com.jinglin.staticproxy;
public class ClientDemo {
private IGetSomething igetsomething;
public IGetSomething getIgetsomething() {
return igetsomething;
}
public void setIgetsomething(IGetSomething igetsomething) {
this.igetsomething = igetsomething;
}
public ClientDemo(IGetSomething _igetsomething){
this.igetsomething=_igetsomething;
}
public void show(){
igetsomething.getsomething();
}
}
5)最后编写测试类:
package com.jinglin.test;
import org.junit.Test;
import com.jinglin.staticproxy.ClientDemo;
import com.jinglin.staticproxy.IGetSomething;
import com.jinglin.staticproxy.ProxyGetSomething;
import com.jinglin.staticproxy.RealGetSomething;
public class ProxyStaticTest {
@Test
public void testit(){
RealGetSomething realGetSomething = new RealGetSomething();
IGetSomething igetsomething = new ProxyGetSomething(realGetSomething);
ClientDemo client = new ClientDemo(igetsomething);
client.show();
}
}
上面的例子,实现一个代理首先要定义一个接口然后还要实现这个接口,如果有很多的代理对象的话那么就要定义并实现很多的接口这样会很麻烦,所以我们要使用动态代理!
动态代理:
1、我们首先创建一个抽象的接口类
2、然后分别创建一个用户类和一个代理类都继承抽象接口,
3、在创建两个代理的对象类
在代理类里面写一个类似与session里面的getMapper的方法,
这里用户想用那个被代理的对象就将这个代理对象交给代理类这样就可以不用定义那么多的代理接口了。
七、AOP(面向切面编程)
aop其实是一种思想,就是把很多没有任何关联的方法通过aop技术给关联起来(上面的动态代理就是运用了aop技术)
aop主要分为两部分
1、通用代码:重复使用的代码
2、业务代码:根据不同的功能而写的代码

AOP的配置
配置声明
<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/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
1、首先要将业务类配置到bean里面(核心方法)

2、将切面类配置到bean里面(附加方法)

3、对aop进行配置

测试类的方法

浙公网安备 33010602011771号