为了账号安全,请及时绑定邮箱和手机立即绑定

spring的bean的注解

标签:
Java

bean可以xml配置,也可以采用注解,注解更加简单,注解也需要配置相关的xml文件。因为bean的装配,采用注解和使用配置不同,所以注解的schema和命名空间都不一样。

重要的xml配置地方

	<!-- 配置 注解Bean 所在包 -->  
	<context:annotation-config/>
	<context:component-scan base-package="dflx.test"></context:component-scan>

bean的实列化采用 Component注解,简单属性采用 Value注解,复杂属性采用 Autowired注解。

spring2.5 引入@Component 等效三个衍生注解
@Repository 用于对DAO实现类进行标注 (持久层)
@Service 用于对Service实现类进行标注 (业务层)
@Controller 用于对Controller实现类进行标注 (表现层)

Autowired 默认按照类型进行注入, @Value @Autowired注解都能够修饰 成员变量 或者 setter方法,如果修改成员变量,不需要提供setter方法。

看一个实列,有Person类,Student类,在Student类中注入Person的对象。

其中Person类,有@Component,@Value注入

@Component(value="person")
public class Person {
	@Value("alice ")
	private String name;
	
	public String  getName()
	{
		return name;
	}
	
	public void print()
	{
		System.out.println("对象调用了我啊");
	}


}

Studet中有 Autowired注释。


@Component(value="student")
public class Student {
	
	@Autowired
	@Qualifier("person")
	//上面二条等价于 其 @Resource(name="userDAO")
	private Person person;
	
	public void displayName()
	{
		System.out.println("name is="+person.getName());
	}
	
}

在main中进行测试


public class Main {

	public static void main(String[] args) {
		
       ApplicationContext app=new ClassPathXmlApplicationContext("bean.xml");
		
		Student student=(Student) app.getBean("student");
		
		student.displayName();

	}

}

结果如下所示

name is=alice 

bean的初始化和销毁

xml中配置bean的初始化为

<bean id="name" class="name"  init-method="function name">

采用注解,@PostConstruct 注释。

xml中 bean的销毁为

<bean id="name" class="name"  destroy-method="function name">

采用注解为 @PreDestroy 注释

来一个InitDestDem类测试一下。

@Component("idtest")
public class InitDestDem {
	
	@PostConstruct
	public  void initFun()
	{
		System.out.println(" inin function start");
	}
	
     @PreDestroy
	public void destroyFun()
	{
		System.out.println(" destroy function start");
	}
	
	
}

在main方法测试结果

	public static void main(String[] args) {
		
		ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("bean.xml");
		
		InitDestDem ind=(InitDestDem) app.getBean("idtest");
		
		 System.out.println(ind);
		 
		 //销毁方法执行,必须调用 applicationcontest的 close方法。
		 
	  app.close();
	}

最后的结果如下

 inin function start
dflx.test.InitDestDem@42607a4f
九月 09, 2018 12:45:26 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@728938a9: startup date [Sun Sep 09 12:45:25 CST 2018]; root of context hierarchy
 destroy function start

bean的作用域 也可以采用 注解 @Scope 来说明, singleton 单例是默认的。

现在再 InitDestDem上采用 @Scope(“prototype”) 注解一下,在生产2个对象,看其的hash的值

 inin function start
dflx.test.InitDestDem@25bbf683
 inin function start
dflx.test.InitDestDem@6ec8211c

和小伙伴,建了一个公众号,在摸索中,欢迎关注。搜索公众号:东风冷雪,英文:satan_master ,现在探索中。无所不有,包括生活,学习,娱乐。 三大板块。

公众号二维码.jpg

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
73
获赞与收藏
369

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消