spring学习24-:在javaconfig中配置bean对象

简介: spring学习24-:在javaconfig中配置bean对象

image.png

Appconfig

package com.geyao.demo.config;
     import com.geyao.demo.dao.UserDao;
     import com.geyao.demo.dao.impl.UserDaoNormal;
     import org.springframework.context.annotation.Bean;
     import org.springframework.context.annotation.ComponentScan;
     import org.springframework.context.annotation.Configuration;
     @Configuration
     public class Appconfig {
         @Bean
         public UserDao userDaoNormal(){
             System.out.println("创建UserDao对象"
             );
             return  new UserDaoNormal();
         }
     }UserNormal类
    package com.geyao.demo.dao.impl;
     import com.geyao.demo.dao.UserDao;
     import org.springframework.stereotype.Repository;
     public class UserDaoNormal implements UserDao {
         public void add(){
             System.out.println("添加到数据库中...");
         }
     }UserDao类
    package com.geyao.demo.dao;
     public interface UserDao {
         void add();
     }UserDaoTest类
    package com.geyao.demo.dao;
     import com.geyao.demo.config.Appconfig;
     import org.junit.Test;
     import org.junit.runner.RunWith;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.test.context.ContextConfiguration;
     import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     @RunWith(SpringJUnit4ClassRunner.class)
     @ContextConfiguration(classes = Appconfig.class)
     public class UserDaoTest {
         @Autowired
         private UserDao userDao;
         @Test
         public void testAdd(){
             userDao.add();
         }
     }运行结果
    [INFO ] 2019-10-31 20:52:25,614 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.getDefaultTestExecutionListenerClassNames(AbstractTestContextBootstrapper.java:260)
     Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
     [INFO ] 2019-10-31 20:52:25,625 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209)
     Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
     [INFO ] 2019-10-31 20:52:25,629 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209)
     Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
     [INFO ] 2019-10-31 20:52:25,631 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.instantiateListeners(AbstractTestContextBootstrapper.java:209)
     Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
     [INFO ] 2019-10-31 20:52:25,632 method:org.springframework.test.context.support.AbstractTestContextBootstrapper.getTestExecutionListeners(AbstractTestContextBootstrapper.java:187)
     Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@67b467e9, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@47db50c5, org.springframework.test.context.support.DirtiesContextTestExecutionListener@5c072e3f]添加到数据库中...
     [INFO ] 2019-10-31 20:52:25,819 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583)
     Refreshing org.springframework.context.support.GenericApplicationContext@6c3f5566: startup date [Thu Oct 31 20:52:25 CST 2019]; root of context hierarchy
     [DEBUG] 2019-10-31 20:52:25,846 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:25,847 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:25,905 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
     [DEBUG] 2019-10-31 20:52:25,910 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
     WARNING: An illegal reflective access operation has occurred
     WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/geyao/.m2/repository/org/springframework/spring-core/4.3.13.RELEASE/spring-core-4.3.13.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
     WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
     WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
     WARNING: All illegal access operations will be denied in a future release
     [DEBUG] 2019-10-31 20:52:26,093 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,094 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,095 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
     [DEBUG] 2019-10-31 20:52:26,122 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,122 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,124 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,128 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
     [DEBUG] 2019-10-31 20:52:26,134 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,142 method:org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:730)
     Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5fdcaa40: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appconfig,userDaoNormal]; root of factory hierarchy
     [DEBUG] 2019-10-31 20:52:26,142 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,142 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,143 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
     [DEBUG] 2019-10-31 20:52:26,143 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
     [DEBUG] 2019-10-31 20:52:26,143 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
     [DEBUG] 2019-10-31 20:52:26,149 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular references
     [DEBUG] 2019-10-31 20:52:26,152 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
     [DEBUG] 2019-10-31 20:52:26,153 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
     [DEBUG] 2019-10-31 20:52:26,153 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
     [DEBUG] 2019-10-31 20:52:26,154 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular references
     [DEBUG] 2019-10-31 20:52:26,171 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
     [DEBUG] 2019-10-31 20:52:26,171 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'appconfig'
     [DEBUG] 2019-10-31 20:52:26,172 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'appconfig'
     [DEBUG] 2019-10-31 20:52:26,173 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'appconfig' to allow for resolving potential circular references
     [DEBUG] 2019-10-31 20:52:26,177 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'appconfig'
     [DEBUG] 2019-10-31 20:52:26,177 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'userDaoNormal'
     [DEBUG] 2019-10-31 20:52:26,177 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'userDaoNormal'
     [DEBUG] 2019-10-31 20:52:26,180 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'appconfig'
     创建UserDao对象
     [DEBUG] 2019-10-31 20:52:26,219 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'userDaoNormal' to allow for resolving potential circular references
     [DEBUG] 2019-10-31 20:52:26,222 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'userDaoNormal'
     [DEBUG] 2019-10-31 20:52:26,222 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
     [DEBUG] 2019-10-31 20:52:26,285 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'lifecycleProcessor'
     [DEBUG] 2019-10-31 20:52:26,294 method:org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:86)
     Processing injected element of bean 'com.geyao.demo.dao.UserDaoTest': AutowiredFieldElement for private com.geyao.demo.dao.UserDao com.geyao.demo.dao.UserDaoTest.userDao
     [DEBUG] 2019-10-31 20:52:26,297 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'userDaoNormal'
     [DEBUG] 2019-10-31 20:52:26,298 method:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.registerDependentBeans(AutowiredAnnotationBeanPostProcessor.java:535)
     Autowiring by type from bean name 'com.geyao.demo.dao.UserDaoTest' to bean named 'userDaoNormal'
     [INFO ] 2019-10-31 20:52:26,308 method:org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:984)
     Closing org.springframework.context.support.GenericApplicationContext@6c3f5566: startup date [Thu Oct 31 20:52:25 CST 2019]; root of context hierarchy
     [DEBUG] 2019-10-31 20:52:26,309 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'lifecycleProcessor'
     [DEBUG] 2019-10-31 20:52:26,310 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:512)


