浏览代码

spring入门、IOC、DI、Bean基础配置、Bean的实例化、bean的生命周期

WanJL 3 天之前
父节点
当前提交
0782d202d1

+ 26 - 0
springDemo/pom.xml

@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.sf</groupId>
+    <artifactId>springDemo</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>11</maven.compiler.source>
+        <maven.compiler.target>11</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <!--用来指定要依赖的外部库坐标-->
+    <dependencies>
+        <!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <version>5.2.10.RELEASE</version>
+        </dependency>
+    </dependencies>
+</project>

+ 28 - 0
springDemo/src/main/java/com/sf/beanLifeCycle/Main.java

@@ -0,0 +1,28 @@
+package com.sf.beanLifeCycle;
+
+
+import com.sf.beanLifeCycle.service.TeaService;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Main
+ * @description
+ * @create 2025/11/23
+ */
+public class Main {
+    public static void main(String[] args) {
+        //ApplicationContext接口没有close方法,所以要使用它的实现类
+        ClassPathXmlApplicationContext context =
+                new ClassPathXmlApplicationContext("applicationContext.xml");
+        //注册销毁钩子方法,让Java程序在结束之前,先关闭IOC容器,再结束
+        context.registerShutdownHook();
+
+        TeaService teaService = (TeaService)context.getBean("TeaService");
+        teaService.print();
+        //程序结束之前,先销毁context
+        //context.close();
+    }
+}

+ 13 - 0
springDemo/src/main/java/com/sf/beanLifeCycle/service/TeaService.java

@@ -0,0 +1,13 @@
+package com.sf.beanLifeCycle.service;
+
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title TeaService
+ * @description
+ * @create 2025/11/23
+ */
+public interface TeaService {
+    void print();
+}

+ 38 - 0
springDemo/src/main/java/com/sf/beanLifeCycle/service/impl/TeaServiceImpl.java

@@ -0,0 +1,38 @@
+package com.sf.beanLifeCycle.service.impl;
+
+
+import com.sf.beanLifeCycle.service.TeaService;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.InitializingBean;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title TeaServiceImpl
+ * @description
+ * @create 2025/11/23
+ */
+public class TeaServiceImpl implements TeaService, InitializingBean, DisposableBean {
+    /*
+        InitializingBean 接口 是专门用来规定初始化规则的接口,里面有初始化的抽象方法
+        DisposableBean 接口是专门永安里规定销毁前执行的规则的接口,里面有销毁前要执行的抽象方法
+        无论是不是实现了InitializingBean接口的初始化方法,在初始化bean的时候都会执行bean的初始化方法
+     */
+
+
+    @Override
+    public void print() {
+        System.out.println("TeaServiceImpl.print()方法.....");
+    }
+
+    @Override
+    public void destroy() throws Exception {
+        System.out.println(this+"对象即将要被销毁了........");
+    }
+
+    @Override
+    public void afterPropertiesSet() throws Exception {
+        System.out.println("当前的XXX线程创建了一个"+this+"对象");
+
+    }
+}

+ 29 - 0
springDemo/src/main/java/com/sf/ioc/Main.java

@@ -0,0 +1,29 @@
+package com.sf.ioc;
+
+
+import com.sf.ioc.service.ProductService;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Main
+ * @description
+ * @create 2025/11/23
+ */
+public class Main {
+    public static void main(String[] args) {
+        //1、创建IOC容器,加载spring核心配置文件
+        ApplicationContext context =
+                new ClassPathXmlApplicationContext("applicationContext.xml");
+        //2、从IOC容器中获取Bean对象
+        //UserService userService = (UserService)context.getBean("userService");
+
+        //3、调用Bean对象的方法
+        //userService.print();
+
+        ProductService productService = (ProductService)context.getBean("productService");
+        productService.print();
+    }
+}

+ 19 - 0
springDemo/src/main/java/com/sf/ioc/factory/ProductServiceFactory.java

@@ -0,0 +1,19 @@
+package com.sf.ioc.factory;
+
+
+import com.sf.ioc.service.ProductService;
+import com.sf.ioc.service.impl.ProductServiceImpl;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title ProductServiceFactory
+ * @description
+ * @create 2025/11/23
+ */
+public class ProductServiceFactory {
+
+    public ProductService getProductService() {
+        return new ProductServiceImpl();
+    }
+}

+ 18 - 0
springDemo/src/main/java/com/sf/ioc/factory/StudentServiceFactory.java

@@ -0,0 +1,18 @@
+package com.sf.ioc.factory;
+
+
+import com.sf.ioc.service.StudentService;
+import com.sf.ioc.service.impl.StudentServiceImpl;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title TeaServiceFactory
+ * @description TeaService的静态工厂类
+ * @create 2025/11/23
+ */
+public class StudentServiceFactory {
+    public static StudentService getStudentService(){
+        return new StudentServiceImpl();
+    }
+}

+ 13 - 0
springDemo/src/main/java/com/sf/ioc/mapper/UserMapper.java

@@ -0,0 +1,13 @@
+package com.sf.ioc.mapper;
+
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title UserMapper
+ * @description
+ * @create 2025/11/23
+ */
+public interface UserMapper {
+    void login();
+}

+ 18 - 0
springDemo/src/main/java/com/sf/ioc/mapper/UserMapperImpl.java

