Spring笔记

Spring是一个轻量级的控制反转IoC和面向切面AOP的容器框架。文章介绍了Spring中的IoC(依赖注入)概念,通过beans.xml配置文件展示了如何进行对象的创建和注入。此外,还讨论了自动装配、注解开发以及静态和动态代理。最后,文章简要提到了AOP在处理横切关注点如日志、安全和事务等方面的作用。
摘要由CSDN通过智能技术生成

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器(框架)。

控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。

控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的。

反转 : 程序本身不创建对象 , 而变成被动的接收对象 。

beans.xml可根据index参数下标、参数名字、参数类型设置。

可用import实现多个xml文件整合。

DI注入

1.构造器注入

2.set注入(重点实现)

Student.java

package org.example;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private  String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private  String wife;
    private Properties info;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

Address.java

package org.example;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

注入:

 <bean id="address" class="org.example.Address"/>
    <bean id="student" class="org.example.Student">
        <property name="name" value="哈哈"/>
        <property name="address" ref="address"/>
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
            </array>
        </property>

        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
            </list>
        </property>
        <property name="card">
            <map>
                <entry key="身份证" value="1234566788">

                </entry>
            </map>
        </property>

        <property name="games">
            <set>
                <value>LOL</value>
            </set>
        </property>

        <property name="wife">
            <null></null>
        </property>
        <property name="info">
            <props>
                <prop key="学号">2020202</prop>
                <prop key="姓名">小李</prop>
            </props>
        </property>
    </bean>

测试:

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

结果:

Student{name='哈哈', address=Address{address='null'}, books=[红楼梦, 西游记], hobbys=[听歌, 敲代码], card={身份证=1234566788}, games=[LOL], wife='null', info={学号=2020202, 姓名=小李}}

3.p命名和c命名注入

自动装配

byname自动装配

    <bean id="cat" class="org.example.Cat"/>
    <bean id="dog" class="org.example.Dog"/>
    <bean id="people" class="org.example.People" autowire="byName">
        <property name="name" value="哈哈"/>
    </bean>

当一个bean节点带有 autowire byName的属性时。

  1. 将查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。

  2. 去spring容器中寻找是否有此字符串名称id的对象。

  3. 如果有,就取出注入;如果没有,就报空指针异常。

此外,还有byType

同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常。

使用注解

@Autowired

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>

注意配置注解支持。

过程中遇见Autowired members must be defined in valid Spring bean 

前面添加@component。

自动装配无法通过一个注解完成,搭配@Qualifier,指定一个唯一的bean对象注入。

@Resource

如有指定的name属性,先按该属性进行byName方式查找装配;其次再进行默认的byName方式进行装配;如果以上都不成功,则按byType的方式自动装配。两个都找不到则报错。

注解开发

配置文件

<?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: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/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--指定注解扫描包-->
    <context:component-scan base-package="org.example"/>

</beans>

User.java

@Component
public class User {
    @Value("哈哈")
    public String name;
}

提供了set方法,能在set方法上添加@value(“值”);

@Component三个衍生注解

@Controller:web层

@Service:service层

@Repository:dao层

JavaConfig

User.java

@Component
public class User {
    private String name="哈哈";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

 Config.java

@Configuration
public class Config {
    @Bean
    public User getUser(){
        return new User();
    }
}

测试

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        User getUser = (User) context.getBean("getUser");
        System.out.println(getUser.getName());
    }
}

静态代理

Rent . java 即抽象角色

public interface Rent {
    public void rent();
    }

Host . java 即真实角色

public class Host implements Rent {
    public void rent() {
        System.out.println("房出租");
    }
}

Proxy . java 即代理角色

public class Proxy implements Rent{
    private Host host;
    public Proxy(){

    }
    public Proxy(Host host){
        this.host = host;
    }

    @Override
    public void rent() {
        host.rent();
    }
}

Client . java 即客户

public class Client {
    public static void main(String[] args) {
        Host host = new Host();
        Proxy proxy = new Proxy(host);
        proxy.rent();
    }
}

动态代理

动态代理的角色和静态代理的一样。

动态代理的代理类是动态生成的,静态代理的代理类是我们提前写好的。

动态代理分为两类 : 一类是基于接口动态代理 ,一类是基于类的动态代理。

AOP

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。        AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知 执行的 “地点”的定义。
连接点(JointPoint):与切入点匹配的执行点。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值