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)