docs/Spring全家桶/Spring/SpringBean的定义与管理(核心).md
SpringܵĺĹ
ʹע룬ΪBeanעֵͨעBeanáעһĽʽBeanļ֯һ𣬶Ӳķʽһ
Rod Johnsonǵһ߶ļJavaʵЭϵˣַʽһ֣ƷתInverse of ControlIoCMartine FowlerΪַʽһƣע루Dependency Injection˲ע룬ǿƷת京ȫͬijJavaߣҪһJavaķʱڴͳģʽͨ
ע֣Ȼᵼµ뱻ʵӲϣdzĿάʹSpring֮ȡֻҪ****SpringΪߵijԱֵɣɴ˿ɼʹSpringȡķʽԭȡ˱ܡRod Johnson֮ΪƷת
SpringĽǶSpringֵߵijԱ൱ΪעʵMartine Fowler֮Ϊע롣
ֵעָIoCͨԱsetterע뱻ע뷽ʽֱۣSpringעʹá
ùϵķʽΪע롣ͨ˵SpringڵײԷ䷽ʽִдָĹִдĹʱͿùԳԱִгʼǹעıʡ
ֵעŵ㣺
ע£
ע⣺ ֵעΪעΪעԡϵ仯ע룬ùע룻ϵע룬Dzֵע롣
ڿ˵ʹSpringҪ£ٿBeanBeanSpring˵ҪľǸļBeanʵBeanʵķ"ע"νIoCıʡ
ͨSpringһBeanʵʱBeanʵʵΪBeanָضSpring֧
ָBeanSpringĬʹsingletonprototypeBeanĴٴ۱ȽϴsingletonBeanʵһɹͿظʹáˣӦþ⽫Beanóprototype
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"/>ʹùBeanʵùע룬SpringײBeanʵҪBeanṩĹ
ĬϵĹBeanʵSpringBeanʵִĬϳʼеĻ͵ֵʼΪ0falseе͵ֵʼΪnull
ʹþ̬BeanʵʱclassҲָʱclassԲָBeanʵʵ࣬Ǿ̬࣬Spring֪ͨĸBeanʵ
֮⣬Ҫʹfactory-methodָ̬Springþ̬һBeanʵһָBeanʵSpringĴͨBeanʵȫһ̬Ҫʹ<constructor-arg.../>Ԫָ̬IJ
ʵ뾲ֻ̬һͬþֻ̬ʹù༴ɣʵҪʵʹʵʱBeanʵ<bean.../>Ԫclassԣʵʹfactory-beanָʵ
ʵBean<bean.../>ԪʱҪָԣ
ʵʱҪʹ<constructor-arg.../>Ԫȷֵ
singletonBeanprototypeBeanʱͬԭΪSpringʼʱԤʼеsingleton Bean``singleton Bean``prototype BeanSpringڳʼsingleton Bean֮ǰȴprototypeBeanȻŴsingleton Beanォprototype Beanעsingleton Bean
ͬķ֣
prototype Beanʵµʵnon-singleton BeanSpringͨʹJDK̬cglibĿͻ˵Ķ룬ӶʵҪõڶַʹ÷ע롣Ϊʹlookupע룬Ҫ
<bean.../>Ԫ<lookup-method.../>ԪSpringΪBeanʵʵָijע⣺
Springʱ̬ǿķʽʵ
<lookup-method.../>ԪָijĿʵֹӿڣSpringJDK̬ʵָó࣬Ϊ֮ʵֳĿûʵֹӿڣSpringcglibʵָó࣬Ϊ֮ʵֳSpring4.0spring-core-xxx.jarѾcglib⡣
Springṩֳõĺ
BeanһBeanBeanṩidԣҪеBeanִкΪеĿBeanɴȣBeanΪBeanBeanBeanʵɹ֮BeanʵнһǿBeanʵBeanPostProcessorӿڣͬʱʵָýӿڵ
Object postProcessBeforeInitialization(Object bean, String name) throws BeansException: ÷ĵһϵͳкBeanʵڶǸBeanidObject 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ṩ¼AnnotationעSpring Bean
@Component: עһͨSpring Bean@Controller: עһ@Service: עһҵ@Repository: עһDAOSpringļãָԶɨİ
<context:component-scan base-package="edu.shu.spring.domain"/>
@Resourceλjavax.annotation£JavaEE淶һAnnotationSpringֱӽ˸AnnotationͨʹøAnnotationΪĿBeanָЭBeanʹ@Resource``<property.../>ԪصrefͬЧ
@ResourcesetterҲֱʵʹ@ResourceʵӼʱSpringֱʹJavaEE淶Fieldע룬ʱsetterԲҪ
@PostConstruct``@PreDestroyͬλjavax.annotation£ҲJavaEE淶AnnotationSpringֱӽǣڶSpringBeanΪǶηκԡǰεķʱBeanijʼεķʱBean֮ǰķ
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