Spring实战——通过Java代码装配bean

  上篇说的是无需半行xml配置完成bean的自动化注入。这篇仍然不要任何xml配置,通过Java代码也能达到同样的效果。

  这么说,是要把上篇的料拿出来再煮一遍? 当然不是,上篇我们几乎都在用注解的方式如@ComponentScan @Component等就完成了自动化注入,但是这些注解不是无所不能的,有些地方它也是望尘莫及的,比如第三方类库,你总不能跑到人家jar包或者库里面悄悄的加上这些注解再伪装成原来的样子用吧,太丑了! 所以,Spring可以通过显示配置的方式来解决,第一种前面有介绍过,就是通过xml来显示声明bean,第二种就是这里要介绍的基于Java代码方式装配bean。

 

  基于注解的自动化注入固然优雅,但是它也有鞭长莫及的时候,这时候就来看看Java代码如何装配bean的。

  还记的上篇的配置类CDPlayerConfig是长这样的

@Configuration
@ComponentScan
public class CDPlayerConfig { 
}

  有了这个万能的组件扫描注解,一切都是那么只能,只需要在bean类上加上如@Component注解,Spring就会自动为该类创建相应的bean类。

  但是现在因为我们有一些第三方类库,我们没办法去深入类中加上这些标记了,所以@ComponentScan就失去了威力和意义。

 

  这篇的CDPlayerConfig应该长这样

@Configuration
public class CDPlayerConfig { 
}

  @Configuration注解表示该类是一个配置类。显然要创建的bean的信息是要放到这个类中的。

  基于其他类以及类中的方法不变比如CompactDisc, CDPlayer, SgtPeppers等。编写测试方法如下

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {@Rulepublic final StandardOutputStreamLog log = new StandardOutputStreamLog();@Autowiredprivate MediaPlayer player;@Autowiredprivate CompactDisc cd;@Testpublic void cdShouldNotBeNull() {assertNotNull(cd);}@Testpublic void play() {player.play();assertEquals("Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles\r\n",log.getLog());}}

 

  首先从代码来看就会出现如下的注入错误

 

  这里显示MediaPlayer无法注入。同时运行程序得到结果如下

Testing started at 0:20 ...
0:20:03: Executing external tasks 'cleanTest test'...
:cleanTest
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
一月 15, 2017 12:20:06 上午 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: 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]
一月 15, 2017 12:20:06 上午 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: 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]
一月 15, 2017 12:20:06 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@6adf0b01: startup date [Sun Jan 15 00:20:06 CST 2017]; root of context hierarchy
一月 15, 2017 12:20:06 上午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@61c715e8] to prepare test instance [soundsystem.CDPlayerTest@1081b08c]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soundsystem.CDPlayerTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private soundsystem.MediaPlayer soundsystem.CDPlayerTest.player; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [soundsystem.MediaPlayer] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:293)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:384)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:105)at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:56)at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private soundsystem.MediaPlayer soundsystem.CDPlayerTest.player; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [soundsystem.MediaPlayer] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:509)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)... 46 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [soundsystem.MediaPlayer] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:481)... 48 moreError creating bean with name 'soundsystem.CDPlayerTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private soundsystem.MediaPlayer soundsystem.CDPlayerTest.player; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [soundsystem.MediaPlayer] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soundsystem.CDPlayerTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private soundsystem.MediaPlayer soundsystem.CDPlayerTest.player; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [soundsystem.MediaPlayer] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:293)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:384)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:105)at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:56)at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private soundsystem.MediaPlayer soundsystem.CDPlayerTest.player; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [soundsystem.MediaPlayer] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:509)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)... 46 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [soundsystem.MediaPlayer] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:481)... 48 moresoundsystem.CDPlayerTest > play FAILEDorg.springframework.beans.factory.BeanCreationExceptionCaused by: org.springframework.beans.factory.BeanCreationExceptionCaused by: org.springframework.beans.factory.NoSuchBeanDefinitionException
1 test completed, 1 failed
:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///C:/Users/Administrator/Desktop/spring_video/SpringiA4_SourceCode/Chapter_02/stereo-javaconfig/build/reports/tests/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.59 secs
There were failing tests. See the report at: file:///C:/Users/Administrator/Desktop/spring_video/SpringiA4_SourceCode/Chapter_02/stereo-javaconfig/build/reports/tests/index.html

  究其原因,主要是既没有在xml中没有声明相应的bean,也没有添加@ComponentScan启动自动扫描组件机制。

  所以这里还是需要在CDPlayerConfig配置类中做文章。

 

  声明bean

  通过注解@Bean声明一个bean对象。

