spring简单注解的应用

最近迷上研究springIOC了,一种是使用xml方式进行bean的实例及相关注入依赖管理,一种是使用Annotation注解应用,在看到xml配置bean的实例化方式、scope以及延迟加载lazy-init等的配置时,再研究注解相关配置是竟不知道如何配了,在网上搜了一下,做一下总结:

<bean id="boy" class= "com.byzx.Boy" scope="singleton" lazy-init="true">
	<property name="pet" ref="dog"></property>
</bean>
其实相当于注解中的:
@Service(value = "boy") //相当于bean标签中的id,四种方式功能一样
// @Component(value = "boy")
// @Controller(value = "boy")
// @Repository(value = "boy")     ==	 id="boy"
@Scope(value = "singleton")       ==     scope="singleton"
@Lazy											==	 lazy-init="true"

@Autowired @Qualifler("dog") 
或者@Resource(name= "dog")   ==     <property name="pet" ref="dog">



回调方法的用法
@PostConstruct                     ==      init-method= "init" 	
public void initMethod(){	System.out.println("这是初始化的方法!");} 
@PreDestroy						==       destroy-method= "destroy" 
public void destroyMethod(){	System.out.println("这是销毁的方法! ");} 

另外,有时大家是不是对@SuppressWarnings 这个注解很陌生,他其实是对Java编译产生的警告的一种抑制,使之不要产生黄色警告来烦我,对于强迫症患者特别有效:
借用一下别人总结的:

关键字	                    用途
all                     	to suppress all warnings
boxing 					to suppress warnings relative to boxing/unboxing operations
cast						to suppress warnings relative to cast operations
dep-ann				to suppress warnings relative to deprecated annotation
deprecation			to suppress warnings relative to deprecation
fallthrough			 to suppress warnings relative to missing breaks in switch statements
finally				 	to suppress warnings relative to finally block that don’t return
hiding					to suppress warnings relative to locals that hide variable
incomplete-switch	    to suppress warnings relative to missing entries in a switch statement (enum case)
nls	 			     	to suppress warnings relative to non-nls string literals
null						to suppress warnings relative to null analysis
rawtypes				to suppress warnings relative to un-specific types when using generics on class params
restriction				to suppress warnings relative to usage of discouraged or forbidden references
serial					to suppress warnings relative to missing serialVersionUID field for a serializable class
static-access		to suppress warnings relative to incorrect static access
synthetic-access 		 to suppress warnings relative to unoptimized access from inner classes
unchecked					 to suppress warnings relative to unchecked operations
unqualified-field-access				to suppress warnings relative to field access unqualified
unused							to suppress warnings relative to unused code


在这儿,特别加一个这个@suppresswarnings(“resource”)。
@Resource可以用来完成依赖注入或者叫资源注入,但是当你在一个类中使用已经使用注解的类,却没有为其注入依赖,就会有黄色警告,这是你就可以放心大胆的用。。。。哈哈哈哈。

说一句废话
注解和xml是可以结合使用的,对于新手来说,这个可以解决好多迷惑。类加载用xml,属性值用注入方式,好多都在用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值