Back to Javatutorial

SpringBean的定义与管理(核心)

docs/Spring全家桶/Spring/SpringBean的定义与管理(核心).md

1.0.06.2 KB
Original Source

ע

SpringܵĺĹ

  • SpringΪ󹤳𴴽еJavaЩJava󱻳ΪBean
  • SpringBean֮ϵSpringʹһֱΪ"ע"ķʽBean֮ϵ

ʹע룬ΪBeanעֵͨעBeanáעһĽʽBeanļ֯һ𣬶Ӳķʽһ

ע

Rod Johnsonǵһ߶ļJavaʵЭϵˣַʽһ֣ƷתInverse of ControlIoCMartine FowlerΪַʽһƣע루Dependency Injection˲ע룬ǿƷת京ȫͬijJava󣨵ߣҪһJava󣨱󣩵ķʱڴͳģʽͨ

  1. ԭʼ: ****Ȼٵñķ
  2. 򵥹ģʽ: ҵĹȻ****ͨȥȡٵñķ

ע֣Ȼᵼµ뱻ʵӲϣdzĿάʹSpring֮󣬵ȡ󣬵ֻҪ****SpringΪߵijԱֵɣɴ˿ɼʹSpring󣬵߻ȡķʽԭȡ˱ܡRod Johnson֮ΪƷת

SpringĽǶSpring𽫱ֵߵijԱ൱ΪעʵMartine Fowler֮Ϊע롣

ֵע

ֵעָIoCͨԱsetterע뱻ע뷽ʽ򵥡ֱۣSpringעʹá

ע

ùϵķʽΪע롣ͨ˵SpringڵײԷ䷽ʽִдָĹִдĹʱͿùԳԱִгʼǹעıʡ

ע뷽ʽĶԱ

ֵעŵ㣺

  • 봫ͳJavaBeanдƣ򿪷Ա⡢ܡͨsetter趨ϵԵøֱۡȻ
  • ڸӵϵùע룬ᵼ¹ӷףĶSpringڴBeanʵʱҪͬʱʵȫʵ½ʹֵע룬ܱЩ⡣
  • ijЩԱѡ£Ĺӱء

ע£

  • עڹоϵע˳ע롣
  • ϵ仯BeanעôΪûsetterеϵȫڹ趨뵣ĺĴϵƻ
  • ϵֻڹ趨ֻĴ߲ܸıϵĵ߶ԣڲϵȫ͸ϸھ۵ԭ

ע⣺ ֵעΪעΪעԡϵ仯ע룬ùע룻ϵע룬Dzֵע롣

SpringеBean

ڿ˵ʹSpringҪ£ٿBeanBeanSpring˵ҪľǸļBeanʵBeanʵķ"ע"νIoCıʡ

Bean

ͨSpringһBeanʵʱBeanʵʵΪBeanָضSpring֧

  1. singleton: ģʽSpring IoCУsingletonBeanֻһʵ
  2. prototype: ÿͨgetBean()ȡprototypeBeanʱһµBeanʵ
  3. request: һHTTPrequestBeanֻһʵζţͬһHTTPڣÿBeanõͬһʵֻWebӦʹSpringʱЧ
  4. session bean ĶΪ HTTP Ự ֻweb-aware Spring ApplicationContextЧ
  5. global session: ÿȫֵHTTP SessionӦһBeanʵڵ͵£ʹportlet contextʱЧֻͬWebӦЧ

ָBeanSpringĬʹsingletonprototypeBeanĴٴ۱Ƚϴ󡣶singletonBeanʵһɹͿظʹáˣӦþ⽫Beanóprototype

ʹԶװעBean

SpringԶװBeanBean֮ϵʹrefʽָBeanSpringXMLļݣijֹΪBeanע뱻Bean SpringԶװͨ<beans/>Ԫصdefault-autowireָԶļеBeanãҲͨ<bean/>ԪصautowireָֻԸBeaná

autowire``default-autowireԽֵ

  • no: ʹԶװ䡣BeanͨrefԪض塣ĬãڽϴIJ𻷾вıãʽúܹõϵ
  • byName: setterԶװ䡣SpringȫBeanҳidsetterȥsetǰ׺СдĸͬBeanע롣ûҵƥBeanʵSpringκע롣
  • byType: setterβԶװ䡣SpringеȫBeanһBeansetterβƥ䣬ԶעBeanҵBean׳һ쳣ûҵBeanʲôᷢsetterᱻá
  • constructor: byTypeƣԶƥ乹IJǡҵһ빹ƥBean׳һ쳣
  • autodetect: SpringBeanڲṹоʹconstructorbyTypeԡҵһĬϵĹ캯ôͻӦbyTypeԡ

һBeanʹԶװʹrefʽָʱʽָԶװڴ͵ӦãʹԶװ䡣ȻʹԶװɼļĹϵԺ͸ԡϵװԴļͣBeanBean֮Ͻ͵Σڸ߲ν

<!--ͨÿԽBeanųԶװ֮--> <bean id="" autowire-candidate="false"/> <!--֮⣬beansԪָ֧ģʽַabcβBeanųԶװ֮--> <beans default-autowire-candidates="*abc"/>

Bean3ַʽ

ʹùBeanʵ

ʹùBeanʵùע룬SpringײBean޲ʵҪBeanṩ޲Ĺ

ĬϵĹBeanʵSpringBeanʵִĬϳʼеĻ͵ֵʼΪ0falseе͵ֵʼΪnull

