• spring IOC与AOP的练习


    创建Car类,1) 借助set给属性注入值

    
    <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="car" class="openlab.com.beans.Car">
            <constructor-arg value="baoma" index="0">constructor-arg>
            <constructor-arg value="german" index="1">constructor-arg>
            <constructor-arg value="100" index="2">constructor-arg>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2)借助构造方法给属性注入值。【两个都是三个参数的构造方法】

    
    <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="car" class="openlab.com.beans.Car">
            <property name="brand" value="baoma">property>
            <property name="corp" value="german">property>
            <property name="price" value="400000">property>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2>创建类,包含集合属性,如何给以下集合属性注入信息
    CollectionTest类

    package openlab.com.beans;
    
    import lombok.Data;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    @Data
    public class CollectionTest {
        private String[] arr;
        private List<String> list;
        private Map<String,String> map;
        private Set<String> set;
        private List<Emp> emps;
        private Map<String,Emp> empMap;
    
        @Override
        public String toString(){
            return "CollectionTest{" +"arr"+ Arrays.toString(arr)+
                    ", list"+list+
                    ", map"+map+
                    ", set"+set+
                    ", emps"+emps+
                    ", empMap"+empMap+"}";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    bean中配置

    
    <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="CollectionTest" class="openlab.com.beans.CollectionTest">
            <property name="arr" >
                <array>
                    <value>1value>
                    <value>1value>
                    <value>1value>
                    <value>1value>
                array>
            property>
            <property name="list">
                <list>
                    <value>1value>
                    <value>1value>
                    <value>1value>
                list>
            property>
            <property name="map">
                <map>
                    <entry key="1" value="c">entry>
                    <entry key="2" value="y">entry>
                    <entry key="3" value="x">entry>
                map>
            property>
            <property name="set">
                <set>
                    <value>1value>
                    <value>1value>
                    <value>1value>
                set>
            property>
            <property name="emps">
               <list>
                   <value>1value>
                   <value>1value>
                   <value>1value>
               list>
            property>
            <property name="empMap">
                <map>
                    <entry key="1" value="c">entry>
                    <entry key="2" value="y">entry>
                    <entry key="3" value="x">entry>
                map>
            property>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    3>创建部门和员工类将类交给spring管理,给dept中的list集合赋值.一种list赋值。util:list

    emp:给emp中的dept赋值

    <bean id="emp1" class="openlab.com.Emp">
            <property name="name" value="cyx"/>
            <property name="id" value="1"/>
        bean>
        <bean id="emp2" class="openlab.com.Emp">
            <property name="name" value="cyx2"/>
            <property name="id" value="2"/>
        bean>
        <bean id="dept" class="openlab.com.Dept">
            <property name="id" value="1"/>
            <property name="empList">
                <list>
                    <ref bean="emp1"/>
                    <ref bean="emp2"/>
                list>
            property>
        bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    AOP练习
    1>创建普通的类
    public class EmpDao(){
    //添加方法
    //修改的方法
    //查看的方法
    }
    2>创建切面类,切面类中规定对哪些类中的方法做增强
    查看,添加,修改 在方法执行都需要获取数据库的链接,在方法执行之后都需要关闭数据库的链接【环绕】
    添加,修改的方法,在方法之前执行,需要开启事务,如果没有异常提交事务,如果有异常,需要回滚事务
    3>用配置和注解的方式实现
    jdbcConfig

    import javax.sql.DataSource;
    
    public class jdbcConfig {
        @Value("${jdbc.driver}")
        private String  driver;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.user}")
        private String userName;
        @Value("${jdbc.password}")
        private String password;
       
        @Bean
        public DataSource dataSource(){
            DruidDataSource ds=new DruidDataSource();
            ds.setDriverClassName(driver);
            ds.setUrl(url);
            ds.setUsername(userName);
            ds.setPassword(password);
            return ds;
        }
           
        @Bean
        public PlatformTransactionManager transactionManager(DataSource dataSource) {
            DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
            transactionManager.setDataSource(dataSource);
            return transactionManager;
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    MybatisConfig:

    package openlab.com.config;
    
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.mapper.MapperScannerConfigurer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.sql.DataSource;
    
    public class MybatisConfig {
    
    
        @Bean
        public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
    
            sqlSessionFactoryBean.setTypeAliasesPackage("com.cdcas.pojo");
            return sqlSessionFactoryBean;
        }
    
        @Bean
        public MapperScannerConfigurer mapperScannerConfigurer() {
            MapperScannerConfigurer mp = new MapperScannerConfigurer();
    
            mp.setBasePackage("com.cdcas.mapper");
            return mp;
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    springConfig:

    package openlab.com.config;
    
    import org.springframework.context.annotation.*;
    
    //Configuration标识这个类为一个配置类
    @Configuration
    //扫描我们的包
    @ComponentScan("com.cdcas")
    //导入要加载的配置类
    @Import({jdbcConfig.class,MybatisConfig.class})
    //导入要加载的配置文件
    @PropertySource("classpath:jdbc.properties")
    //告诉Spring开启了Aop注解
    @EnableAspectJAutoProxy
    //告诉Spring用注解事务驱动
    @EnableTransactionManagement
    public class SpringConfig {
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    EmpMapper:

    package openlab.com.mapper;
    
    import openlab.com.beans.Emp;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public interface EmpMapper {
    //    添加
        @Insert("insert into tbl_emp values (null,#{name})")
        void add(Emp emp);
    //    修改
        @Update("update tbl_emp set name=#{name} where id=#{id}")
        void update(Emp emp);
    //    查看
        @Select("select * from tbl_emp")
        List<Emp> select();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    AOP:

    package openlab.com.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    
    @Component
    @Aspect
    public class ProjectAdvice {
       
        @Pointcut("execution(* com.cdcas.service.*Service.*(..))")
        private static void servicePt() {
        }
    
        @Pointcut("execution(* com.cdcas.service.*Service.*(..))&&!" +
                "execution(* com.cdcas.service.*Service.select(..))")
        private static void servicePt2() {
        }
    
        @Autowired
        DataSource dataSource;
    
        @Around("servicePt()")
        public Object operation(ProceedingJoinPoint pjp) throws Throwable {
            Connection connection = dataSource.getConnection();
            System.out.println("数据库连接" + connection);
            Object obj = pjp.proceed();
            System.out.println("关闭数据库连接");
            connection.close();
            return obj;
        }
    
        @Around("servicePt2()")
        public Object operation2(ProceedingJoinPoint pjp) {
            try {
                return pjp.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
  • 相关阅读:
    Linux网络编程——IO多路复用
    删除单链表偶数节点
    清华计算几何-ConvexHull(凸包)-极点InTriangle/ToLeft Test
    听GPT 讲Istio源代码--pkg(4)
    音视频云架构
    《真象还原》读书笔记——第八章 内存管理系统(字符串操作、位图定义与实现)
    网络安全应急响应与实践合集
    <网络> HTTP
    TCP协议
    SSM+健身房管理系统 毕业设计-附源码191656
  • 原文地址:https://blog.csdn.net/qq_51552328/article/details/126602484