1.png

ioc模块

  1. xml配置版:Student、Phone、排序、list、set、map等例子

    • phone

      //实体类
      public class Phone {
          private String brand;
          private Double price;
      
          public Phone(String brand, Double price) {
              this.brand = brand;
              this.price = price;
          }
      
          public String getBrand() {
              return brand;
          }
      
          public void setBrand(String brand) {
              this.brand = brand;
          }
      
          public Double getPrice() {
              return price;
          }
      
          public void setPrice(Double price) {
              this.price = price;
          }
      
          public Phone() {
          }
      
          @Override
          public String toString() {
              return "Phone{" +
                      "brand='" + brand + '\'' +
                      ", price=" + price +
                      '}';
          }
      }
//使用构造方法创建bean实例(xml代码块)
<bean id="phone3" class="com.soft1851.spring.ioc.entity.Phone">
    <constructor-arg value="诺基亚"></constructor-arg>
    <constructor-arg value="1000"></constructor-arg>
</bean>
//使用set方法构造bean实例(xml代码块)
<bean id="phone1" class="com.soft1851.spring.ioc.entity.Phone" p:brand="iPhone11" p:price="6666"/>
<bean id="phone2" class="com.soft1851.spring.ioc.entity.Phone" p:brand="iPhoneXS" p:price="8888"/>
  • Student

    //实体类
    public class Student {
        private Integer id;
        private String name;
        private List<String> hobbies;
        private List<Phone> phone;
    
        public Student(Integer id, String name, List<String> hobbies, List<Phone> phone) {
            this.id = id;
            this.name = name;
            this.hobbies = hobbies;
            this.phone = phone;
        }
    
        public Student() {
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", hobbies=" + hobbies +
                    ", phone=" + phone +
                    '}';
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<String> getHobbies() {
            return hobbies;
        }
    
        public void setHobbies(List<String> hobbies) {
            this.hobbies = hobbies;
        }
    
        public List<Phone> getPhone() {
            return phone;
        }
    
        public void setPhone(List<Phone> phone) {
            this.phone = phone;
        }
    }
//通过构造方法传入参数(list操作)
<bean id="student1" class="com.soft1851.spring.ioc.entity.Student">
<property name="id" value="1001"/>
<property name="name" value="Tom"/>
<property name="hobbies">
    <list>
        <value>打游戏</value>
        <value>看电视</value>
        <value>玩手机</value>
    </list>
</property>
    <property name="phone">
        <list>
            <ref bean="phone1" />
            <ref bean="phone2" />
        </list>
    </property>
</bean>
  • Grade

    //实体类
    public class Grade {
        private String name;
        private Map<String,Student> students;
    
        public Grade(String name, Map<String, Student> students) {
            this.name = name;
            this.students = students;
        }
    
        public Grade() {
        }
    
        @Override
        public String toString() {
            return "Grade{" +
                    "name='" + name + '\'' +
                    ", students=" + students +
                    '}';
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Map<String, Student> getStudents() {
            return students;
        }
    
        public void setStudents(Map<String, Student> students) {
            this.students = students;
        }
    }
//通过构造方法传入参数(map操作)
<bean id="grade" class="com.soft1851.spring.ioc.entity.Grade">
    <property name="name" value="soft1851"></property>
    <property name="students">
        <map>
            <entry key="student1" value-ref="student1"></entry>
            <entry key="student2" value-ref="student2"></entry>
        </map>
    </property>
</bean>
  1. 注解配置版

    • Student

      //基于注解实现的bean对象
      @Configuration
      public class StudentConfig {
          @Bean
          public Student student(){
              Student student = new Student();
              student.setId(1);
              student.setName("小王");
              List hobbyList = new ArrayList(){{
                  this.add("吃饭");
                  this.add("睡觉");
                  this.add("打豆豆");
              }};
              student.setHobbies(hobbyList);
              List phoneList = new ArrayList(){{
                  this.add("诺基亚");
                  this.add("三星");
              }};
              student.setPhone(phoneList);
              return student;
          }
      }
//测试方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {StudentConfig.class})
public class StudentConfigTest {
    @Autowired
    private Student student;
    @Test
    public void student() {
        System.out.println(student);
    }
}
  • Grade

    //实体类
    @Configuration
    public class GradeConfig {
        @Bean
        public Grade grade(){
            Grade grade = new Grade();
            grade.setName("软件1851");
            List phoneList = new ArrayList(){{
                this.add("phone");
            }};
            List hobbyList = new ArrayList(){{
                this.add("吃饭");
                this.add("学习");
            }};
            Map studentMap = new HashMap();
            for(int i =0;i<3;i++){
                Student student = new Student();
                student.setName("小同学"+i);
                student.setId(12+i);
                student.setPhone(phoneList);
                student.setHobbies(hobbyList);
                studentMap.put("student"+i,student);
            }
            grade.setStudents(studentMap);
            return grade;
        }
    }
//测试方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {GradeConfig.class})
public class GradeConfigTest {
    @Autowired
    private Grade grade;
    @Test
    public void grade() {
        System.out.println(grade);
    }
}
  • phone

    //使用扫包的方法获取bean对象
    @Configuration
    public class PhoneConfig {
        @Bean
        public Phone phone(){
            Phone phone = new Phone();
            phone.setBrand("苹果");
            phone.setPrice(8000.0);
            return phone;
        }
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PhoneConfig.class);
            ac.scan("com.soft1851.spring.ioc.config");
            Phone phone = (Phone)ac.getBean("phone");
            System.out.println(phone);
    
        }
    }

orm模块

  1. Spring JDBC的xml配置版(Forum和post)
  2. Spring JDBC的注解配置版
  3. Mybatis的xml配置版
  4. Mybatis的注解配置版
  5. Spring Data JPA的xml配置版
  6. Spring Data JPA的注解配置版

web模块

  1. Spring MVC的xml配置版(返回视图和返回JSON、过滤器、拦截器、跨域、静态资源等配置)
  2. Spring MVC的注解配置版

aop模块

  1. 前置、后置、环绕增强的xml配置版
  2. 前置、后置、环绕增强的注解版
  3. 使用场景:日志、事务、统一异常处理、拦截等
Last modification:January 21st, 2021 at 09:06 pm