spring语法——spring-beans

理解:springbeans在spring框架的配置文件中定义,在spring容器中初始化,然后被注入到对应的类里面,可以有效的去耦合。

写两个两个个问题
1,如何让spring接管整个应用程序,将所有的类实例化
2,这些实例化的类不管是谁,这里面有引用类型ref三种,value值类型八种,他们是怎样注入值的

通过小案例来理解spring:
一,实例化对象
创建一个maven项目,用spring之前第一步,加入jar包,目前最新版本为4.3.3.RELEASE,建议使用此版本:

<!-- 引入spring依赖 -->
		<dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring-context</artifactId>
	        <version>4.3.3.RELEASE</version>
	    </dependency>
``

不用spring直接实例化得到对象,写一个EntityDao的接口,在接口实现类EntityDaoImpl里面写一个测试方法添加的方法,然后在test测试类里面测试:

```java
for (int i = 0; i < 5; i++) {//实例化5个对象
				EntityDao entityDao = new EntityDaoImpl();
				System.out.println(entityDao);
	} 
	结果如下,每一次实例化都会开辟一个不同的地址空间

在这里插结果入图片描述
使用spring,新建一个applicationContext.xml配置文件,在配置文件里面将项目中的类写成bean,然后实例化放到容器池里。

<?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-4.3.xsd">  
			
		</beans>

下面的bean就相当于实例化了,就相当于是EntityDao entityDao = new EntityDaoImpl…
在这里插入图片描述
配置文件中初始化完之后,在test测试类里进行测试,测试的步骤,
1,读取配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext(“applicationContext.xml”);
2,从配置文件里得到要测试的bean,
EntityDao entityDao2 = (EntityDao)ctx.getBean(“EntityDao”);
3,输出结果(为了有对比,上面得到的bean进行循环5次)如下,可以看出不管调用几次,开辟的地址空间都只是这一个,大大节省了上面实例化占用的地址空间。
在这里插入图片描述
二,给对象传值的三种方式,这里主要介绍使用sping配置文件进行赋值。
1,构造方法
2,javabean
3,spring配置文件赋值,其实也是使用上面两种方法进行注入,代码如下(name里写对象属性名):
在这里插入图片描述
三,通过上面已经知道在spring配置文件中配置好bean后,可以直接通过获取配置文件来使用,可是这样又有一个问题,在一个项目中,有持久层,业务层,控制层,怎样才能在控制层调用业务层,业务层调用持久层的时候只用获取一次配置文件呢?这里就来讨论一下耦合问题。
配置文件里面进行实例化如下:
在这里插入图片描述
注意点:上面代码中实例化控制层,注意真正的控制层肯定不用实例化的,因为它是受web容器控制的,也就是web.xml。。。。property 里面的name是对应类里面定义的属性。。。。reg里面是指定的引用对象。

配置好spring配置文件后,就可以注入到对应的类里面了。比如业务层里面注入xml文件中配置好的引用对象如下。
在这里插入图片描述
最后,一张项目调用实例化过程流程图:
在这里插入图片描述
ps:本人是一枚入门菜鸟,写博客是想记录一下自己在学习过程中的知识点。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptMapper' defined in file [D:\WorkSpace\work13\djd_server\target\classes\com\jiading\djd\mapper\DeptMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; 元素内容必须由格式正确的字符数据或标记组成。 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at com.jiading.djd.DjdApplication.main(DjdApplication.java:14) [classes/:na]报错了
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值