相关文章
|
24天前
|
Java 开发者 微服务
手写模拟Spring Boot自动配置功能
【11月更文挑战第19天】随着微服务架构的兴起,Spring Boot作为一种快速开发框架,因其简化了Spring应用的初始搭建和开发过程,受到了广大开发者的青睐。自动配置作为Spring Boot的核心特性之一,大大减少了手动配置的工作量,提高了开发效率。
46 0
|
2月前
|
Java API 数据库
Spring Boot框架因其简洁的配置、快速的启动特性及丰富的功能集而备受开发者青睐
本文通过在线图书管理系统案例,详细介绍如何使用Spring Boot构建RESTful API。从项目基础环境搭建、实体类与数据访问层定义,到业务逻辑实现和控制器编写,逐步展示了Spring Boot的简洁配置和强大功能。最后,通过Postman测试API,并介绍了如何添加安全性和异常处理,确保API的稳定性和安全性。
41 0
|
2月前
|
Java API Spring
在 Spring 配置文件中配置 Filter 的步骤
【10月更文挑战第21天】在 Spring 配置文件中配置 Filter 是实现请求过滤的重要手段。通过合理的配置,可以灵活地对请求进行处理,满足各种应用需求。还可以根据具体的项目要求和实际情况,进一步深入研究和优化 Filter 的配置,以提高应用的性能和安全性。
|
1月前
|
缓存 Java Spring
实战指南:四种调整 Spring Bean 初始化顺序的方案
本文探讨了如何调整 Spring Boot 中 Bean 的初始化顺序,以满足业务需求。文章通过四种方案进行了详细分析: 1. **方案一 (@Order)**:通过 `@Order` 注解设置 Bean 的初始化顺序,但发现 `@PostConstruct` 会影响顺序。 2. **方案二 (SmartInitializingSingleton)**:在所有单例 Bean 初始化后执行额外的初始化工作,但无法精确控制特定 Bean 的顺序。 3. **方案三 (@DependsOn)**:通过 `@DependsOn` 注解指定 Bean 之间的依赖关系,成功实现顺序控制,但耦合性较高。
实战指南:四种调整 Spring Bean 初始化顺序的方案
|
7天前
|
安全 Java 开发者
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
13 1
|
29天前
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
53 9
|
1月前
|
Java Spring
[Spring]aop的配置与使用
本文介绍了AOP(面向切面编程)的基本概念和核心思想。AOP是Spring框架的核心功能之一,通过动态代理在不修改原代码的情况下注入新功能。文章详细解释了连接点、切入点、通知、切面等关键概念,并列举了前置通知、后置通知、最终通知、异常通知和环绕通知五种通知类型。
30 1
|
Java API Spring
Spring学习路径
Spring作为一个优秀的开源企业级框架有着一下特点 开源框架 简化企业级应用开发的流程 Spring是一个JavaSE/EE的一站式框架 优点在于 方便解耦 AOP的编程支持 声明式事务的支持 可以引入jUnit4,方便程序测试 对优秀开源框架的支持,方便集成 降低JavaEE API的使用难度.
2515 0
|
3月前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
2月前
|
人工智能 自然语言处理 前端开发
SpringBoot + 通义千问 + 自定义React组件:支持EventStream数据解析的技术实践
【10月更文挑战第7天】在现代Web开发中,集成多种技术栈以实现复杂的功能需求已成为常态。本文将详细介绍如何使用SpringBoot作为后端框架,结合阿里巴巴的通义千问(一个强大的自然语言处理服务),并通过自定义React组件来支持服务器发送事件(SSE, Server-Sent Events)的EventStream数据解析。这一组合不仅能够实现高效的实时通信,还能利用AI技术提升用户体验。
193 2