Spring

Spring

1.IOC的部分

  1. IOC的理念:控制反转:***基于setter方法***让用户在客户端能选择实现的方法。

1.基于setter方法bean的配置

    <bean id="Hello" class="com.yang.pojo.Hello">
        <!-- collaborators and configuration for this bean go here -->
        <property name="String" value="Spring-Hello"/>
    </bean>

测试类的调用

@Test
public void Test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    //强转类
        Hello string = (Hello) context.getBean("Hello");
        System.out.println(string);
}

2.基于构造函数配置bean.xml

<!--name属性也是别名,并可以取多个别名-->
    <bean id="UserName" class="com.yang.pojo.UserName" name="user123">
        <constructor-arg name="name" value="springCon" ></constructor-arg>
    </bean>

3.几种常见的注入,依赖注入

  • ​ 详见包Spring_day01—Spring-04-di

4.自动装配

  • //自动装配
    //<bean id="Person" class="com.yang.pojo.Person" p:name="zyn" autowire="byName"/>
    @Autowired
    //选定要传入的是哪一个对象
    @Qualifier(value = "cat22")
    
    

5.注解

  •     <!-- 指定要扫描的包,这个包下注解就生效-->
        <!-- 此两句为 使注解生效,开启注解支持-->
        <context:component-scan base-package="com.yang"/>
        <context:annotation-config/>
        //等价于 <bean id="user" class="com.yang.pojo.User" p:name="zyn">
    //Component 组件
        @Component		//pojo
        @Service		//service
        @Controller		//controller
        @Repository		//dao
    

通用的代码

  • ​ pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>Spring_Day01</artifactId>
            <groupId>org.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>Spring-09-AOP</artifactId>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
        <dependencies>
    
            <!--Aop的包-->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.4</version>
            </dependency>
        </dependencies>
    
    </project>
    
  • bean.xml

    
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 指定要扫描的包,这个包下注解就生效-->
        <!-- 此两句为 使注解生效,开启注解支持-->
        <context:component-scan base-package="com.yang"/>
        <context:annotation-config/>
    </beans>
    
  • Test

    @Test
        public void Test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            User user1 = (User) context.getBean("user");
            User user = new User("zyn");
        }
    

AOP的实现

  1. 静态/动态代理
  2. 何为aop:切面编程思想

AOP的实现

//开启注解支持
<aop:aspectj-autoproxy/>

//
@Aspects

Before,After,AfterTrothowing…方法

package com.yang.Diy;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AnnoPointCut {

    @Pointcut("execution(* com.yang.service.UserServiceImpl.*(..))")
    public void PointCut(){}

    @Before("PointCut()")
    public void before(){
        System.out.println("===执行前===");
    }

    @After("PointCut()")
    public void after(){
        System.out.println("===已经执行===");
    }

    @Around("PointCut()")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前"+jp.toString());

        Object proceed = jp.proceed();  //执行方法

        System.out.println("环绕后"+jp.toString());


    }

}

注意:对service增加并发功能(如日志等),不修改原本代码,使用新建代理类,即为aop思想。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值