@@ -0,0 +1,18 @@
+package com.sf.ioc.mapper;
+
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title UserMpapperImpl
+ * @description
+ * @create 2025/11/23
+ */
+public class UserMapperImpl implements UserMapper {
+
+    @Override
+    public void login() {
+        //用户名+密码方式登录
+        System.out.println("mapper01......");
+    }
+}

+ 16 - 0
springDemo/src/main/java/com/sf/ioc/mapper/UserMapperImpl02.java

@@ -0,0 +1,16 @@
+package com.sf.ioc.mapper;
+
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title UserMapperImpl02
+ * @description
+ * @create 2025/11/23
+ */
+public class UserMapperImpl02 implements UserMapper {
+    @Override
+    public void login() {
+        System.out.println("mapper02......");
+    }
+}

+ 13 - 0
springDemo/src/main/java/com/sf/ioc/service/ProductService.java

@@ -0,0 +1,13 @@
+package com.sf.ioc.service;
+
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title ProductService
+ * @description
+ * @create 2025/11/23
+ */
+public interface ProductService {
+    void print();
+}

+ 12 - 0
springDemo/src/main/java/com/sf/ioc/service/StudentService.java

@@ -0,0 +1,12 @@
+package com.sf.ioc.service;
+
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title StudentService
+ * @description
+ * @create 2025/11/23
+ */
+public interface StudentService {
+}

+ 14 - 0
springDemo/src/main/java/com/sf/ioc/service/UserService.java

@@ -0,0 +1,14 @@
+package com.sf.ioc.service;
+
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title UserService
+ * @description
+ * @create 2025/11/23
+ */
+public interface UserService {
+    void print();
+
+}

+ 18 - 0
springDemo/src/main/java/com/sf/ioc/service/impl/ProductServiceImpl.java

@@ -0,0 +1,18 @@
+package com.sf.ioc.service.impl;
+
+
+import com.sf.ioc.service.ProductService;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title ProductServiceImpl
+ * @description
+ * @create 2025/11/23
+ */
+public class ProductServiceImpl implements ProductService {
+    @Override
+    public void print() {
+        System.out.println("实例工厂创建bean....");
+    }
+}

+ 14 - 0
springDemo/src/main/java/com/sf/ioc/service/impl/StudentServiceImpl.java

@@ -0,0 +1,14 @@
+package com.sf.ioc.service.impl;
+
+
+import com.sf.ioc.service.StudentService;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title StudentServiceImpl
+ * @description
+ * @create 2025/11/23
+ */
+public class StudentServiceImpl implements StudentService {
+}

+ 36 - 0
springDemo/src/main/java/com/sf/ioc/service/impl/UserServiceImpl.java

@@ -0,0 +1,36 @@
+package com.sf.ioc.service.impl;
+
+
+import com.sf.ioc.mapper.UserMapper;
+import com.sf.ioc.service.UserService;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title UserServiceImpl
+ * @description
+ * @create 2025/11/23
+ */
+public class UserServiceImpl implements UserService {
+
+
+    //构造方法
+    public UserServiceImpl() {
+        System.out.println("userService的构造方法.....");
+    }
+
+    private UserMapper userMapper;
+
+    /**
+     * 为UserMapper增加set方法
+     * @param userMapper
+     */
+    public void setUserMapper(UserMapper userMapper) {
+        this.userMapper = userMapper;
+    }
+    @Override
+    public void print() {
+        userMapper.login();
+        System.out.println("UserServiceImpl......");
+    }
+}

+ 47 - 0
springDemo/src/main/resources/applicationContext.xml

@@ -0,0 +1,47 @@
+<?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配置文件,配置对应的类,作为spring管理的bean-->
+    <!--
+        bean是专门指在IOC容器里的对象
+        bean标签:表示配置bean
+        id属性:是给bean取个名字
+        class属性:表示给bean定义实际的类型
+        name属性:为这个bean取别名
+        scope属性:定义bean的作用范围,可以选择:单例(singleton)、非单例(prototype),默认scope属性的值是singleton单例模式
+        request
+        session
+        application
+        websocket....
+    -->
+    <bean id="UserMapper" name="userMapper,userMapperImpl,userMapperImpl1" class="com.sf.ioc.mapper.UserMapperImpl" scope="singleton" />
+    <!--配置service和mapper之间的关系-->
+    <bean id="UserService" name="userService,userServiceImpl1" class="com.sf.ioc.service.impl.UserServiceImpl" scope="prototype">
+        <!--
+            property标签:配置当前的bean的属性
+            name属性:对应着具体的属性变量
+            ref属性:对应着要参照的是哪一个bean
+        -->
+        <property name="userMapper" ref="UserMapper"/>
+    </bean>
+
+    <bean id="StudentService" name="studentService"
+          class="com.sf.ioc.factory.StudentServiceFactory" factory-method="getStudentService" />
+
+    <!--先创建工厂bean-->
+    <bean id="productServiceFactory" class="com.sf.ioc.factory.ProductServiceFactory"/>
+
+    <!--创建productServiceBean的时候,引入工厂bean-->
+    <bean id="ProductService" name="productService"
+          factory-bean="productServiceFactory" factory-method="getProductService"/>
+
+    <!--
+        init-method属性:表示这个bean初始化后立刻要执行的方法
+        destroy-method属性:表示这个bean在销毁之前要执行的方法
+    -->
+    <bean id="TeaService" name="teaService"
+          class="com.sf.beanLifeCycle.service.impl.TeaServiceImpl"/>
+</beans>
+