Spring重点总结


spring其实就是容器,IOC也是其中一个,把我们的对象收集起来

IOC创建对象

  • User
public class User {
    private String name;
    public User(){  
        System.out.println("User的无参构造");
    }
    public String getName() {
        return name;
    }     
    public void setName(String name) {
        this.name = name;
    }
}
  • applicatonContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <import resource="beans.xml"></import>
    
    <alias name="hello" alias="hello1"></alias>
    
    <bean id="hello" class="com.liu.pojo.Hello" name = "hello2,hello3 hello4;hello5">
        <property name="str" value="Spring"/>
    </bean>
</beans>
  • test
public class MyTest {
    public static void main(String[] args) {
        //获取Spring的上下文对象
        ApplicationContext Context = new ClassPathXmlApplicationContext("applicatonContext.xml");
        //我们的对象现在都在Spring中管理了,我们要使用,直接去里面取出来就可以了
        Hello hello = (Hello) Context.getBean("hello");
        // Hello hello = Context.getBean("hello",Hello.class); 就不用每次强转了
        System.out.println(hello);
    }
}
  • 使用Spring来创建对象,在Spring这些对象都称为Bean ,对象字段就是property 属性
  • <bean id="hello" class="com.liu.pojo.Hello"></bean> 相当于创建对象了 (new Hello()无参构造)
  • <property name="name" value="ljy"></property>相当于user.set(ljy)
  • id = 变量名(bean 的唯一标识符 也就是我们学过的对象名)
  • class = new 的对象(对象所对应的全限定名:包名+类名)
  • property 相当于给对象中的属性设置一个值
  • alias给变量名起别名,bean中的name:也是别名 而且name更高级 可以同时取多个别名 空格和逗号和分号都可以分割
  • <import resource="beans.xml"></import>导入其他配置文件

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了

依赖注入DI

注入主要是对对象中的属性注入,也就是给属性赋值,也就是setxxx()方法,现在交给spring容器来做

构造器注入
  • 有参构造
public User(String name){
    this.name = name;
}
  • applicatonContext.xml 三种
<!--第一种 下标赋值 -->
<bean id="user" class="com.liu.pojo.User">
    <constructor-arg index="0" value="刘建宇"></constructor-arg>
</bean>

<!--第二种方式 通过类型创建 不建议使用-->
<bean id="user" class="com.liu.pojo.User">
    <constructor-arg type="java.lang.String" value="liujianyu"></constructor-arg>
</bean>

<!--第三种 直接通过参数名来设置-->
<bean id="user" class="com.liu.pojo.User">
    <constructor-arg name = "name" value="刘建"></constructor-arg>
</bean>
  • 无参构造,采用的set方式
public User(){  
    System.out.println("User的无参构造");
}
  • applicatonContext.xml
<!--相当于创建对象了 (无参构造)-->
<bean id="user1" class="com.liu.pojo.User"></bean>

<bean id="user2" class="com.liu.pojo.User" >
    <property name="name" value="123"></property>
</bean>

<bean id="user" class="com.liu.pojo.User" name="user3,user4 user5;user6">
    <property name="name" value="ljy"></property>
</bean>
<!--起别名,如果添加了别名  我们也可以使用别名获取到这个对象-->
<alias name="user" alias="user7"></alias>
  • test 注意输出三次无参构造
public class MyTest {
    public static void main(String[] args) {
        //User user = new User();
        //spring容器 就类似于婚介
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicatonContext.xml");
        User user = (User) context.getBean("user");
    }
}
/*
User的无参构造
User的无参构造
User的无参构造
name=ljy
*/
set注入
  • Student
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobby;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
}
  • Address
public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
  • applicatonContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="Address" class="com.liu.pojo.Address">
        <property name="address" value="成都"></property>
    </bean>
    
    <bean id="student" class="com.liu.pojo.Student">
        <!--第一种,普通值注入,value-->
        <property name="name" value="ljy"></property>
        <!--第二种,Bean注入,ref-->
        <property name="address" ref="Address"></property>
        <!--第三种,数组,value-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
        <!--list注入-->
        <property name="hobby">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>
        <!--Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="1234"/>
                <entry key="银行卡" value="1212"/>
            </map>
        </property>
        <!--Set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CS</value>
                <value>CF</value>
            </set>
        </property>
        <!--null注入-->
        <property name="wife">
            <value>null</value>
        </property>
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="学号">20190525</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>

</beans>
  • test
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicatonContext.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
/*
        Student{name='ljy',
        address=Address{address='成都'},
        books=[红楼梦, 水浒传, 三国演义],
        hobby=[听歌, 敲代码, 看电影],
        card={身份证=1234, 银行卡=1212},
        games=[LOL, CS, CF], wife='null',
        info={学号=20190525, 性别=男}}
*/
    }
}

