B_第02章:Spring基础



spring的最大目的之一就是使得j2ee开发更加容易。

spring是一个开源的轻量级的控制反转(IOC)和面向切面(AOP)的容器框架。

依赖注入(DI)和控制反转(IOC):

IOC的优缺点:
    优点:实现组件的解藕,提高程序的灵活性和可维护性,提高组件的重用效率,并为系统搭建一个灵活可扩展的平台。
    缺点:
        1,把创建对象变得复杂了。
        2,通过反射创建对象,在效率上有损耗(可以忽略)
        3,xml进行配置太复杂,但是使用注解会简单一些。

    简单实现依赖注入:
        public class CleanAir {
            @Override
            public String toString() {
                return "cleanAir";
            }
        }
        public class Person {
            public Person(CleanAir cleanAir) {
                System.out.println(cleanAir.toString());
            }
        }
    对上面的例子进行优化,可以对CleanAir使用interface接口
        public interface IAir {
            public String toString();
        }
        public class CleanAir implements IAir {
            @Override
            public String toString() {
                return "cleanAir";
            }
        }
        public class DirtyAir {
            @Override
            public String toString() {
                return "dirtyAir";
            }
        }
        public class Person {
            public Person(IAir iAir) {
                System.out.println(iAir.toString());
            }
        }

spring中实现注入的方式有两种
    1,在application.xml文件中进行配置
    2,使用注解 @Component,但是需要在陪着文件中开启扫描:<context:component-scan base-package="org.example.demoA"/>

    1,在application.xml文件中进行配置
        <bean id="CleanAir" class="org.example.demoA.CleanAir">
            <qualifier value="cleanAir"/>
        </bean>
    2,使用注解:<context:component-scan base-package="org.example.demoA"/>
        @Component
        public class Cat implements Animal {
            @Override
            public String toString() {
                System.out.println("this is cat");
                return super.toString();
            }
        }

        @Component
        public class Like {
            Animal animal;
            @Autowired //代表自动注入
            public Like(@Qualifier("cat") Animal animal) {
                this.animal=animal;
            }
            public void play() {
                System.out.println(animal.toString());
            }
        }


AOP:aop使用横切技术,将影响了多个类的公共部分封装到一个可用模块,并将其命名为 Aspect,即切面。
    AOP实际上是由目标类的代理实现的。

    动态代理:
        public interface IService {
            public void service(String name) throws Exception;
        }

        public class ServiceImplA implements IService {
            @Override
            public void service(String name) throws Exception {
                System.out.println(name+"==========");
            }
        }

        public class DynaProxyServiceA implements InvocationHandler {
            private Object target;
            public Object bind(Object object) {
                this.target = object;
                System.out.println(‘1‘);
                Object obj = Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this);
                System.out.println(‘2‘);
                return obj;
            }

            public Object invoke(Object object, Method method, Object[] args) throws Throwable {
                Object result = null;
                System.out.println("method=" + method);
                System.out.println("args=" + args);
                System.out.println("target=" + this.target);
                System.out.println("log start");
                try {
                    result = method.invoke(object, args);
                } catch (Exception e) {
                    throw e;
                }
                System.out.println("log end");
                return result;
            }

            public static void main(String[] args) throws Exception {
                IService iService = (IService) new DynaProxyServiceA().bind(new ServiceImplA());
                iService.service("sssss");
            }
        }

B_第02章:Spring基础

上一篇:SpringBoot打成war包,部署到Tomcat服务器


下一篇:8个最热门的微信小程序 UI 组件库