Spring注解实现依赖注入(DI)配置细节

spring配置文件

<?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:context="http://www.springframework.org/schema/context"       
       xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	
	<context:component-scan base-package="cn.itcast"></context:component-scan>
	<!-- 写了自动扫描配置就不需要,<context:annotation-config /> 不需要这个节点,因为自动扫描会把注解配置处理器一并注册上 -->
	<!-- 扫描base-package包下所有的标注了以下四个注解的类
	@Component 泛指组件,当组件不好归类的时候用这个    spring 注解
	@Service	用于标注业务层组件(BLL层)           	 spring 注解    
	@Repository 用于标注数据访问层组件(Dao层)			 spring 注解
	@Controller 用于标注控制层组件(Action)			 spring 注解
	@Scope("prototype")指定bean的作用域				 spring 注解
	@PostConstruct 指定bean实化后的初始化方法			java注解
	-->
</beans>
package cn.itcast.dao;

public interface IPersonDao {

	public void add();
}
package cn.itcast.dao;

public interface IPersonDao {

	public void add();
}
package cn.itcast.service;

public interface IPersonService {

	public abstract void save();

}
package cn.itcast.service.impl;

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

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

import cn.itcast.dao.IPersonDao;
import cn.itcast.service.IPersonService;

//@Service
@Service("personService")   //指定纳入管理的bean的名称
//@Scope("prototype")
public class PersonServiceBean implements IPersonService {
	private IPersonDao personDao;
	
	public PersonServiceBean() {
		System.out.println("空构造函数");
	}
	@Resource
	public void setPersonDao(IPersonDao personDao) {
		this.personDao = personDao;
	}
	@PostConstruct
	public void init(){
		System.out.println("PersonServiceBean初始化。。。");
	}
	@PreDestroy
	public void destory(){
		System.out.println("PersonServiceBean销毁,关闭资源操作");
	}
	@Override
	public void save(){
		personDao.add();
	}
}
package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.IPersonService;

public class SpringTest {
	static AbstractApplicationContext ctx=null;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
	}
	@Test
	public void test(){
		IPersonService personServiceBean = (IPersonService)ctx.getBean("personService");
		IPersonService personServiceBean1 = (IPersonService)ctx.getBean("personService");
		System.out.println(personServiceBean==personServiceBean1);
		personServiceBean.save();
		ctx.close();
	}
}
一、实例化spring容器有两种方式
1、在类路径下寻找配置文件来实例化容器
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"application.xml"});
2、在文件系统下寻找配置文件来实例化容器
ApplicationContext ctx=new FileSystemXMLApplication(new String[]{"d:\\beans.xml"});

spring的配置文件可以指定多个,可以通过String传入

二、实例化Bean的三种方法
1、spring反射来创建
   <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" />
   
2、静态工厂实例化
	<bean id="personService2" class="cn.itcast.service.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean" />

3、实例工厂方法来创建,要实例化工厂的对象来创建Bean,这里就不是静态工厂了
	<bean id="personServiceBeanFactory2" class="cn.itcast.service.impl.PersonServiceBeanFactory2"></bean>
	<bean id="personService3" factory-bean="personServiceBeanFactory2" factory-method="createPersonServiceBean" />
虽然三种实例化的方式不一样,但默认实例化的Bean都是单例的
	

三、注解注入
<context:annotation-config />
	<!-- 这个配置隐式注册了多个对注释进行解析处理的处理器,
	AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanProcessor,
	PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor 
	-->
	注解注入两种方式
	1、属性上面加@Resource注解
	2、set方法上加@Resource注解
	
四、自动扫描方式把组件纳入Spring容器管理

	<context:annotation-config />  注解注入配置
	<context:component-scan base-package="cn.itcast"></context:component-scan> 组件自动扫描配置
	<!-- 写了自动扫描配置就不需要,<context:annotation-config /> 不需要这个节点,因为自动扫描会把注解配置处理器一并注册上 -->
	<!-- 扫描base-package包下所有的标注了以下四个注解的类
	@Component 泛指组件,当组件不好归类的时候用这个    spring 注解
	@Service	用于标注业务层组件(BLL层)           	 spring 注解    
	@Repository 用于标注数据访问层组件(Dao层)			 spring 注解
	@Controller 用于标注控制层组件(Action)			 spring 注解
	目前,这些注解的功能是一样的,没有区别
	@Scope("prototype")指定bean的作用域				 spring 注解
	@PostConstruct 指定bean实化后的初始化方法			java注解
	@PreDestroy 指定 bean销毁方法						java注解
	-->

猜你喜欢

转载自88548886.iteye.com/blog/1570926