Spirng的原理,以及实现

Spirng

Spirng的东西非常之多,这里讲两个重要的东西
I

CO和AOP

IOC

其实说白了,IOC其实就是个,工厂,以前我们开发,都是自己直接拿着工具来做产品(对象),现在我们想要某个产品,直接叫工厂做给我们,这就是IOC,那为什么要这么做,方便统一管理啦。

如何实现呢:
(1)下载包,建议下载spring-webmvc,因为他会下载许多包

   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>

(2)建立spring配置文件

   </bean>
    <bean id="testUserDaox" class="com.kuang.dao.Userdaox">
    </bean>

    <bean id="UserService" class="com.kuang.service.UserService">
        <property name="user" ref="testUserDaox"></property>
    </

(3),构建对象,getBean,强转类型

public class Test {
   public static void main(String[] args)
   {
       ApplicationContext context = new ClassPathXmlApplicationContext("Spring-config.xml");
       Hello hello=(Hello)context.getBean("hello");
       String  name=hello.getName();
       System.out.println(name);

       UserService userService=(UserService)context.getBean("UserService");
       userService.getUser();
   }

这里主要讲解一下注解
如果不想在spring配置文件注册,可以使用下面的
(1)首先在spring里面注册

<context:annotation-config/>
<context:component-scan base-package="com.kuang"></context:component-scan>

(2)然后在想要注册的类添加@@Component就可以了,对应类似的有@service和@controller
对于属性的用 @Autowired进行注入(Qualifier,可以指定类名,或者注册的id)

@Component
public class Peopel {
    @Autowired
    @Qualifier(value = "cat2")
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    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;
    }

    @Override
    public String toString() {
        return "Peopel{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

AOP

AOP,叫做切面编程,什么叫前面,其实就是,贯穿所有的相关类的一个操作,在不改变原有代码的前提下,横向的添加方法,下面给出几种实现方式
(1)自己定义多个切面方法
例如这里定义

package com.kuang.log;

import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;


import java.lang.reflect.Method;
@Component

public class befor implements MethodBeforeAdvice {
    //method:对象的方法;Object[] :args;Object:目标类
    public void before(Method method, Object[] objects, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"DE"+method.getName());
    }
}
package com.kuang.log;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
@Component
public class after implements AfterReturningAdvice {
    public void afterReturning(Object returnValues, Method method, Object[] objects, Object target) throws Throwable {
        System.out.println(method.getName()+"xx"+returnValues);
    }
}

(2)然后在spring配置文件里面添加切入点

<aop:config>
        <aop:aspect ref="serviceLOG">
            <aop:pointcut id="pointCut" expression="execution(* com.kuang.service.User.*(..))"/>
            <aop:before method="befor" pointcut-ref="pointCut"/>
            <aop:after method="after" pointcut-ref="pointCut"/>
        </aop:aspect>
    </aop:config>

对了还有导入织入依赖:

  <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

第二种:
利用注解的形式
在spring配置文件中,加入

!--方式三,利用注解,需要在xml中配置注解支持-->
    <aop:aspectj-autoproxy/>

然后在代码@Aspect,进一步的采用 @Before和@After一类的注解定义通知

package com.kuang.log;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class serviceLOG {
    @Before("execution(* com.kuang.service.User.*(..))")
    public void befor()
    {
        System.out.println("fuck befor");
    }
    @After("execution(* com.kuang.service.User.*(..))")
    public void after()
    {
        System.out.println("fuck after");
    }
}

相对来说,注解简单好多,好多,建议用注解方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值