SSM之SpringIOC入门

    科技2025-10-24  12

    Spring的介绍

    Spring介绍 Spring是分层的Java SE/EE应用full-stack轻量级开源框架 》full-stack:Service Dao Web 》轻量级:按需添加模块 》开源:可以获取源代码 以IoC(Inverse Of Control:反转控制)和AOP-(Aspect Oriented Programming:面向切面编程)为内核特点 提供了展现层SpringMVC 持久层Spring JDBC 能够整合开源世界众多著名的第三方框架和类库 业务层事务管理AOP 方便解耦,简化开发IOC所以Spring逐渐成为了使用最多的JavaEE企业应用开源框架

    Spring框架体系

    Test:用于测试使用Core container:核心容器,用于装Java Bean的对象AOP:切面编程Aspects:提供了与AspectJ的集成Data access:数据访问,用于访问操作我们的数据库。支持持久化的操作 jdbc Template mybatisweb:用于支持数据展示层,支持Http请求Transactions:用于支持事务处理,用于解决业务层的事务处理问题,编程事务管理和声明式事务管理

    Spring的IoC理论

    什么是IoC 控制反转 (Inversion of Control,缩写为IoC) 》把原来new对象的这种方式转换成了spring通过反射创建对象的方式 》spring创建完的对象放到一个容器中,谁需要就给谁注入(获取对象并赋值给引用) 简单来说,就是把创建对象和管理对象的权力交给了spring

    Spring 的IoC入门-代码编写

    创建Project maven创建模块module maven配置依赖 <!--spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.9.RELEASE</version> </dependency> 定义Person类 private String username; private String password; private int id; private String name; private int age; private Date birthday; 手动完成创建与赋值由spring创建与赋值 》创建容器对象 》度配置文件 new ClassPathXmlApplicationContext(“applicationContext.xml”); 》从容器中查找getBean()Test01SpringIoc @Test public void test01(){ //1:创建ioc 容器对象 暂时看成map ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); //2:给定配置文件的名称 applicationContext.xml //3:调用容器的getBean方法获取id对应的对象 Person person = (Person) context.getBean("person"); System.out.println(person); } 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 要让 spring容器给我创建一个person对象--> <!-- 配置类名,用于反向创建对象--> <!-- 同时给一个编号方便查找--> <bean id="person" class="com.wxx.domain.Person" /> </beans>
    Processed: 0.009, SQL: 8