Spring的IOC注解学习

先引入jar包,common-annotations.jar

接着上代码:

1、dao接口
package com.dao;

public interface OkpDao {
	public void save();
}

  

2、dao实现
package com.dao.impl;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dao.OkpDao;
@Component
public class OkpDaoImpl implements OkpDao{
    
    public void save() {
        System.out.println("OkpDaoImpl.save()");
    }
}

  

@Component注解:将该类加载为spring容器的组件,相当于在spring配置文件文件中的<bean name="okpDaoImpl" class="com.dao.Impl.OkpDaoImpl"/>,@Component注解格式为
@Component("name") 手动设置bean配置的name,如果不设置会自动设为 "类名首字母小写" 。
这四个注解用法基本一样,只是明确的分出哪个处理的层。@controller 控制器(注入服务) @service 服务(注入dao) @repository dao(实现dao访问) @component ,将其他几个注解标签替换上述代码的@Component标签会有同样的效果。

3、service
package com.service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dao.OkpDao;
@Component
public class OkpService {
	private OkpDao okpDao;

	public OkpDao getOkpDao() {
		return okpDao;
	}
	@Resource(name="okpDaoImpl")
	public void setOkpDao(OkpDao okpDao) {
		this.okpDao = okpDao;
	}
	public void save(){
		this.okpDao.save();
	}
	@PostConstruct
	public void init(){
		
		System.out.println("容器创建前执行");
	}
	
	@PreDestroy
	public void destory(){
		System.out.println("容器销毁后执行");
	}
} 

@Component也是将name="okpService"的bean加入到spring容器 
@Resource将name="okpDaoImpl"的bean 注入到okpDao,该注解先根据ByName找相应的bean,如果没有找到就会根据ByType查找,现在推荐使用该注解,不推荐使用@Autowired(根据ByTpe装配)以及@Qualifier(根据ByName装配)---该两个注解一般联合使用

@Autowired
public void setOkpDao(@Qualifier("okpDaoImpl")OkpDao okpDao) {
		this.okpDao = okpDao;
}

  


  因为根据ByType进行查找有可能会有多个bean,到处出错,所以还需要@Qualifier
  @PostConstruct 标记的方法代表容器创建之前执行的方法
  @PreDestory标记的方法代表容器销毁之后执行的方法
  还有一个不太常用的 @Scope代表该类的作用范围 value默认为Singleton单例模式,proptotype原型(每次都会创建一个新的对象)。
4、Service测试类
package com.service;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class OkpServiceTest {

	@Test
	public void testSave() {
		BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");
		OkpService okp=(OkpService) bf.getBean("okpService");
		okp.save();
	}

}
  5、spring配置文件  applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:tx="http://www.springframework.org/schema/tx" 
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:context="http://www.springframework.org/schema/context" 
		xmlns:jee="http://www.springframework.org/schema/jee"
		xsi:schemaLocation="
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
		<context:annotation-config />
		<context:component-scan base-package="com"></context:component-scan>
</beans>
  标签 <context:annotation-config/> 使用注解
  <context:component-scan base-package="com"></context:component-scan> 使用@Component注解 要加上这句,他会根据base-package的包名,扫描他下面所有的包,以及包中用@component注解的类 注册到Spring容器。
 
  由于刚开始学习Spring 注解,有什么理解有误的地方,求大婶告知。
posted @ 2014-04-20 00:48  以梦为码  阅读(450)  评论(2编辑  收藏  举报