@Beanpublic CompactDisc compactDisc() {return new SgtPeppers();}

  该声明好比在xml中添加

<bean id="sgtPeppers" class="soundsystem.SgtPeppers"/>

  通过这种方式,Spring会默认为SgtPeppers创建一个名为sgtPeppers的bean,如果你想换个名字,只需要在@Bean后面加上类似(name="lonelyHeartClubBand")这样的属性即可。

 

  如果你的野心远不止满足于通过Java代码创建一个bean的话,其实你还试试如何在CDPlayer中播放CompactDisc,那就需要在CDPlayer中注入CompactDisc的bean了。于是我们可以这样声明

@Beanpublic CDPlayer cdPlayer(CompactDisc compactDisc) {return new CDPlayer(compactDisc);}

  加上@Bean注解,表明cdPlayer()方法会创建一个bean实例并将其注册到Spring的应用上下文中,显然bean的名字默认就是cdPlayer。在执行这个方法的时候,Spring会拦截所有对方法的调用,当然这里会传入Spring注册好的CompactDisc的实例bean给cdPlayer方法来确保返回的是创建好的cdPlayer的bean。注意这里的bean是单例的,其实在xml中配置的bean如果没有特殊声明,默认也是单例的。也就是说即使这里创建了多个类似cdPlayer的方法,得到的仍然是同一个cdPlayer的bean。

 

  这时候运行测试类CDPlayerTest

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {@Rulepublic final StandardOutputStreamLog log = new StandardOutputStreamLog();@Autowiredprivate MediaPlayer player;@Testpublic void play() {player.play();assertEquals("Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles\r\n",log.getLog());}}

 

  

 

  测试通过。

  表明CDPlayer注入成功。前面通过Java代码注册的Bean有效。

 

如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!如果您想持续关注我的文章,请扫描二维码,关注JackieZheng的微信公众号,我会将我的文章推送给您,并和您一起分享我日常阅读过的优质文章。

转载于:https://www.cnblogs.com/bigdataZJ/p/SpringInAction2.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541929.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java.io和util的区别_Java NIO与IO的区别和比较

Java NIO与IO的区别和比较导读J2SE1.4以上版本中发布了全新的I/O类库。本文将通过一些实例来简单介绍NIO库提供的一些新特性&#xff1a;非阻塞I/O&#xff0c;字符转换&#xff0c;缓冲以及通道。一. 介绍NIONIO包(java.nio.*)引入了四个关键的抽象数据类型&#xff0c;它们共…

区块链+税务的思考

2016年&#xff0c;区块链技术火了&#xff01;各大金融公司、互联网巨头都竞相参加到区块链技术的研究中。我们公司的业务是税务的信息化领域&#xff0c;也希望通过区块链技术的应用&#xff0c;来提升为财税领域的服务。 区块链技术优缺点总结 下图是对区块链技术的一些特点…

如何使用JavaScript删除CSS属性?

In this article, well see how we can remove a CSS property from a certain element using JavaScript? We can remove only those properties that we assign ourselves and the pre-default ones cannot be removed by this method. 在本文中&#xff0c;我们将看到如何使…

【喜报】JEEWX荣获“2016 年度码云新增热门开源软件排行榜”第一名!

为什么80%的码农都做不了架构师&#xff1f;>>> 2016 年度码云新增项目排行榜 TOP 50 正式出炉&#xff01;根据 2016 年在码云上新增开源项目的 Watch、Star、Fork 数量以及其他角度的统计&#xff0c;JEEWX捷微管家荣获“2016 年度码云新增热门开源软件排行榜”第…

java 二叉树特点_疯狂java笔记之树和二叉树

树的概述树是一种非常常用的数据结构&#xff0c;树与前面介绍的线性表&#xff0c;栈&#xff0c;队列等线性结构不同&#xff0c;树是一种非线性结构1.树的定义和基本术语计算机世界里的树&#xff0c;是从自然界中实际的树抽象而来的&#xff0c;它指的是N个有父子关系的节点…

tomcat +apache 配置集群

2019独角兽企业重金招聘Python工程师标准>>> APACHE2.2.25TOMCAT6.0.37配置负载均衡 目标: 使用 apache 和 tomcat 配置一个可以应用的 web 网站&#xff0c;要达到以下要求&#xff1a; 1. Apache 做为 HttpServer &#xff0c;后面连接多个 tomcat 应用实例&…