Bean的自动装配

使用Autowired 我们可以不用编写set方法了,前提式你这个自动装配的属性在IOC(Spring)容器中,还减少了我们new的步骤

  • People、Cat、Dog
public class People {
    //如果显示的定义了Autowired属性为false,说明了这个对象可以为null,否则不允许为空
    @Autowired(required = false)
    @Qualifier(value = "cat") // 
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog")
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }
    // 使用了Autowired就不用编写set方法了
    /*public void setCat(Cat cat) {
        this.cat = cat;
    }*/
    public Dog getDog() {
        return dog;
    }
    /*public void setDog(Dog dog) {
        this.dog = dog;
    }*/
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}
public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}
  • applicatonContext.xml
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--开启注解的支持,也就是支持Autowired-->
    <context:annotation-config/>
    
    <bean id="cat1" class="com.liu.pojo.Cat"></bean>
    <bean id="cat2" class="com.liu.pojo.Cat"></bean>
    <bean id="dog1" class="com.liu.pojo.Dog"></bean>
    <bean id="dog2" class="com.liu.pojo.Dog"></bean>
    
    <bean id="people" class="com.liu.pojo.People">
        <property name="name" value="小刘"></property> <!--因为这里Cat,Dog使用的Autowired自动注入所以不用写dog,														cat 写了反而报错因为没有了set方法-->
    </bean>
</beans>
  • test
public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);

        people.getDog().shout();
        people.getCat().shout();
        System.out.println(people.getCat());
        System.out.println(people.getDog());
        System.out.println(people.getName());
        System.out.println(people);
    }
}
/*
wang~
miao~
com.liu.pojo.Cat@5a4aa2f2
com.liu.pojo.Dog@6591f517
小刘
People{cat=com.liu.pojo.Cat@5a4aa2f2, dog=com.liu.pojo.Dog@6591f517, name='小刘'}
*/
  • byName:会自动在容器上下文中查找,bean id必须唯一
  • byType:会自动在容器上下文中查找,属性类型相同的bean!必须保证类型全局唯一 也就是class唯一, id可以省略

@Autowired

  • @Autowired先去匹配类型(也就是byType),如果重复再在去找beanid,如果beanid和类定义变量名(cat,dog)不相同则可以使用@Qualifier
@Autowired(required = false)
@Qualifier(value = "cat1") 
private Cat cat;
@Autowired
@Qualifier(value = "dog1")
private Dog dog;
// 因为这里面没有和类定义变量名(cat,dog)相同的beanid 所以要使用Qualifier来指定。
    <bean id="cat1" class="com.liu.pojo.Cat"></bean>
    <bean id="cat2" class="com.liu.pojo.Cat"></bean>
    <bean id="dog1" class="com.liu.pojo.Dog"></bean>
    <bean id="dog2" class="com.liu.pojo.Dog"></bean>

@Resource

  • @Resource注解先匹配beanid(byName)再去匹配类型 ,和@Autowired相反,当beanid和属性类型都不行时,也可以使用name标签去唯一标识bean注入
public class People {
    @Resource(name="cat2")
    private Cat cat;
    @Resource()
    private Dog dog;
}
// 也就是cat不等于cat2,然后再去匹配类型发现类型唯一,所以就会锁定到cat2这个对象,如果把cat1加上就匹配类型的时候不唯一了,所以必须加name来锁定不然报错

	// <bean id="cat1" class="com.liu.pojo.Cat"></bean>
    <bean id="cat2" class="com.liu.pojo.Cat"></bean>
    <bean id="dog1" class="com.liu.pojo.Dog"></bean>
    <bean id="dog2" class="com.liu.pojo.Dog"></bean>

@Resource 和 @Autowired的区别:

  • 都是用来自动装配的,都可以放在我们的属性字段上

  • @Autowired通过byType的方式实现,而且必须要求这个对象存在【常用

  • @Resource默认通过byName的方式实现,如果找不到名字,则通过byType实现!

  • @Resource相当于@Autowired和@Qualifier的结合

总结

  • 使用@Autowired就是我们直接从容器中那我们的对象,不用再用我们去编写set方法去设置,也减少了我们new的步骤

  • 使用之前要先开启注解支持<context:annotation-config/>

注解开发

​ 在使用注解需要导入的context约束,增加注解的支持

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
                           
       <!--指定要扫描的包,这个包下的注解就会自动生效-->
		<context:component-scan base-package="com.liu"/>
        <!--开启注解支持-->                             
        <context:annotation-config/>
