Spring学习笔记(一)

Spring学习笔记(一)

1. pom.xml

  • <?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.hjx</groupId>
      <artifactId>01-spring</artifactId>
      <version>1.0.0</version>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    
      <dependencies>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.0.4.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

2. 配置文件spring-config.xml

  • 在resources 文件夹下创建 spring-config.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">
    
        <!--
            id : 对象的自定义名称,唯一值
            class : 类的全限定类名
         -->
        <bean id="someService" class="com.hjx.spring.impl.SomeServiceImpl"/>
    
    </beans>
    

3. 创建实体beans

  • /**
     * @Author: Hjx
     * @Date: 2021/9/15 19:21
     */
    public interface SomeService {
        /**
         * 进行操作的函数
         */
        public void doSome();
    }
    
  • /**
     * @Author: Hjx
     * @Date: 2021/9/15 19:21
     */
    public class SomeServiceImpl implements SomeService {
    
        /**
         * 进行操作的函数
         */
        @Override
        public void doSome() {
            System.out.println("执行doSome方法,进行了一系列的操作");
        }
    }
    

4. TestSpring测试类

  • /**
     * @Project: 01-spring
     * @Package: com.hjx.spring
     * @Description: TODO
     * @author: junxin.hu
     * @date: 2021/9/16 11:05
     * @version: V1.0
     */
    public class TestSpring01 {
        
        @Test
        public void test01(){
    
            // 指定spring容器的配置文件
            String configFile = "spring-config.xml";
    
            // 创建spring容器类
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configFile);
    
            // 从容器中获取生成的bean
            // 参数 someService 为spring-config.xml 文件中配置的 bean的 id
            SomeService someService = (SomeService)applicationContext.getBean("someService");
    
            // 调用bean的方法
            someService.doSome();
    
            // 容器常用方法
            
            // 获取容器中 bean 的数量
            int count = applicationContext.getBeanDefinitionCount();
            System.out.println("容器中bean的数量:"+count);
    
            // 获取容器中所有 bean 的名称
            String[] names = applicationContext.getBeanDefinitionNames();
            for (String name:names){
                System.out.println("Bean的名称:"+name);
            }
        }
    }
    
  • 返回结果:
    
    执行doSome方法,进行了一系列的操作
    容器中bean的数量:1
    Bean的名称:someService
    

5. DI依赖注入

5.1 set注入

  • 创建实体类

  • /**
     *
     * @Project: 01-spring
     * @Package: com.hjx.spring.entity
     * @Description: TODO
     * @author: junxin.hu
     * @date: 2021/9/16 12:18
     * @version: V1.0
     */
    public class Student {
        private String name;
        private Integer age;
        private School school;
    
        public Student() {
        }
    
        public Student(String name, Integer age, School school) {
            this.name = name;
            this.age = age;
            this.school = school;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public School getSchool() {
            return school;
        }
    
        public void setSchool(School school) {
            this.school = school;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", school name=" + school.getName() +
                    '}';
        }
    }
    
  • /**
     *
     * @Project: 01-spring
     * @Package: com.hjx.spring.entity
     * @Description: TODO
     * @author: junxin.hu
     * @date: 2021/9/16 14:24
     * @version: V1.0
     */
    public class School {
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }
    
  • 在resources文件夹下创建di文件夹,在其下创建spring-config.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">
    
        <bean id="student" class="com.hjx.spring.entity.Student">
            <property name="name" value="张三"/>
            <property name="age" value="24"/>
            <!-- 引用类型依赖注入 -->
            <property name="school" ref="school"/>
        </bean>
    
        <bean id="school" class="com.hjx.spring.entity.School">
            <property name="name" value="北京大学"/>
        </bean>
    
    </beans>
    
  • 创建测试类

  • /**
     *
     * @Project: 01-spring
     * @Package: com.hjx.spring
     * @Description: TODO
     * @author: junxin.hu
     * @date: 2021/9/16 12:26
     * @version: V1.0
     */
    public class TestSpring {
        @Test
        public void test() {
            // 创建spring容器
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/di/spring-config.xml");
            // 获取实体bean
            Student student = (Student) applicationContext.getBean("student");
            // 打印bean的信息
            System.out.println(student.toString());
    
        }
    }
    
  • 打印结果:
    
    Student{name='张三', age=24, school name=北京大学}
    

5.2 构造方法注入

  • 修改配置文件

  • <?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">
    
        <bean id="student" class="com.hjx.spring.entity.Student">
            <constructor-arg name="age" value="25"/>
            <constructor-arg name="name" value="李四"/>
            <constructor-arg name="school" ref="school"/>
        </bean>
    
        <bean id="school" class="com.hjx.spring.entity.School">
            <property name="name" value="北京大学"/>
        </bean>
    
    </beans>
    
  • 重新运行 TestSpring测试类

  • 运行结果:
    
    
    Student{name='李四', age=25, school name=北京大学}
    

6. 引用类型自动注入

6.1 byName

  • <?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">
    
        <!--
            autowire = "byName"
            通过类的名称及bean的id 自动注入依赖
            条件:
                在类 Student 中的方法 setSchool
                    set 后面的字段(即 school) 必须 与 school bean 中的 id 对应(即school)
        -->
        <bean id="student" class="com.hjx.spring.entity.Student" autowire="byName">
            <property name="name" value="李四"/>
            <property name="age" value="24"/>
        </bean>
    
        <bean id="school" class="com.hjx.spring.entity.School">
            <property name="name" value="清华大学"/>
        </bean>
    
    </beans>
    

6.2 byType

  • <?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">
        
        <!-- 
            autowire = "byType"
            通过类的限定名称即 class 自动注入依赖
                Student类中需要注入 School类的依赖
                	spring会挨个查询 class 中的全限定类名,遇到匹配的类就注入依赖
                    注入的类也可以是 School 的子类
                    同样的,如果需要注入接口的依赖,按照类型注入其接口实现类也是可以的
                        
                    若同时存在 School类 和 它的子类 或者需要注入接口依赖但同时存在接口和其接口实现类,会报错,bean不唯一,无法识别注入哪一个依赖
        -->
        <bean id="student" class="com.hjx.spring.entity.Student" autowire="byType">
            <property name="name" value="李四"/>
            <property name="age" value="24"/>
        </bean>
    
        <bean id="mySchool" class="com.hjx.spring.entity.School">
            <property name="name" value="清华大学"/>
        </bean>
    
    </beans>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值