java双缓存机制_详解JVM类加载机制及类缓存问题的处理方法

前言大家应该都知道&#xff0c;当一个Java项目启动的时候&#xff0c;JVM会找到main方法&#xff0c;根据对象之间的调用来对class文件和所引用的jar包中的class文件进行加载(其步骤分为加载、验证、准备、解析、初始化、使用和卸载)&#xff0c;方法区中开辟内存来存储类的运…

什么是mvc?

什么是MVCMVC 是一种设计模式&#xff0c;它将应用划分为3 个部分&#xff1a;数据&#xff08;模型&#xff09;、展现层&#xff08;视图&#xff09;和用户交互层&#xff08;控制器&#xff09;。换句话说&#xff0c;一个事件的发生是这样的过程&#xff1a;1&#xff0e;…

mysql的安装和基本命令_MySQL安装以及简单命令用法

MYSQL:关系型数据库存储引擎:负责将逻辑层的概念转化为物理层机制&#xff0c;在物理层完成物理机制。支持事务&#xff1a;transaction必须满足的条件&#xff1a;ACID(一致性,持久性,原子性,隔离性)锁&#xff1a;并发访问随机访问&#xff1a;数据在磁盘上是随机存储的安装&…

docker集群运行在calico网络上

2019独角兽企业重金招聘Python工程师标准>>> ##网络及版本信息 docker1 centos7 192.168.75.200 docker2 centos7 192.168.75.201 物理网络 192.168.75.1/24 Docker version 1.10.3, build 3999ccb-unsupported &#xff0c;安装过程略 # calicoctl version Version…

人脸识别python face_recognize_python2.7使用face_recognition做人脸识别

偶然看到一篇文章&#xff0c;说是可以实时人脸识别&#xff0c;很有兴趣就自己按照文章开始动手人脸识别&#xff0c;但是实现过程中遇到了几个问题这里做个总结&#xff0c;希望可以帮助到大家安装face_recognition这个之前需要先安装编译dlib&#xff0c;如果没有安装dlib&a…

jvm延迟偏向_用于偏向硬币翻转模拟的Python程序

jvm延迟偏向Here, we will be simulating the occurrence coin face i.e. H - HEAD, T - TAIL. Simply we are going to use an inbuilt library called as random to call a random value from given set and thereby we can stimulate the occurrence value by storing the o…

opengl es的射线拾取

2019独角兽企业重金招聘Python工程师标准>>> 在opengl中关于拾取有封装好的选择模式&#xff0c;名字栈&#xff0c;命中记录&#xff0c;实现拾取的功能&#xff0c;相对容易一些。但是到了opengl es里面就比较倒霉了&#xff0c;因为opengl es是opengl的简化版&am…

视觉学习(4) —— 添加地址传递数据

Modbus Slave 选择一个地址右键&#xff0c;选择发送的数据类型 视觉软件 一、添加地址 当地址为100时&#xff0c;先将首地址改为100&#xff0c;第0个地址为100&#xff0c;第1个地址为101&#xff0c;往后累加 若想使用100—150的地址&#xff0c;即首地址为100&#xff…

jquery中阻止事件冒泡的方法

2019独角兽企业重金招聘Python工程师标准>>> 根据《jquery基础教程》 第一种方法&#xff1a;判断事件的“直接”目标是否是自身&#xff0c;如果不是自身&#xff0c;不予处理 $(div.outter).click(function(event) {if (event.target this) {$(p).css(color, red…

java swing 组织机构_课内资源 - 基于Java Swing的小型社团成员管理系统

一、需求分析1.1 个人信息学号、姓名、性别、年级、系别、专业、出生日期、联系方式、个性签名、地址、照片。1.2 基本功能要求管理员信息管理登录、注销功能修改密码功能部落成员信息管理添加成员删除成员修改成员信息按条件查找筛选成员1.3 高级特性管理员权限管理成员信息包…

centos下安装pip时失败:

2019独角兽企业重金招聘Python工程师标准>>> [rootwfm ~]# yum -y install pip Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.tuna.tsinghua…

java写手机游戏_如何将自己编写的JAVA小游戏写到手机里?

2019-06-19怎么用java编写获取星期几的程序&#xff1f;import java。util。*; public class WeekDay { Calendar date Calendar。getInstance(); private int getMaxDate(int moth){ moth moth -1; if(moth > 12 || moth < 0){ System。 out。println("输入月份错…