JavaWeb框架————Spring(二)

1、使用注解配置Spring
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

代码示例

Car.java

package cn.ctgu.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("car")
public class Car {
    @Value("玛莎拉蒂")
    private String name;
    @Value("绿色")
    private String color;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public String toString() {
        return "Car [name=" + name + ", color=" + color + "]";
    }

}

User.java

package cn.ctgu.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//<bean name="user" class="cn.ctgu.bean.User"/>和注解等同
@Component("user")
    /*以下三个注解和上面的注解一样,只是应用于不同层注解对象时的区分,其实每个层都可以用这四个
     * @Service("user")//service层注解对象
     * @Controller("user")//web层注解对象
     * @Repository("user")//dao层注解对象
     * 
     * */
//指定对象的作用范围
@Scope(scopeName="singleton")//注解成单例或多例模式
//<bean name="user" class="cn.ctgu.bean.User" scope="singleton" init-method="init" destroy-method="destroy"></bean>
public class User {
    @Value("tom")//<property name="name" value="tom"></property>
    //通过反射的Field赋值
    private String name;

    @Value(value="18")//只有一个值的的时候,可以不写value,即@Value("18")
    private Integer age;

    @Autowired  //自动装配,为car属性注入配置的car对象,问题:如果匹配多个类型一致的对象,将无法选择具体注入哪一个对象
    /*<!--引用类型注入:为car属性注入下方配置的car对象 -->
    <property name="car" ref="car"></property>*/
    @Qualifier("car2")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象
    //@Resource(name="car2")//手动注入,指定注入哪个名称的对象,推荐使用这种
    private Car car;

    public User() {
        System.out.println("User对象空参构造方法!");
        // TODO Auto-generated constructor stub
    }
    public User(String name, Car car) {
        System.out.println("User(String name,Car car)!!");
        this.name = name;
        this.car = car;
    }

    public User(Car car, String name) {
        System.out.println("User(String name,Car car)!!");
        this.name = name;
        this.car = car;
    }
    public User(Integer name, Car car) {
        System.out.println("User(String name,Car car)!!");
        this.name = name+"";
        this.car = car;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String getName() {
        return name;
    }
//跟上面效果一样,为name赋值   @Value("tom")//<property name="name" value="tom"></property>
    //通过set方法赋值
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @PostConstruct //在对象被创建后调用,相当于init-method
    public void init() {
        System.out.println("我是初始化方法!");
    }
    @PreDestroy  //在对象销毁之前被调用,相当于destroy-method
    public void destroy() {
        System.out.println("我是销毁方法!");
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
    }


}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">

<!--指定扫描cn.ctgu.bean包及子包下的所有类的注解  -->
<context:component-scan base-package="cn.ctgu.bean"></context:component-scan>   

<bean name="car2" class="cn.ctgu.bean.Car">
        <property name="name" value="兰博基尼"></property>
        <property name="color" value="黄色"></property>
    </bean> 

</beans>

2、Spring和Junit整合测试
Demo.java

package cn.ctgu.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.ctgu.bean.User;

//Junit与Spring整合
@RunWith(SpringJUnit4ClassRunner.class)//帮我们创建容器
//指定创建容器时使用哪个配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
    //将名为user的对象注入到u变量中
    @Resource(name="user")
    private User u;
    @Test
    public void fun1() {
        System.out.println(u);
    }
}

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">

<!--指定扫描cn.ctgu.bean包及子包下的所有类的注解  -->
<context:component-scan base-package="cn.ctgu.bean"></context:component-scan>   

<bean name="car2" class="cn.ctgu.bean.Car">
        <property name="name" value="兰博基尼"></property>
        <property name="color" value="黄色"></property>
    </bean> 

</beans>

3、Spring中的代理
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
代码示例

动态代理

UserService.java(接口)

package cn.ctgu.service;

public interface UserService {
    void save();
    void delete();
    void find();
    void update();
}

UserServiceImpl.java(接口实现类)

package cn.ctgu.service;

public class UserServiceImpl implements UserService {

    @Override
    public void save() {
        System.out.println("保存用户");

    }

    @Override
    public void delete() {
        System.out.println("删除用户");

    }

    @Override
    public void find() {
        System.out.println("查找用户");

    }

    @Override
    public void update() {
        System.out.println("修改用户");

    }

}

UserServiceProxyFactory.java

package cn.ctgu.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import cn.ctgu.service.UserService;
import cn.ctgu.service.UserServiceImpl;
//动态代理
public class UserServiceProxyFactory implements InvocationHandler{

    private UserService us;

    public UserServiceProxyFactory(UserService us) {
        super();
        this.us = us;
    }

