spring的基本使用图文详解

spring的基本使用

  • 建一个基本的maven java项目

在这里插入图片描述

e7kg==,size_20,color_FFFFFF,t_70,g_se,x_16)

  • 添加依赖

   <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.9</version>
            <scope>test</scope>
        </dependency>
        <!--        注解-->
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <!-- 包含Spring框架基本的核心工具类 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <!-- 所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及
        进行Inversion ofControl / Dependency Injection(IoC/DI)操作相关的所有类-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <!-- 为Spring 核心提供了大量扩展 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <!--Spring-aop(必须)
            包含在应用中使用Spring 的AOP 特性时所需的类和源码级元数据支持。-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <!-- aspectj的runtime包(必须) -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.5</version>
        </dependency>
        <!-- aspectjweaver是aspectj的织入包(必须) -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.0</version>
        </dependency>

  • 创建配置文件

在这里插入图片描述
初始xml头部

<?xml version="1.0" encoding="UTF-8"?>
<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">
       </beans>

简单的例子:

  • 创建2个类对象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Car {
    private String name;
    private String model;
    private String color;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Users {
    private Integer id;
    private String name;
    private Car car;
}
  • 添加配置文件
<!--    spring创建对象默认是单例的  启动容器的时候就创建对象了
        scope="singleton"    单例的   默认   开启容器的时候就创建对象
        scope="prototype"    非单例的   从容器取数据的时候才创建对象
        -->
    <bean id="car" class="com.ljw.bean.Car" scope="singleton" >
        <!-- 通过setter给属性赋值-->
        <property name="name" value="奥迪"></property>
        <property name="model" value="A8"></property>
        <property name="color" value="黑色"></property>
    </bean>
    
    <bean id="car2" class="com.ljw.bean.Car">
        <!-- 通过构造函数给属性赋值-->
        <constructor-arg name="name" value="宝马"></constructor-arg>
        <constructor-arg name="model" value="X3"></constructor-arg>
        <constructor-arg name="color" value="黑色"></constructor-arg>
    </bean>
    
    <bean id="users" class="com.ljw.bean.Users">
        <property name="name" value="admin"></property>
        <!-- 参照外部bean  car的值 -->
        <property name="car" ref="car2"></property>
    </bean>
    
    <bean id="users2" class="com.ljw.bean.Users">
        <property name="name" value="admin"></property>
       <property name="car">
           <!--  参照内部bean  只能在这里使用 不能作为标识在外部调用-->
           <bean id="car4" class="com.ljw.bean.Car" p:name="内部车" p:color="红色" p:model="内部型号"></bean>
       </property>
    </bean>
  • 测试
    @Test
    public void test01(){
        //启动 spring 容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //从 spring 容器中取出数据(3中获取方式)
        Car car = (Car) context.getBean("car");
        Car car1 = context.getBean("car", Car.class);
//        Car car2 = context.getBean(Car.class);
        //通过对象调用方法
        System.out.println(car.getName());
        System.out.println(car1.getName());
//        System.out.println(car2.getName());
        //当配置文件中Car有两个id标识时,不能使用Car.class获取数据
    }
    @Test
    public void test02(){
        //启动 spring 容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //从 spring 容器中取出数据
        Car car = (Car) context.getBean("car2");
        Car car1 = context.getBean("car2", Car.class);
        //通过对象调用方法
        System.out.println(car.getName());
        System.out.println(car1.getName());
    }
    @Test
    public void test03(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Users users = context.getBean("users", Users.class);
        //获取users下car的信息
        System.out.println(users.getCar());
    }

关于bean的详细使用

<!--通过P空间赋值 -->
    <bean id="car3" class="com.ljw.bean.Car" p:name="奔驰" p:model="GLC" p:color="白色"></bean>
    <bean id="users" class="com.ljw.bean.Users">
  • 定义一个集合类
@Data
public class MyCollections {
    private String[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String, String> map;
}

xml文件

 <bean id="myCollections" class="com.ljw.bean.MyCollections">
        <property name="array">
            <array>
                <value>数组元素1</value>
                <value>数组元素2</value>
                <value>数组元素3</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>列表元素1</value>
                <value>列表元素2</value>
                <value>列表元素3</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value>集合元素1</value>
                <value>集合元素2</value>
                <value>集合元素3</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="键1" value="值1"></entry>
                <entry key="键2" value="值2"></entry>
                <entry key="键3" value="值3"></entry>
            </map>
        </property>
    </bean>

测试

@Test
    public void test04(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        MyCollections myCollections = context.getBean("myCollections", MyCollections.class);
        //获取myCollections下集合的信息
        System.out.println(Arrays.toString(myCollections.getArray()));
        System.out.println(myCollections.getList());
        System.out.println(myCollections.getSet());
        System.out.println(myCollections.getMap());
    }

元素也可以注入为对象

 <property name="list">
            <list>
            <!-- 注入Bean引用 -->
               <ref bean="car"></ref>
               <ref bean="car2"></ref>
               <ref bean="car3"></ref>
            </list>
        </property>

用了util标签后,集合可以简写

 <util:set id="set">
        <value>集合元素1</value>
        <value>集合元素2</value>
        <value>集合元素3</value>
    </util:set>

就像这样:

 <property name="set" ref="set">
        </property>

FactoryBean

FactoryBean是一个接口,当在IOC容器中的Bean实现了FactoryBean后,通过getBean(String BeanName)获取到的Bean对象并不是FactoryBean的实现类对象,而是这个实现类中的getObject()方法返回的对象。要想获取FactoryBean的实现类,就要getBean(&BeanName),在BeanName之前加上&。

@Data
public class MyFactoryBean implements FactoryBean<Object> {
    private String type;
    @Override
    public Object getObject() throws Exception {
        if (type.equals("user")) {
            return new Users();
        }else {
            return null;
        }

    }

    @Override
    public Class<?> getObjectType() {
        return Users.class;
    }
}

xml配置

    <bean id="myFactoryBean" class="com.ljw.bean.MyFactoryBean" p:type="user">

    </bean>

测试

    @Test
    public void test05(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Object obj = context.getBean("myFactoryBean");
        System.out.println(obj.getClass().getName());
        Object obj2 = context.getBean("&myFactoryBean");
        System.out.println(obj2.getClass().getName());
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值