</bean
  • @Component
@Component  //等价于  <bean id="user" class="com.liu.pojo.User"/>
//@Component 组件
@Scope("singleton") //标注为单例模式(singleton),原型模式(prototype)
public class User {
    @Value("liujianyu") //相当于<property name="name" value="liujianyu"></property>
    public String name;
    @Value("liujianyu2")
    public void setName(String name) {
        this.name = name;
    }
}
  • @Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层
    • dao【@Repository】
    • service【@Service】
    • controller【@Controller】
    • 这四个功能都是一样的,都是代表将某个类注册到Spring中,装配Bean

Java配置xml

我们现在要完全不适用Spring的xml配置了,全权交给Java来做!

  • User
//这里这个注解的意思,就是说明这个类被Spring接管了,注册到了容器中
@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }
	@Value("liujianyu")
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}
  • @Configuration代表这是一个配置类,就和我们之前看的beans.xml是一样的
@Configuration
//这个也会被Spring容器托管,注册到容器中,因为它本来就是一个@Component
@ComponentScan("com.liu.pojo")//扫描包
@Import(MyConfig2.class)//引入其他配置类
public class MyConfig {
    @Bean
    //注册一个bean,就相当于之前我们写的一个bean标签
    //这个方法的名字,就相当于bean标签中的id属性
    //这个方法的返回值,就相当于bean标签中的class属性
    public User getUser() {
        System.out.println("执行了此方法=================");
        return new User();//就是返回要注入到bean的对象!
    }
}
  • test
public class MyTest {
    public static void main(String[] args) {
        //如果完全使用了配置类方式去做,我们只能通过AnnotationConfig,上下文来获取容器,通过配置类的class对象加载!
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User getUser = (User) context.getBean("user"); // 这里可以去user是因为@Component,这个走的是xml中的bean
        System.out.println(getUser.getName());
         
        User user = context.getBean("getUser", User.class); //取方法名,这个是先走的bean,再走的new User();
        System.out.println(user.getName());

        System.out.println(user == getUser);
    }
}
/*
执行了此方法=================
liujianyu
执行了此方法=================
liujianyu
false
*/

代理模式

租房

  • 静态代理
//租房
public interface Rent {
    public void rent();
}
//房东
public class Host implements Rent {

    public void rent() {
        System.out.println("房东要出租房子");
    }
}
//代理
public class Proxy implements Rent {
    private Host host;

    public Proxy(Host host) {
        this.host = host;
    }
    public void rent() {
        seeHouse();
        host.rent();
        hetong();
        fare();
    }
    //看房
    public void seeHouse(){
        System.out.println("中介带你看房");
    }
    //签合同
    public void hetong(){
        System.out.println("签租赁合同");
    }
    //收中介费
    public void fare(){
        System.out.println("收中介费");
    }
}
//客户
public class Client {
    public static void main(String[] args) {

        //房东要租房子
        Host host = new Host();
        //代理,中介帮房东租房子。但是呢,一般代理角色会有一些附属操作!
        Proxy proxy = new Proxy(host);
        //你不用面对房东,直接找中介租房即可!
        proxy.rent();
        
        /*
        	中介带你看房
            房东要出租房子
            签租赁合同
            收中介费
            */
    }
}

用户管理业务

  • 静态代理
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void query();
}
public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增加了一个用户");
    }

    public void delete() {
        System.out.println("删除了一个用户");

    }

    public void update() {
        System.out.println("修改了一个用户");

    }

    public void query() {
        System.out.println("查询了一个用户");

    }
    //改动原有的业务代码,在公司是大忌!所以我们要用代理来扩张功能
}
public class UserServiceProxy implements UserService{
    private UserServiceImpl userService;

    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }

    public void add() {
        log("add");
        userService.add();
    }

    public void delete() {
        log("delete");
        userService.delete();
    }

    public void update() {
        log("update");
        userService.update();
    }

    public void query() {
        log("query");
        userService.query();
    }
    //日志方法
    public void log(String msg){
        System.out.println("[Debug]使用了"+msg+"方法");
    }
}
public class Client {
    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        UserServiceProxy proxy = new UserServiceProxy();
        proxy.setUserService(userService);
        proxy.add();
        /*
        	[Debug]使用了add方法
            增加了一个用户
            */
    }
}

租房

  • 动态代理
//我们会用这个类,自动生成代理类!
public class ProxyInvocationHandler implements InvocationHandler {

    //被代理的接口
    private Rent rent;

    public void setRent(Rent rent) {
        this.rent = rent;
    }