    public UserService getUserServiceProxy() {
        //生成动态代理
        UserService usProxy=(UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), 
                            this);
        //返回
        return usProxy;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //打开事务
        System.out.println("打开事务");
        //被增强对象的的方法
        Object invoke=method.invoke(us, args);
        //打开事务
        System.out.println("提交事务");
        return invoke;
    }

}

cglib代理
UserServiceProxyFactory2.java

package cn.ctgu.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import cn.ctgu.service.UserService;
import cn.ctgu.service.UserServiceImpl;
//cglib代理
public class UserServiceProxyFactory2 implements MethodInterceptor{


    public UserService getUserServiceProxy() {
        Enhancer en=new Enhancer();//帮我们生成代理对象

        en.setSuperclass(UserServiceImpl.class);//设置对谁进行代理

        en.setCallback(this);//代理要做什么
        UserService us=(UserService) en.create();//创建代理对象
        return us;
    }

    @Override
    /*
     * proxyObj被代理对象(原始对象)
     * method  原始方法
     * arg  方法参数
     * methodproxy  代理方法 
     *  
     * */
    public Object intercept(Object proxyobj, Method method, Object[] arg, MethodProxy methodProxy) throws Throwable {

        //打开事务
        System.out.println("打开事务");
        //调用原有方法
        Object returnValue=methodProxy.invokeSuper(proxyobj, arg);
        //提交事务
        System.out.println("提交事务");
        return returnValue;
    }


}

测试类
Demo.java

package cn.ctgu.proxy;

import org.junit.Test;

import cn.ctgu.service.UserService;
import cn.ctgu.service.UserServiceImpl;

public class Demo {
    //测试代理对象
    @Test
    public void fun1() {
        UserService us=new UserServiceImpl();

        UserServiceProxyFactory factory=new UserServiceProxyFactory(us);

        UserService usProxy=factory.getUserServiceProxy();

        usProxy.save();
    }
    //测试cglib代理
    @Test
    public void fun2() {

        UserServiceProxyFactory2 factory=new UserServiceProxyFactory2();

        UserService usProxy=factory.getUserServiceProxy();

        usProxy.save();
        //判断代理对象是否属于被代理对象类型
        //结果:true   因为代理对象继承被代理对象
        System.out.println(usProxy instanceof UserServiceImpl);
    }
}

4、Spring中的AOP
4.1 xml配置AOP
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

目标对象
UserService.java(接口)

package cn.ctgu.service;

public interface UserService {
    void save();
    void delete();
    void find();
    void update();
}

UserServiceImpl.java

package cn.ctgu.service;

public class UserServiceImpl implements UserService {

    @Override
    public void save() {
        System.out.println("保存用户");

    }

    @Override
    public void delete() {
        System.out.println("删除用户");

    }

    @Override
    public void find() {
        System.out.println("查找用户");

    }

    @Override
    public void update() {
        System.out.println("修改用户");

    }

}

通知
MyAdvice.java

package cn.ctgu.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

//通知类
public class MyAdvice {
    //前置通知(目标方法运行前调用)

    //后置通知(如果出现异常将不会调用,在目标方法运行之后调用)

    //环绕通知(在目标方法之前和之后都调用)

    //异常拦截通知(如果出现异常就会调用)

    //后置通知(无论是否出现异常都会调用,在目标方法运行之后调用)
//===================================================
    //前置通知
    public void before() {
        System.out.println("这是前置通知!!");
    }
    //后置通知
    public void afterReturning() {
        System.out.println("这是后置通知(如果出现异常将不会调用)");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分");
        Object proceed=pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分");
        return proceed;
    }
    //异常拦截通知
    public void afterException() {
        System.out.println("出事了!出现异常了");
    }
    //后置通知
    public void after() {
        System.out.println("这是后置通知(如果出现异常也会调用)");
    }
}

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!--准备工作:导入aop(约束)命名空间 -->
<!-- 1、配置目标对象 -->
    <bean name="userServiceTarget" class="cn.ctgu.service.UserServiceImpl"></bean>
<!-- 2、配置通知对象 -->
    <bean name="myAdvice" class="cn.ctgu.aspect.MyAdvice"></bean>
<!-- 3、配置将通知织入目标对象 -->
    <aop:config>
        <!--配置切入点
            public void cn.ctgu.service.UserServiceImpl.save()
            void cn.ctgu.service.UserServiceImpl.save() 
            * cn.ctgu.service.UserServiceImpl.save() 返回值不限制

            * cn.ctgu.service.UserServiceImpl.*()  配置所有方法,参数为空参
            * cn.ctgu.service.UserServiceImpl.*(..) 配置所有方法,参数无限制
            * cn.ctgu.service.*ServiceImpl.*(..)  配置cn.ctgu.service包下包含ServiceImpl类下的所有方法
            * cn.ctgu.service..*ServiceImpl.*(..)  配置cn.ctgu.service包及子包下包含ServiceImpl类下的所有方法
         -->
        <aop:pointcut expression="execution(* cn.ctgu.service.UserServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myAdvice">
            <!--指定名为before方法作为前置通知  -->
            <aop:before method="before" pointcut-ref="pc"/>
            <!-- 后置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc"/>
            <!--环绕  -->
            <aop:around method="around" pointcut-ref="pc"/>
            <!--异常拦截通知  -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"/>
            <!-- 后置 -->
            <aop:after method="after" pointcut-ref="pc"/>
        </aop:aspect>

    </aop:config>
