
Spring是一个开源框架,为简化企业级开发而生。它以IOC(控制反转)和AOP(面向切面)为思想内核,提供了控制层SpringMVC、数据层SpringData、服务层事务管理等众多技术,并可以整合众多第三方框架。Spring将很多复杂的代码变得优雅简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。 Spring官网地址:Spring | Home Spring官方网站:

Spring框架根据不同的功能被划分成了多个模块,这些模块可以满足一切企业级应用开发的需求,在开发过程中可以根据需求有选择性地使用所需要的模块。

接下来我们使用Spring实现IOC,Spring内部也有一个容器用来管理对象。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>Student实体类
package com.example.pojo;
public class Student {
private int id;
private String name;
private String address;
public Student(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public Student(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student[ " +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
" ]";
}
}StudentDao接口
package com.example.dao;
import com.example.pojo.Student;
public interface StudentDao {
// 根据id查询学生
Student findById(int id);
}StudentDao接口实现类StudentDaoImpl1
package com.example.dao;
import com.example.pojo.Student;
public class StudentDaoImpl1 implements StudentDao{
public StudentDaoImpl1() {
}
public StudentDaoImpl1(int a){};
@Override
public Student findById(int id){
return new Student(id,"程序员","北京");
}
}<?xml version="1.0" encoding="UTF-8" ?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="constructor">
<bean id="studentDao" class="com.example.dao.StudentDaoImpl1"/>
</beans>package com.example;
import com.example.dao.StudentDao;
import com.example.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestContainer {
@Test
public void t1(){
// 创建Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 从容器中获取对象
StudentDao studentDao1 = ac.getBean("studentDao",StudentDao.class);
StudentDao studentDao2 = ac.getBean("studentDao",StudentDao.class);
System.out.println(studentDao1.hashCode());
System.out.println(studentDao2.hashCode());
System.out.println(studentDao1.findById(1));
}
}
OK,同样返回两个对象的哈希值都是一样的,说明了确实是从容器中获取同一个对象。

OK,同样返回两个对象的哈希值都是一样的,说明了确实是从容器中获取同一个对象。
@Test
public void t2(){
// 创建spring容器
ApplicationContext ac = new FileSystemXmlApplicationContext("C:\\JavaProjects\\06SSM_Projects\\springdemo\\spring_ioc1\\src\\main\\resources\\bean.xml");
// 从容器中获取对象
StudentDao userDao = ac.getBean("studentDao",StudentDao.class);
System.out.println(userDao);
System.out.println(userDao.findById(1));;
}
OK,本次使用Spring实现IOC就到这里了,上述讲到的三个实现类会在接下来中多次使用,希望对大家有所帮助
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。