Spring基础

1、Spring的基础入门

1.1、引入maven依赖:

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>5.1.4.RELEASE</version>
</dependency>

1.2、创建applicationContext.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">

    <bean id="person" class="com.wx.study.Person">
     
    </bean>
</beans>

1.3、创建测试文件

public class StudySptringTest {
    @Test
    public void test(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Person person = (Person) ctx.getBean("person");
        System.out.println(person);
    }
}
ClassPathXmlApplicationContext(配置文件路径)是web项目中使用的

2、常用方法测试

  • getBean()方法

用户获取applicationContext中配置的bean对象,通过对象的id获取,也可以通过对象的class来获取

通过id来获取:

public void test(){
         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        Person person = (Person) ctx.getBean("person");
        System.out.println(person);

        Person bean = ctx.getBean(Person.class);
        System.out.println(bean);
}

//Person{age=18, name='zhangsan'}
//Person{age=18, name='zhangsan'}

注意: 

配置文件中不能有两个id重复的bean对象,有得话启动就会报错。

如果有两个bean的class相同,那么在调用ctx.getBean(Person.class);的时候也会报错,因为系统无法识别是获取哪一个bean对象。

  • getBeanDefinitionNames()

获取配置文件中的所有bean对象的id值

@Test
public void test(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");

        String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
        System.out.println("getBeanDefinitionNames()");
        for (int i = 0; i < beanDefinitionNames.length; i++) {
            System.out.println(beanDefinitionNames[i]);
        }
}

//getBeanDefinitionNames()
//person
//person1
  • getBeanNamesForType(class)

根据指定class获取配置文件中的bean对象

    @Test
    public void test(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");

        String[] beanNamesForType = ctx.getBeanNamesForType(Person.class);
        System.out.println("getBeanNamesForType()");
        for (int i = 0; i < beanNamesForType.length; i++) {
            System.out.println(beanNamesForType[i]);

        }
    }
//getBeanNamesForType()
//person
  • containsBeanDefinition()

判断配置文件中id为指定值的bean对象

    <bean id="person" name="p1" class="com.wx.study.Person">
        <property name="age" value="18"></property>
        <property name="name" value="zhangsan"></property>
    </bean>

    <bean id="person1" name="p2" class="com.wx.study.Person1">
    </bean>
boolean isPersonDef = ctx.containsBeanDefinition("person1");
boolean isPersonDefP = ctx.containsBeanDefinition("p2");
System.out.println("isPersonDef="+isPersonDef);
System.out.println("isPersonDefP="+isPersonDefP);

//isPersonDef=true
//isPersonDefP=false
  • containsBean()

判断配置文件中id或者name为指定值的bean对象

boolean isPerson = ctx.containsBean("person");
boolean isPersonP = ctx.containsBean("p1");
System.out.println("isPerson="+isPerson);
System.out.println("isPersonP="+isPersonP);


//isPerson=true
//isPersonP=true

3、总结

其实Spring底层用的就是代理来帮助我们创建对象的,来帮助我们的程序进行解耦。

如果想看spring为何可以解耦,可以看下我这篇博客:https://blog.csdn.net/wxssbsb/article/details/114679668?spm=1001.2014.3001.5502

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值