Spring基础入门二:基于XML及注解配置完成Spring对Bean的注入

第一种方法:手动装配完成依赖注入

基于XML配置的:

1。通过set方法注入

	<!-- 手动装配 通过setter方式注入 -->
	<!-- Spring的DI注入方式 -->
	<bean id="studao" class="di.dao.imple.StudentDaoImple"></bean>
	
	<bean id="stuService"
		class="di.service.imple.StudentServiceImple">
		<property name="stuDao" ref="studao"></property>
	</bean>
 
public class StudentServiceImple implements StudentService {
	private StudentDao stuDao;

	public void save() {
		stuDao.add();
	}

	public void setStuDao(StudentDao stuDao) {
		this.stuDao = stuDao;
	}
}

 2。通过构造方法注入

<!-- 手动装配 通过构造器方式注入  -->
	<bean id="stuService3"
		class="di.service.imple.StudentServiceImple2">
		<constructor-arg>
			<bean class="di.dao.imple.StudentDaoImple"></bean>
		</constructor-arg>
	</bean>

	<bean id="stuService4"
		class="di.service.imple.StudentServiceImple2">
		<constructor-arg>
			<bean class="di.dao.imple.StudentDaoImple"></bean>
		</constructor-arg>
		<constructor-arg>
			<bean class="java.lang.String"></bean>
		</constructor-arg>
		<!--  
		<constructor-arg index="0" type="di.dao.StudentDao" ref="studao" />
		<constructor-arg index="1" type="java.lang.String" value="xiaobo" />
		-->
	</bean>
 
public class StudentServiceImple2 implements StudentService {

	private StudentDao stuDao;

	private String stuName;

	public StudentServiceImple2(StudentDao stuDao) {
		this.stuDao = stuDao;
	}
	
	public StudentServiceImple2(StudentDao stuDao,String stuName) {
		this.stuDao = stuDao;
		this.stuName = stuName;
	}

	public void save() {
		System.out.println("=====通过构造器注入进来的属性====");
		System.out.println(stuDao);
		System.out.println(stuName);
		stuDao.add();
	}
}

 3。通过内部Bean完成注入及其它集合类型的属性注入

<!-- Spring的DI内部Bean注入方式 -->
	<bean id="stuService2"
		class="di.service.imple.StudentServiceImple">
		<property name="stuDao">
			<bean class="di.dao.imple.StudentDaoImple"></bean>
		</property>
		<!-- 普通属性的注入 -->
		<property name="stuName" value="http://zmx.iteye.com"></property>
		<property name="stuAge" value="23"></property>
		<!-- 集合类型的注入 -->
		<property name="sets">
			<set>
				<value>abc</value>
				<value>mengya</value>
				<ref bean="studao"></ref>
			</set>
		</property>
		<property name="lists">
			<list>
				<value>list1</value>
				<ref bean="studao"></ref>
			</list>
		</property>
		<property name="properties">
			<props>
				<prop key="key1">value1</prop>
				<prop key="key2">value2</prop>
				<prop key="key3">value3</prop>
			</props>
		</property>
		<property name="maps">
			<map>
				<entry key="mapkey1" value="mapValue1"></entry>
				<entry>
					<key>
						<value>mapkey2</value>
					</key>
					<ref bean="studao"></ref>
				</entry>
			</map>
		</property>
	</bean>
 
/**
 * 手动装配 通过set方法注入
 * 
 * @author 张明学
 */