</beans>

测试类
Demo.java

package cn.ctgu.springaop;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.ctgu.bean.User;
import cn.ctgu.service.UserService;

//Junit与Spring整合
@RunWith(SpringJUnit4ClassRunner.class)//帮我们创建容器
//指定创建容器时使用哪个配置文件
@ContextConfiguration("classpath:cn/ctgu/springaop/applicationContext.xml")
public class Demo {
    //将名为user的对象注入到u变量中
    @Resource(name="userServiceTarget")
    private UserService us;
    @Test
    public void fun1() {
        us.save();
    }
}

4.2 使用注解配置AOP
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

目标对象
UserService.java

package cn.ctgu.service;

public interface UserService {
    void save();
    void delete();
    void find();
    void update();
}

UserServiceImpl.java

package cn.ctgu.service;

public class UserServiceImpl implements UserService {

    @Override
    public void save() {
        System.out.println("保存用户");

    }

    @Override
    public void delete() {
        System.out.println("删除用户");

    }

    @Override
    public void find() {
        System.out.println("查找用户");

    }

    @Override
    public void update() {
        System.out.println("修改用户");

    }

}

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!--准备工作:导入aop(约束)命名空间 -->
<!-- 1、配置目标对象 -->
    <bean name="userServiceTarget" class="cn.ctgu.service.UserServiceImpl"></bean>
<!-- 2、配置通知对象 -->
    <bean name="myAdvice" class="cn.ctgu.annotationaop.MyAdvice"></bean>
<!-- 3、开启使用注解完成织入 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

将通知织入目标对象
MyAdvice.java

package cn.ctgu.annotationaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

//使用注解完成aop配置
//通知类
@Aspect//表示该类是一个通知类
public class MyAdvice {
    //前置通知(目标方法运行前调用)

    //后置通知(如果出现异常将不会调用,在目标方法运行之后调用)

    //环绕通知(在目标方法之前和之后都调用)

    //异常拦截通知(如果出现异常就会调用)

    //后置通知(无论是否出现异常都会调用,在目标方法运行之后调用)
//===================================================
    //抽取出注解,以便后面使用MyAdvice.pc,否则将要写全
    @Pointcut("execution(* cn.ctgu.service.UserServiceImpl.*(..))")
    public void pc() {}

    //前置通知
    //指定该方法是前置通知,并制定切入点
    //@Before("execution(* cn.ctgu.service.UserServiceImpl.*(..))")
    @Before("MyAdvice.pc()")
    public void before() {
        System.out.println("这是前置通知!!");
    }
    //后置通知
    //@AfterReturning("execution(* cn.ctgu.service.UserServiceImpl.*(..))")
    @AfterReturning("MyAdvice.pc()")
    public void afterReturning() {
        System.out.println("这是后置通知(如果出现异常将不会调用)");
    }
    //环绕通知
    //@Around("execution(* cn.ctgu.service.UserServiceImpl.*(..))")
    @Around("MyAdvice.pc()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分");
        Object proceed=pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分");
        return proceed;
    }
    //异常拦截通知
    //@AfterReturning("execution(* cn.ctgu.service.UserServiceImpl.*(..))")
    @AfterReturning("MyAdvice.pc()")
    public void afterException() {
        System.out.println("出事了!出现异常了");
    }
    //后置通知
    //@After("execution(* cn.ctgu.service.UserServiceImpl.*(..))")
    @After("MyAdvice.pc()")
    public void after() {
        System.out.println("这是后置通知(如果出现异常也会调用)");
    }
}

测试类
Demo.java

package cn.ctgu.annotationaop;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.ctgu.bean.User;
import cn.ctgu.service.UserService;

//Junit与Spring整合
@RunWith(SpringJUnit4ClassRunner.class)//帮我们创建容器
//指定创建容器时使用哪个配置文件
@ContextConfiguration("classpath:cn/ctgu/annotationaop/applicationContext.xml")
public class Demo {
    //将名为user的对象注入到u变量中
    @Resource(name="userServiceTarget")
    private UserService us;
    @Test
    public void fun1() {
        us.save();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值