文档章节

spring

architect刘源源
 architect刘源源
发布于 06/12 19:05
字数 1537
阅读 9
收藏 0

小技巧:

当需要导入到jar包的某个类时,可以点击buildPath后的jar包找到对应的类,然后ctrl+T查看具体的继承关系,也可以show view 中的outline查看具体的对应方法。

<?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="student" class="com.atguigu.ioc.scope.Student" scope="singleton">
		<property name="sid" value="1001"></property>
		<property name="sname" value="张三"></property>
	</bean>

</beans>
package com.atguigu.ioc.scope;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		
		/**
		 * 若spring中有单例模式的bean,在初始化容器时九华创建此对象
		 * 而多例即原型的bean,会在使用时创建
		 */
		ApplicationContext ac = new ClassPathXmlApplicationContext("scope.xml");
		Student student1 = ac.getBean("student", Student.class);
		Student student2 = ac.getBean("student", Student.class);
		System.out.println(student1);
		System.out.println(student2);
		
	}
	
}
六月 12, 2019 7:03:36 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4437c4: startup date [Wed Jun 12 19:03:36 CST 2019]; root of context hierarchy
六月 12, 2019 7:03:36 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [scope.xml]
Student 构造方法
com.atguigu.ioc.scope.Student@2f686d1f
com.atguigu.ioc.scope.Student@2f686d1f
<?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-4.0.xsd">

	<!-- 
		<context:component-scan>:扫描组件,对设置的包下面的类进行扫苗,会将加上注解的类作为spring的组件进行加载
		组件:指spring中管理的bean
		作为spring的组件进行加载:会自动在spring的配置文件中生成相对应的bean,这些bean的id会以类的首字母小写为值
		<context:include-filter>:在设定的包结构下,再次通过注解或类型具体包含到某个或某几个类
		注意:在使用包含时,一定要设置use-default-filters="false",将默认的过滤(即扫描包下所有的类)关闭
		<context:exclude-filter>:在设定的包结构下,再次通过注解或类型排除某个或某几个类
		注意:在使用排除时,一定要设置use-default-filters="true",将默认的过滤(即扫描包下所有的类)打开
		切记:一个<context:component-scan>中可以出现多个include,也可以同时出现多个exclude,但是两个不能同时出现
	 -->
	<context:component-scan base-package="com.atguigu.ioc.userMod" use-default-filters="true">
		<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
		<!-- <context:include-filter type="assignable" expression="com.atguigu.ioc.userMod.service.UserServiceImpl"/> -->
		<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> -->
		<!-- <context:exclude-filter type="assignable" expression="com.atguigu.ioc.userMod.dao.UserDaoImpl"/> -->
	</context:component-scan>

	<!-- <bean id="userController" class="com.atguigu.ioc.userMod.controller.UserController"></bean>
	<bean id="userServiceImpl" class="com.atguigu.ioc.userMod.service.UserServiceImpl"></bean>
	<bean id="userDaoImpl" class="com.atguigu.ioc.userMod.dao.UserDaoImpl"></bean> -->
	
	<!-- <bean id="userDao" class="com.atguigu.ioc.userMod.dao.UserDaoImpl"></bean> -->
	

</beans>
package com.atguigu.ioc.userMod.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.atguigu.ioc.userMod.service.UserService;

/**
 * 基于注解的组件化管理:
 * @Component,@Controller(控制层),@Service(业务层),@Repository(持久层)
 * 以上四个注解功能完全相同,不过在实际开发中,要在实现不同功能的类上加上相应的注解
 * 完成组件化管理的过程:
 * 1、在需要被spring管理的类上加上相应注解
 * 2、在配置文件中通过<context:component-scan>对所设置的包结构进行扫描,就会将加上注解的类,作为spring的组件进行加载
 * 组件:指spring中管理的bean
       作为spring的组件进行加载:会自动在spring的配置文件中生成相对应的bean,这些bean的id会以类的首字母小写为值;
       也可以通过@Controller("beanId")为自动生成的bean指定id
 * 自动装配:在需要赋值的非字面量属性上,加上@Autowired,就可以在spring容器中,通过不同的方式匹配到相对应的bean
 * @Autowired装配时,会默认使用byType的方式,此时要求spring容器中只有一个能够为其赋值
 * 当byType实现不了装配时,会自动切换到byName,此时要求spring容器中,有一个bean的id和属性名一致
 * 若自动装配时,匹配到多个能够复制的bean,可使用@Qualifier(value="beanId")指定使用的bean
 * @Autowired和@Qualifier(value="beanId")可以一起作用域一个带形参的方法上,此时,@Qualifier(value="beanId")
 * 所指定的bean作用于形参
 *
 */



//@Controller(value="aaa")
@Controller("aaa")
public class UserController {

	@Autowired
	private UserService userService;
	
	public void addUser() {
		userService.addUser();
	}
	
