spring学习1 springIOC的三种注入方式

本文介绍了Spring框架中IOC容器的三种对象注入方式:setter注入、构造方法注入和注解注入,并通过具体示例展示了如何在实际项目中应用这些注入方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在学习spring3.2.2框架,记录一下springIOC的三种对象注入方式

1. 项目结构图


2. bean对象类 com.xdh.vo.Student

package com.xdh.vo;

public class Student {
	private String name;
	private String id;
	private int age;
	
	//spring会默认调用该空白的构造方法,需要保留
	public Student(){
		
	}
	
	public Student(String id, String name, int age){
		this.id = id;
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
}

3. 接口StudentDAO

package com.xdh.dao;

import com.xdh.vo.Student;

public interface StudentDAO {
	public boolean save(Student stu);
}

4. 接口的实现类StudentDAOImpl

package com.xdh.dao.impl;

import com.xdh.dao.StudentDAO;
import com.xdh.vo.Student;

public class StudentDAOImpl implements StudentDAO {

	@Override
	public boolean save(Student stu) {
		
		if(stu != null){
			System.out.println("学号: " + stu.getId());
			System.out.println("姓名: " + stu.getName());
			System.out.println("年龄: " + stu.getAge());
			
			return true;
		}else{
			
			return false;
		}
	}
	
	
}

5. 服务类StudentService

package com.xdh.service;

import javax.annotation.Resource;

import com.xdh.dao.StudentDAO;
import com.xdh.vo.Student;

public class StudentService {
	
	@Resource(name="studentDAO")
	private StudentDAO stuDAO;
	
	@Resource(name="student")
	private Student stu;
	
	public StudentService(){
		
	}
	
	
	public StudentService(Student stu) {
		this.stu = stu;
	}


	public Student getStu() {
		return stu;
	}

	public void setStu(Student stu) {
		this.stu = stu;
	}

	public StudentDAO getStuDAO() {
		return stuDAO;
	}

	public void setStuDAO(StudentDAO stuDAO) {
		this.stuDAO = stuDAO;
	}


	public boolean saveStudent(){
		
		if(stuDAO.save(stu)){
			
			return true;
		}else{
			
			return false;
		}
	}
}

6. spriing的配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">

	<context:annotation-config/>
    <bean name="student" class="com.xdh.vo.Student">
    	<constructor-arg index="0" value="3110402121"></constructor-arg>
    	<constructor-arg index="1" value="夏董晖"></constructor-arg>
    	<constructor-arg index="2" value="18"></constructor-arg>
    </bean>
    
	<bean name="studentDAO" class="com.xdh.dao.impl.StudentDAOImpl"/>
	
	<bean name="studentService" class="com.xdh.service.StudentService">
		<!-- <property name="stuDAO" ref="studentDAO"></property> -->
		<!-- <constructor-arg ref="student"></constructor-arg> -->
	</bean>

</beans>

7. 测试类Test.java

package com.xdh.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xdh.service.StudentService;


public class Test {

	private static ApplicationContext ctx;

	public static void main(String[] args) {
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		StudentService stuService = (StudentService)ctx.getBean("studentService");
		stuService.saveStudent();
	}

}

说明:

spring注入对象有三种方法:

1. setter注入

2. 构造方法注入

3. 注解注入(推荐使用java的注解方式)


一般推荐使用注解注入,因为这样不用经常变动applicationContext.xml配置文件。

一般使用@Resource(name = "xxx"),其中xxx代表该对象在配置文件中的bean的名字。


注解的判断对象的顺序是先按名字匹配,再按类型匹配。

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值