微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring开发_Annotation_注解

gallery/image/112614.html">http://www.cnblogs.com/hongten/gallery/image/112614.html

com.b510.app.test; java.util.Arrays; org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; SpringTest { main(String[] args) { ApplicationContext act = ClasspathXmlApplicationContext("beans.xml"); System.out.println(Arrays.toString(act.getBeanDeFinitionNames())); } }

com.b510.service; AnimalService { 获取相关信息 getInfo(); }

com.b510.service; MeatService { 方法whatMeat 一个字符串 String whatMeat(); }

添加注解,Spring会采用约定的方式来为这些Bean实例指定名称,这些Bean实例的名称认是Bean类的首字母小写,其他部分不变。如:catServiceBean

添加注解,相当于覆盖了Spring提供的认实例名称,所以Bean的实例名称为:cat

com.b510.service.impl; javax.annotation.postconstruct; javax.annotation.Resource; org.springframework.stereotype.Component; com.b510.service.AnimalService; com.b510.service.MeatService; @Component("cat") CatServiceBean AnimalService { age; MeatService meatService; CatServiceBean(){ System.out.println("猫类被初始化了"); } getAge() { age; } 方法:@postconstruct @postconstruct @Override getInfo() { System.out.println("我是猫,我的年龄是:"+age+",我很喜欢吃"+meatService.whatMeat()); } MeatService getMeatService() { meatService; } setAge( age) { .age = age; } @Resource(name="fish") setMeatService(MeatService meatService) { .meatService = meatService; } }

com.b510.service.impl; javax.annotation.postconstruct; javax.annotation.PreDestroy; javax.annotation.Resource; org.springframework.stereotype.Component; com.b510.service.AnimalService; com.b510.service.MeatService; @Component("dog") DogServiceBean AnimalService { age; 方法可以不要滴 @Resource(name = "pork") MeatService meatService; DogServiceBean() { System.out.println("狗类被初始化了"); } getAge() { age; } 方法 @postconstruct @Override getInfo() { System.out.println("我是狗,我的年龄是:" + age + ",我很喜欢吃" + meatService.whatMeat()); } MeatService getMeatService() { meatService; } setAge( age) { .age = age; } 方法 @PreDestroy close() { System.out.println("这是Bean DogServiceBean销毁之前的方法"); } }

com.b510.service.impl; org.springframework.stereotype.Component; com.b510.service.MeatService; @Component("fish") FishServiceBean MeatService { FishServiceBean(){ System.out.println("鱼肉类被初始化了"); } @Override String whatMeat() { "鱼肉"; } }

com.b510.service.impl; org.springframework.stereotype.Component; com.b510.service.MeatService; @Component("pork") PorkServiceBean MeatService { PorkServiceBean(){ System.out.println("猪肉类被初始化了"); } @Override String whatMeat() { "猪肉"; } }

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http:spring-beans-2.5.xsd http:decoration: underline;">http://www.springframework.org/schema/context/spring-context-2.5.xsd http:decoration: underline;">http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http:decoration: underline;">http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ="com.b510.service"> ="com.b510.service.impl.CatServiceBean"> ="com.b510.service.impl.DogServiceBean">

2012-3-12 21:17:39 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClasspathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClasspathXmlApplicationContext@c1b531]; startup date [Mon Mar 12 21:17:39 CST 2012]; root of context hierarchy 2012-3-12 21:17:39 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions 信息: Loading XML bean deFinitions from path resource [beans.xml] 2012-3-12 21:17:39 org.springframework.beans.factory.support.DefaultListablebeanfactory registerBeanDeFinition 信息: Overriding bean deFinition bean 'cat': replacing [Generic bean: [com.b510.service.impl.CatServiceBean]; scope=singleton; =; lazyInit=; autowireMode=0; dependencyCheck=0; autowireCandidate=; primary=; factorybeanName=; factoryMethodName=; initMethodName=; destroyMethodName=; defined in file [D:\Wordplace\spring_workspace\spring_1800_Annotation\bin\com\b510\service\impl\CatServiceBean.]] with [Generic bean: [com.b510.service.impl.CatServiceBean]; scope=singleton; =; lazyInit=; autowireMode=0; dependencyCheck=0; autowireCandidate=; primary=; factorybeanName=; factoryMethodName=; initMethodName=; destroyMethodName=; defined in path resource [beans.xml]] 2012-3-12 21:17:39 org.springframework.beans.factory.support.DefaultListablebeanfactory registerBeanDeFinition 信息: Overriding bean deFinition bean 'dog': replacing [Generic bean: [com.b510.service.impl.DogServiceBean]; scope=singleton; =; lazyInit=; autowireMode=0; dependencyCheck=0; autowireCandidate=; primary=; factorybeanName=; factoryMethodName=; initMethodName=; destroyMethodName=; defined in file [D:\Wordplace\spring_workspace\spring_1800_Annotation\bin\com\b510\service\impl\DogServiceBean.]] with [Generic bean: [com.b510.service.impl.DogServiceBean]; scope=singleton; =; lazyInit=; autowireMode=0; dependencyCheck=0; autowireCandidate=; primary=; factorybeanName=; factoryMethodName=; initMethodName=; destroyMethodName=; defined in path resource [beans.xml]] 2012-3-12 21:17:39 org.springframework.context.support.AbstractApplicationContext obtainFreshbeanfactory 信息: Bean factory application context [org.springframework.context.support.ClasspathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListablebeanfactory@587c94 2012-3-12 21:17:39 org.springframework.beans.factory.support.DefaultListablebeanfactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListablebeanfactory@587c94: defining beans [cat,dog,fish,pork,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalrequiredAnnotationProcessor]; root of factory hierarchy 猫类被初始化了 鱼肉类被初始化了 我是猫,我的年龄是:2,我很喜欢吃鱼肉 狗类被初始化了 猪肉类被初始化了 我是狗,我的年龄是:5,我很喜欢吃猪肉 [cat,org.springframework.context.annotation.internalrequiredAnnotationProcessor]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