public class StudentServiceImple implements StudentService {
	private StudentDao stuDao;
	private String stuName;
	private Integer stuAge;
	private Set<Object> sets;
	private List<Object> lists;
	private Properties properties;
	private Map<String, Object> maps;
	public void save() {
		stuDao.add();
		System.out.println("------普通属性注入-----");
		System.out.println(stuName);
		System.out.println(stuAge);
		System.out.println("------set集合注入-----");
		for (Object str : sets) {
			System.out.println(str);
		}
		System.out.println("------list集合注入-----");
		for (Object str : lists) {
			System.out.println(str);
		}
		System.out.println("------properties合注入-----");
		Set<Entry<Object, Object>> entrys = properties.entrySet();
		for (Map.Entry<Object, Object> entry : entrys) {
			System.out.println(entry.getKey() + " = " + entry.getValue());
		}
		System.out.println("------map合注入-----");
		for (Map.Entry<String, Object> entry : maps.entrySet()) {
			System.out.println(entry.getKey() + " = " + entry.getValue());
		}
	}
	public void setStuAge(Integer stuAge) {
		this.stuAge = stuAge;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public void setStuDao(StudentDao stuDao) {
		this.stuDao = stuDao;
	}
	public void setSets(Set sets) {
		this.sets = sets;
	}
	public void setLists(List<Object> lists) {
		this.lists = lists;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	public void setMaps(Map<String, Object> maps) {
		this.maps = maps;
	}
}

 基于注解配置的:@Autowired与@Resource的用法

<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">
    <!-- 手动装配 通过注解的方式注入
    	 添加common-annotations.jar,配置下面内容即打开Spring对注解的解析器的功能
     -->
	<context:annotation-config/>
	
	<bean id="myStuDao" class="di.dao.imple.StudentDaoImple"></bean>
	<bean id="stuService" class="di.service.imple.StudentServiceImple3"></bean>
</beans>
 
/**
 * 手动装配 通过注解的方式注入对象
 * 
 * @author 张明学
 */
/**
 * 方式:@Autowired 
 * 		表示:可以用在字段或set方法上面,按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,
 * 			 可以设置它required属性为false。如果我们使用按名称装配,可以结合@Qualifier注解一起使用
 * 			 如:@Autowired(required=false)
 *			    @Qualifier("stuDao")
 *	            private StudentDao stuDao;
 *	                       
 * 		@Resource  
 * 		表示:可以用在字段或set方法上面,按名称装配依赖对象,名称可以通过@Resource的name属性指定,如果没有
 * 			 指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性
 * 			 的set方法上,即默认取属性名作为bean名称寻找依赖对象。
 * 			 如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时,@Resource注解会回退到按类型装配。
 *			 但一旦指定了name属性。就只能按名称装配了。
 */
public class StudentServiceImple3 implements StudentService {
	
	@Resource(name="myStuDao")
	private StudentDao stuDao;
	
	public void save() {
		stuDao.add();
	}
}

 测试:

public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans3.xml");
		StudentService stuService = null;
		stuService = (StudentService) ctx.getBean("stuService");
		stuService.save();
	}

 第二种方法:自动装配完成依赖注入

<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">
	<!--依赖注入:自动装配方式-->
	<!-- 
		autowire:byName表示:按名称装配,可以根据属性名称,在容器中寻找跟属性名相同的bean,
		如果没有找到,即属性值为null。
		byType表示:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean,
		如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null。
		constructor表示:与byType的方式类似,不同之处在于它应用于构造器参数。如果
		在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
		autodetect表示:通过bean类的自省机制(introspection)来决定是使用constructor
		还是byType方式进行自动分配。
	-->
	<bean id="stuDao"
		class="di.autowire.dao.imple.StudentDaoImple">
	</bean>
	<bean id="stuService1"
		class="di.autowire.service.imple.StudentServiceImple"
		autowire="byName">
	</bean>
	<bean id="stuService2"
		class="di.autowire.service.imple.StudentServiceImple"
		autowire="byType">
	</bean>
	<bean id="stuService3"
		class="di.autowire.service.imple.StudentServiceImple"
		autowire="constructor">
	</bean>
</beans>

 javaBean代码如下:

package di.autowire.dao;

public interface StudentDao {
	public abstract void add();
}
 
package di.autowire.dao.imple;

import di.dao.StudentDao;

public class StudentDaoImple implements StudentDao {
	public void add() {
		System.out.println("自动按配StudentdaoImple的add方法");
	}
}
 
package di.autowire.service;

public interface StudentService {
	public abstract void save();
}
 
/**
 * 使用Spring自动装配
 * 
 * @author 张明学
 */
public class StudentServiceImple implements StudentService {

	private StudentDao stuDao;

	// 通过byName或byType都要提供set方法注入
	public void setStuDao(StudentDao stuDao) {
		this.stuDao = stuDao;
	}
	public void save() {
		stuDao.add();
	}
	public StudentServiceImple() {

	}
	// 通过构造器进行依赖注入
	public StudentServiceImple(StudentDao stuDao) {
		this.stuDao = stuDao;
	}
}

 测试:

/**
 * 依赖注入:自动装配
 * 
 * @author 张明学
 */
public class AutowireDITest {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans4.xml");
		StudentService stuService = null;
		// stuService = (StudentService) ctx.getBean("stuService1");
		// stuService = (StudentService) ctx.getBean("stuService2");
		stuService = (StudentService) ctx.getBean("stuService3");
		stuService.save();
	}
}
 

你可能感兴趣的:(DAO,spring,xml,bean)