    //生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this );
    }
        //处理代理实例,并返回结果
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            //动态代理的本质,就是使用反射机制实现!
            seeHouse();
            Object result = method.invoke(rent, args);
            fare();
            return result;
        }
    public void seeHouse() {
        System.out.println("中介带看房子");
    }
    public void fare() {
        System.out.println("收中介费");
    }
}	
public class Client {
    public static void main(String[] args) {
        //真实角色
        Host host = new Host();

        //代理角色:现在没有
        ProxyInvocationHandler pih = new ProxyInvocationHandler();
        //通过调用程序处理角色来处理我们要调用的接口对象!
        pih.setRent(host);
        Rent proxy = (Rent) pih.getProxy();//这里的Proxy就是动态生成的,我们并没有写
        proxy.rent();
        /*
        	Connected to the target VM, address: '127.0.0.1:50900', transport: 'socket'
            中介带看房子
            房东要出租房子
            收中介费
            Disconnected from the target VM, address: '127.0.0.1:50900', transport: 'socket'
            */
    }
}

用户管理业务

  • 动态代理
//我们会用这个类,自动生成代理类!
public class ProxyInvocationHandler implements InvocationHandler {

    //被代理的接口(真实角色)
    private Object target;
    public void setTarget(Object target) {
        this.target = target;
    }

    //生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this );
    }

    //处理代理实例,并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质,就是使用反射机制实现!
        log(method.getName());//增加日志
        Object result = method.invoke(target, args);
        return result;
    }

    public void log(String msg) {
        System.out.println("执行了"+msg+"方法");
    }
}
public class Client{
    public static void main(String[] args) {
        //真实角色
        UserServiceImpl userService = new UserServiceImpl();
        //代理角色,不存在
        ProxyInvocationHandler pih = new ProxyInvocationHandler();

        pih.setTarget(userService);//设置要代理的对象
        //动态生成代理类
        UserService proxy = (UserService) pih.getProxy();
        proxy.delete();
        /*
        执行了delete方法
		删除了一个用户
		*/
    }
}

AOP

【重点】使用AOP注入,需要导入一个依赖包

<dependencies>
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>
</dependencies>

AOP在不改变原有代码的情况下,去增加新的功能(代理)

  • Service
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}
public class UserServiceImpl implements UserService{
    public void add() {
        System.out.println("增加一个方法");
    }
    public void delete() {
        System.out.println("删除一个方法");
    }
    public void update() {
        System.out.println("修改一个方法");
    }
    public void select() {
        System.out.println("查询一个方法");
    }
}
  • test
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理,代理的是接口:注意点
        UserService userService = (UserService) context.getBean("userService");
        userService.delete();
    }
}
  • applicationContext.xml
  1. 方式一:使用原生Spring API接口
<?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: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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!--注册bean-->
    <bean id="userService" class="com.liu.service.UserServiceImpl"/>
    <bean id="log" class="com.liu.log.Log"/>
    <bean id="afterLog" class="com.liu.log.AfterLog"/>

    <!--配置AOP:需要导入AOP的约束-->
    <aop:config>
        <!--切入点:expression:表达式,execution(要执行的位置! 修饰词 返回值 类名 方法名 参数)-->
        <aop:pointcut id="pointcut" expression="execution(* com.liu.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增强!-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
   
</beans>
  • log、afterlog
public class Log implements MethodBeforeAdvice {
    //method:要执行的目标对象方法
    //target:目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}
public class AfterLog implements AfterReturningAdvice {
    //returnValue:返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws 	
        Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果		  
                           为:"+returnValue+target.getClass().getName());
    }
}
/*
com.liu.service.UserServiceImpl的delete被执行了
删除一个方法
执行了delete方法,返回结果为:nullcom.liu.service.UserServiceImpl
*/ 
  1. 方式二:自定义类
<!--......-->
<bean id="diy" class="com.liu.diy.DiyPointCut"></bean>
<aop:config>
    <!--自定义切面 ref:要引用的类-->
    <aop:aspect ref="diy">
        <!--切入点-->
        <aop:pointcut id="point" expression="execution(* com.liu.service.UserServiceImpl.*(..))"/>
        <!--通知-->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>
<!--........-->
  • 自定义类DiyPointCut
public class DiyPointCut {
    public void before() {
        System.out.println("=============方法执行前============");
    }
    public void after() {
        System.out.println("=============方法执行后============");
    }
}
/*
=============方法执行前============
删除一个方法
=============方法执行后============
*/
  1. 方式三:使用注解实现