	public UserController() {
		System.out.println("UserController");
	}
	
}
六月 12, 2019 7:15:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4437c4: startup date [Wed Jun 12 19:15:16 CST 2019]; root of context hierarchy
六月 12, 2019 7:15:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [user.xml]
UserController
UserServiceImpl
UserDaoImpl
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aaa': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.atguigu.ioc.userMod.service.UserService com.atguigu.ioc.userMod.controller.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.atguigu.ioc.userMod.dao.UserDao com.atguigu.ioc.userMod.service.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.atguigu.ioc.userMod.dao.UserDao] is defined: expected single matching bean but found 2: userDaoImpl,userDaoMybatisImpl
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.atguigu.ioc.userMod.Test.main(Test.java:16)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.atguigu.ioc.userMod.service.UserService com.atguigu.ioc.userMod.controller.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.atguigu.ioc.userMod.dao.UserDao com.atguigu.ioc.userMod.service.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.atguigu.ioc.userMod.dao.UserDao] is defined: expected single matching bean but found 2: userDaoImpl,userDaoMybatisImpl
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
	... 13 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.atguigu.ioc.userMod.dao.UserDao com.atguigu.ioc.userMod.service.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.atguigu.ioc.userMod.dao.UserDao] is defined: expected single matching bean but found 2: userDaoImpl,userDaoMybatisImpl
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
	... 15 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.atguigu.ioc.userMod.dao.UserDao com.atguigu.ioc.userMod.service.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.atguigu.ioc.userMod.dao.UserDao] is defined: expected single matching bean but found 2: userDaoImpl,userDaoMybatisImpl
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
	... 26 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.atguigu.ioc.userMod.dao.UserDao] is defined: expected single matching bean but found 2: userDaoImpl,userDaoMybatisImpl
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
	... 28 more

多个实现类,都被spring管理,并不能区分要使用哪一个,会报这个错误

 

© 著作权归作者所有

上一篇: 链表图解
下一篇: spring框架
architect刘源源

architect刘源源

粉丝 169
博文 564
码字总数 941209
作品 0
浦东
程序员
私信 提问
Spring Cloud Greenwich.RC1 发布

Spring Cloud Greenwich 首个 RC 版发布了,这个里程碑版本与 Spring Boot 2.1.1.RELEASE 兼容,各个项目都进行了更新,以实现与 Java 11 的兼容性。可点此查看在该版本中,分配给这个版本的...

局长
2018/12/17
1K
1
Spring Cloud Greenwich.M3 版本发布

Spring Cloud Function 的第三个 Milestone 3 已发布,该版本的各个模块现可在 Spring Milestone 存储库中使用。 主要更改 此版本与 Spring Boot 2.1.0.RELEASE 兼容,并已对 Java 11 兼容性...

淡漠悠然
2018/11/22
5.6K
0
Spring Boot的启动器Starter详解

Spring Boot的启动器Starter详解 Spring Boot应用启动器基本的一共有44种,具体如下: 1)spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。 2)spring-boot...

anlve
2018/02/22
176
0
Spring Cloud Finchley SR4 正式发布

Spring Cloud Finchley 的 Service Release 4 (SR4) 版本已发布,本次更新主要是对其包含的一些模块进行了升级,查看发布说明以了解更多信息,可从 Maven 中央仓库获取源码。Spring Cloud Fi...

xplanet
06/15
2.3K
1
spring-boot-plus 1.1.0 发布,集成 Spring Boot Admin 管理和监控应用

spring-boot-plus1.1.0.发布 集成Spring Boot Admin管理和监控应用 Spring Boot Admin用来管理和监控Spring Boot应用程序 应用程序向我们的Spring Boot Admin Client注册(通过HTTP)或使用S...

geekidea
08/01
4.3K
9

没有更多内容

加载失败,请刷新页面

加载更多

最简单的获取相机拍照的图片

  import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import andr......

MrLins
今天
6
0
说好不哭!数据可视化深度干货,前端开发下一个涨薪点在这里~

随着互联网在各行各业的影响不断深入,数据规模越来越大,各企业也越来越重视数据的价值。作为一家专业的数据智能公司,个推从消息推送服务起家,经过多年的持续耕耘,积累沉淀了海量数据,在...

个推
今天
9
0
第三方支付-返回与回调注意事项

不管是支付宝,微信,还是其它第三方支付,第四方支付,支付机构服务商只要涉及到钱的交易都要进行如下校验,全部成功了才视为成功订单 1.http请求是否成功 2.校验商户号 3.校验订单号及状态...

Shingfi
今天
5
0
简述Java内存分配和回收策略以及Minor GC 和 Major GC(Full GC)

内存分配: 1. 栈区:栈可分为Java虚拟机和本地方法栈 2. 堆区:堆被所有线程共享,在虚拟机启动时创建,是唯一的目的是存放对象实例,是gc的主要区域。通常可分为两个区块年轻代和年老代。更...

DustinChan
今天
7
0
Excel插入批注:可在批注插入文字、形状、图片

1.批注一直显示:审阅选项卡-------->勾选显示批注选项: 2.插入批注快捷键:Shift+F2 组合键 3.在批注中插入图片:鼠标右键点击批注框的小圆点【重点不可以在批注文本框内点击】----->调出批...

东方墨天
今天
7
1

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部