Spring笔记(一)环境搭建,ioc

it2025-10-30  1

Spring笔记一

spring:一、下载JAR包二、编写配置文件三、使用容器 IoC:一、概念1、before2、after IoC创建对象方式一、默认用无参构造二、如何使用有参构造 Spring配置一、别名二、Bean的配置三、import

spring:

官网

https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#spring-core

一、下载JAR包

https://maven.springframework.org/release/org/springframework/spring/4.3.9.RELEASE/ 以4.3.9版本为例(spring-framework-4.3.9.RELEASE-dist.zip)

开发spring至少需要使用的jar(5个+1个):spring-aop. jar开发AOP特性时需要的JARspring-beans. jar处理Bean的jarspring-context. jar处理spring_上下文的jarspring-core. jarspring核心jarspring- expression. jarspring表达式三方提供的日志jarcommons- logging. jar日志

或者直接一个spring-webmvc

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency>

二、编写配置文件

为了编写时有一-些提示、自动生成一 些配置信息,可以增加支持spring的插件: spring tool suite

(services.xml)配置文件👇 实际上就是上面get,如果把get方法删除则会报错 创建bean就相当于new了一个对象,默认调用无参

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- services --> <!--使用Spring创建对象,在Spring中称为Bean 类型 变量名 = new 类型(); HeLlo hello = new Hello(); id = 变量名 class = new的对象; . property 相当于给对象中的属性设置一个值! --> <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl"> <property name="accountDao" ref="accountDao"/> <property name="itemDao" ref="itemDao"/> <!-- additional collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions for services go here --> </beans>

三、使用容器

不再需要new

public class MyTest { public static void main(String[] args) { //获取spring的上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.toString()); } }

IoC:

控制反转

一、概念

1、before

UserDao 👉 UserDaoImpl 👉 UserService 👉 UserServiceImpl 👉 main

-------------👉MysqlDaoImpl👉 UserService 👉 UserServiceImpl 👉 main

必须修改UserService

2、after

用户增加需求模块,不需改修改底层代码

IoC创建对象方式

一、默认用无参构造

二、如何使用有参构造

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--默认使用无参构造--> <!-- <bean id="user1" class="User">--> <!-- <property name="name" value="zs"/>--> <!-- </bean>--> <!--使用有参构造--> <!--下标赋值--> <bean id="user2" class="User"> <constructor-arg index="0" value="张三"/> </bean> <!--通过类型赋值(不实用)--> <bean id="user3" class="User"> <constructor-arg type="java.lang.String" value="张三"/> </bean> <!--参数名赋值(主要)--> <bean id="user4" class="User"> <constructor-arg name="name" value="张三"/> </bean> </beans>

Spring配置

一、别名

<!--别名--> <alias name="user2" alias="new"/>

有了别名,用别名也能调用

二、Bean的配置

<!-- id:bean 的唯一标识符,也就是相当于我们学的对象名 class: bean 对象所对应的全限定名: 包名 + 类型 name:也是别名,而且name可以取多个别名(,;都可以) --> <bean id="user2" class="User" name="newuser,u2;u3"> <constructor-arg index="0" value="张三2"/> </bean>

三、import

一般用于团队开发,将多个配置文件导入合并成一个

最新回复(0)