<!--......-->
<bean id="annotationPointCut" class="com.liu.diy.AnnotationPointCut"/>
<!--开启注解支持! JDK(默认 proxy-target-class="false") cglib proxy-target-class="true"-->
<aop:aspectj-autoproxy/>
<!--........-->
  • AnnotationPointCut
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.liu.service.UserServiceImpl.*(..))")//定义切点
    public void before() {
        System.out.println("=============方法执行前============");
    }
    @After("execution(* com.liu.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("=============方法执行后============");
    }
    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.liu.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("=============环绕前============");
        Object proceed = joinPoint.proceed();//执行方法
        System.out.println("=============环绕后============");

        Signature signature = joinPoint.getSignature();//获得签名
        System.out.println("signature"+signature);
        System.out.println(proceed);
    }
}
/*
=============环绕前============
=============方法执行前============
删除一个方法
=============方法执行后============
=============环绕后============
signaturevoid com.liu.service.UserService.delete()
null
*/

整合Myabatis

导入外部properties资源

<!--找到我们的jdbc.properties-->
<!--context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
</bean>-->

1、第一种

  • UserMapper、UserMapperImpl、UserMapper.xml
public interface UserMapper {
    public List<User> getUser();
}

public class UserMapperImpl implements UserMapper{
    //我们的所有操作,都是用sqlSession来执行,现在都是用SqlSessionTemplate
    private SqlSessionTemplate sqlSessionTemplate;
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
    public List<User> getUser() {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.getUser();
    }
}
/*
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.liu.dao.UserMapper">

<select id="getUser" resultType="com.liu.pojo.User">
    select * from mybatis.user;
</select>
</mapper
*/
  • User
public class User  {
    private int id;
    private String name;
    private String pwd;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}
  • spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--dataSource-->
    <!--DataSource:使用Spring的数据源替换Mybatis的配置 上c3p0 dbcp druid
    我们这里使用Spring提供的JDBC org.springframework.jdbc.datasource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?allowPublicKeyRetrieval=true&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--重点-->
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"></property>
        <!--绑定Mybatis配置文件-->
        <!--这里就可以替代mybatis配置文件里面的内容
         <mappers>
            <mapper class="com.liu.dao.UserMapper"/>
        </mappers>-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:com/liu/dao/UserMapper.xml"/>
    </bean>
	<!--sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
	<!--userMapper-->
    <bean id="userMapper" class="com.liu.dao.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"></property>
    </bean>
</beans>
  • test
public class MyTest  {
    public static void main(String[] args) throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper bean = (UserMapper) context.getBean("userMapper");
        List<User> user = bean.getUser();
        for (User user1 : user) {
            System.out.println(user1);
        }
}

2、第二种

  • UserMapperImpl2
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    public List<User> getUser() {
        return getSqlSession().getMapper(UserMapper.class).getUser();

    }
}
  • spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--dataSource-->
    <!--DataSource:使用Spring的数据源替换Mybatis的配置 上c3p0 dbcp druid
    我们这里使用Spring提供的JDBC org.springframework.jdbc.datasource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?allowPublicKeyRetrieval=true&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--重点-->
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"></property>
        <!--绑定Mybatis配置文件-->
        <!--这里就可以替代mybatis配置文件里面的内容
         <mappers>
            <mapper class="com.liu.dao.UserMapper"/>
        </mappers>-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:com/liu/dao/UserMapper.xml"/>
    </bean>
    <!--userMapper2-->
    <bean id="userMapper2" class="com.liu.dao.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

</beans>
  • test
public class MyTest  {
    public static void main(String[] args) throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper bean = (UserMapper) context.getBean("userMapper2");
        List<User> user = bean.getUser();
        for (User user1 : user) {
            System.out.println(user1);
        }
}

spring配置事务

  • applicationContext.xml
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="XXX" transaction-manager="transactionManager">
    <!--给那些方法配置事务-->
    <!--配置事务的传播特性-->
    <tx:attributes>
        <!--add方法配置事务-->
<!--            <tx:method name="add" propagation="REQUIRED"/>-->
        <!--所有方法-->
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<!--配置事务切入-->
<aop:config>
    <aop:pointcut id="xxx" expression="execution(* com.liu.dao.*.*(..))"/>
    <aop:advisor advice-ref="XXX" pointcut-ref="xxx"/>
</aop:config>
  • 在UserMapperImpl中的selectUser方法中制造错误
public List<User> selectUser() {
    User user = new User(16, "AAA", "1234");
    UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
    mapper.deleteUser(15);
    // id主键,并且已经有了16号,所以会报错,那么上面删除的15号也会被回滚
    mapper.addUser(user);
    return mapper.selectUser();
}
  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 33
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早上真起不来!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值