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