• 注解


    一、IOC注解

      1、常用注解

        @Compoent  实现业务Bean的注入
        @Repository 专门注入Dao 基于@Compoent

      2、IOC注解需要需要在大配置文件中,扫描包   <context:compoent-scan base-package="cn.spring"/>

        

       2、向spring容器中注入Bean

        (1)简单案例:

          创建Dao层接口

    package ca.mapper.impl;
    
    import com.spring.entity.UserInfo;
    /**
     * Dao层接口
     */
    public interface IUserInfoMapper {
        public int addUser(UserInfo info);
    }

          创建创建Dao层实现类Impl

    package ca.mapper.impl;
    
    import com.spring.entity.UserInfo;
    import com.spring.mapper.IUserMapper;
    import org.springframework.stereotype.Repository;
    /**
     * Dao层标识   @Repository
     */
    @Repository("iUserInfoMapperImpl")  //注入注解
    public class IUserInfoMapperImpl implements IUserInfoMapper {
        @Override
        public int addUser(UserInfo info) {
            System.out.println("添加成功");
            return 1;
        }
    }

          创建Service层

    package ca.mapper.service;
    
    import com.spring.entity.UserInfo;
    
    public interface IUserInfoService {
        public int addUser(UserInfo info);
    }

          创建Service实现层Impl

    package ca.mapper.service;
    
    import ca.mapper.impl.IUserInfoMapper;
    import com.spring.entity.UserInfo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import javax.annotation.Resource;
    
    @Service("iUserInfoServiceImpl")
    public class IUserInfoServiceImpl implements IUserInfoService{
        //植入Dao层对象
        //@Resource 默认是根据ByName的方式,但是一旦名字为空,就根据ByType
        @Resource(name = "iUserInfoServiceImpl")
        private IUserInfoMapper iUserInfoMapper;
    
        @Override
        public int addUser(UserInfo info) {
            return iUserInfoMapper.addUser(info);
        }
    }

          test实现类

    package ca.mapper.service;
    
    import ca.mapper.impl.IUserInfoMapper;
    import com.spring.entity.UserInfo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import javax.annotation.Resource;
    
    @Service("iUserInfoServiceImpl")
    public class IUserInfoServiceImpl implements IUserInfoService{
        //植入Dao层对象
        //@Resource 默认是根据ByName的方式,但是一旦名字为空,就根据ByType
        @Resource(name = "iUserInfoServiceImpl")
        private IUserInfoMapper iUserInfoMapper;
    
        @Override
        public int addUser(UserInfo info) {
            return iUserInfoMapper.addUser(info);
        }
    }

    二、AOP注解

      示例:

        配置applicationContext.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:aop="http://www.springframework.org/schema/aop"
           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/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <!--扫描注解:包扫描器-->
        <context:component-scan base-package="aop.mapper"/>
    
        <!--开启AOP注解支持-->
        <aop:aspectj-autoproxy/>
    </beans>

        创建一个类

    package aop.mapper;
    
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    /**
     * 增强类,你得吧增强类注入Spring容器
     */
    @Aspect
    @Component
    public class MyAdvice {
        //定义一个空方法,为了可以应用切点表达式
        @Pointcut("execution(* aop.mapper.*.do*(..))")
            public void pointCut(){
        }
    
        //自定义增强方法
        @Before("pointCut()")
        public void before(){
            System.out.println("===============前置增强===============");
        }
    
        //自定义增强方法
        @AfterReturning("pointCut()")
        public void after(){
            System.out.println("================后置增强=====================");
        }
    }   
      创建业务类IdoSomeService
    package aop.mapper;
    
    import org.springframework.stereotype.Service;
    /**
     * 业务类
     */
    @Service("idoSomeService")
    public class IdoSomeService {
        public void doSome(){
            System.out.println("业务类当中的doSome方法");
        }
    
        public void say(){
            System.out.println("业务类当中的say方法");
        }
    }

        test测试代码

    package com.mapper;
    
    import aop.mapper.IdoSomeService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AopMapperTest {
        public static void main(String[] args) {
            ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContextAOPMapper.xml");
            IdoSomeService idoSomeService = (IdoSomeService)cxt.getBean("idoSomeService");
            idoSomeService.doSome();
            idoSomeService.say();
        }
    }

        测试结果如下

        

  • 相关阅读:
    Valid Palindrome
    LeetCode: Path Sum 2
    LeetCode: Path Sum
    LeetCode:Convert Sorted Array to Binary Search Tree
    LeetCode:Convert Sorted List to Binary Search Tree
    LeetCode: Balanced Binary Tree
    LeetCode: Maximum Depth of Binary Tree
    django文件上传,只上传一张
    URLError: <urlopen error [Errno 10061] >
    error: <class 'socket.error'>, [Errno 101] Network is unreachable: file: /usr/local/python2.7.12/lib/python2.7/socket.py line: 575
  • 原文地址:https://www.cnblogs.com/tinghao/p/11776581.html
Copyright © 2020-2023  润新知