ʹþ̬Bean

ʹþ̬BeanʵʱclassҲָʱclassԲָBeanʵʵ࣬Ǿ̬࣬Spring֪ͨĸBeanʵ

֮⣬Ҫʹfactory-methodָ̬Springþ̬һBeanʵһָBeanʵSpringĴͨBeanʵȫһ̬Ҫʹ<constructor-arg.../>Ԫָ̬IJ

ʵBean

ʵ뾲ֻ̬һͬþֻ̬ʹù༴ɣʵҪʵʹʵʱBeanʵ<bean.../>Ԫclassԣʵʹfactory-beanָʵ ʵBean<bean.../>ԪʱҪָԣ

  • factory-bean: ԵֵΪBeanid
  • factory-method: ָʵĹ

ʵʱҪʹ<constructor-arg.../>Ԫȷֵ

ЭͬBean

singletonBeanprototypeBeanʱͬԭΪSpringʼʱԤʼеsingleton Bean``singleton Bean``prototype BeanSpringڳʼsingleton Bean֮ǰȴprototypeBeanȻŴsingleton Beanprototype Beanעsingleton Bean ͬķ֣

  • ע: singletonBeanÿҪprototypeBeanʱµBeanʵɱ֤ÿעprototype Beanʵµʵ
  • ÷ע: עͨʹlookupע룬ʹlookupעSpringдBeanij巽زBeanĽҵBeanͨһnon-singleton BeanSpringͨʹJDK̬cglib޸Ŀͻ˵Ķ룬ӶʵҪ

õڶַʹ÷ע롣Ϊʹlookupע룬Ҫ

  1. BeanʵඨΪ࣬һ󷽷ȡBean
  2. <bean.../>Ԫ<lookup-method.../>ԪSpringΪBeanʵʵָij󷽷

ע⣺

Springʱ̬ǿķʽʵ<lookup-method.../>Ԫָij󷽷ĿʵֹӿڣSpringJDK̬ʵָó࣬Ϊ֮ʵֳ󷽷ĿûʵֹӿڣSpringcglibʵָó࣬Ϊ֮ʵֳ󷽷Spring4.0spring-core-xxx.jarѾcglib⡣

ֺ

Springṩֳõĺ

  • Bean: ֺBeanкBeanжǿ
  • : ֺIoCкǿܡ

Bean

BeanһBeanBeanṩidԣҪеBeanִкΪеĿBeanɴȣBeanΪBeanBeanBeanʵɹ֮󣬶BeanʵнһǿBeanʵBeanPostProcessorӿڣͬʱʵָýӿڵ

  1. Object postProcessBeforeInitialization(Object bean, String name) throws BeansException: ÷ĵһϵͳкBeanʵڶǸBeanid
  2. Object postProcessAfterinitialization(Object bean, String name) throws BeansException: ÷ĵһϵͳкBeanʵڶǸBeanid

һעBeanBeanͻԶÿBeanʱԶBeanĻصʱͼ

עһ㣬ʹBeanFactoryΪSpringֶעBeanȡBeanʵȻֶעᡣ

BeanPostProcessor bp = (BeanPostProcessor)beanFactory.getBean("bp"); beanFactory.addBeanPostProcessor(bp); Person p = (Person)beanFactory.getBean("person");

BeanеBeanʵʵBeanFactoryPostProcessorӿڣʵָýӿڵһpostProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)ʵָ÷ķǶSpringеĴִԶSpringԶչȻҲԶSpringκδ

BeanPostProcessor``ApplicationContextԶ⵽еԶעʹBeanFactoryΪSpringֶøBeanFactory

Spring""֧

Bean

Springṩ¼AnnotationעSpring Bean

  • @Component: עһͨSpring Bean
  • @Controller: עһ
  • @Service: עһҵ߼
  • @Repository: עһDAO

SpringļãָԶɨİ

<context:component-scan base-package="edu.shu.spring.domain"/>

ʹ@Resource

@Resourceλjavax.annotation£JavaEE淶һAnnotationSpringֱӽ˸AnnotationͨʹøAnnotationΪĿBeanָЭBeanʹ@Resource``<property.../>ԪصrefͬЧ @ResourcesetterҲֱʵʹ@ResourceʵӼ򵥣ʱSpringֱʹJavaEE淶Fieldע룬ʱsetterԲҪ

ʹ@PostConstruct@PreDestroyΪ

@PostConstruct``@PreDestroyͬλjavax.annotation£ҲJavaEE淶AnnotationSpringֱӽǣڶSpringBeanΪǶηκԡǰεķʱBeanijʼεķʱBean֮ǰķ

Spring4.0ǿԶװ;ȷװ

Springṩ@AutowiredעָԶװ䣬@Autowiredsetterͨʵ͹ȡʹ@AutowiredעsetterʱĬϲbyTypeԶװԡֲ£Զװ͵ĺѡBeanʵжʱͿ쳣Ϊʵ־ȷԶװ䣬Springṩ@Qualifierע⣬ͨʹ@QualifierBeanidִԶװ䡣

ο

https://www.w3cschool.cn/wkspring https://www.runoob.com/w3cnote/basic-knowledge-summary-of-spring.html http://codepub.cn/2015/06/21/Basic-knowledge-summary-of-Spring https://dunwu.github.io/spring-tutorial https://mszlu.com/java/spring http://c.biancheng.net/spring/aop-module.html