• Spring学习总结[1]-入门


    快速入门

    目录结构

    这里写图片描述

    程序流程

    User是实体类;将User注入到UserInit中,初始化实体类;将UserInit注入到UserService中,调用UserInit的方法获得初始化后的User。需要注入的属性必须要有getter,setter方法

    User实体类

    package me.spring.model;
    
    public class User {
        int     id;
        String  name;
        int     age;
        String  sex;
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
    }
    

    UserInit

    package me.spring.init;
    
    import me.spring.model.User;
    
    public class UserInit {
        User user;
        public User initUser(){
            user.setAge(18);
            user.setId(1);
            user.setName("阿德勒");
            user.setSex("男");
            return user;
        }
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }
    
    }

    UserService

    package me.spring.service;
    
    import com.alibaba.fastjson.JSON;
    
    import me.spring.init.UserInit;
    
    public class UserService {
        UserInit userInit;
        public void printUser(){
            System.out.println(JSON.toJSONString(userInit.initUser()));
        }
        public UserInit getUserInit() {
            return userInit;
        }
        public void setUserInit(UserInit userInit) {
            this.userInit = userInit;
        }
    }
    

    bean.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="user" class="me.spring.model.User" />
    
        <bean id="userInit" class="me.spring.init.UserInit" >
            <!-- 注入User -->
            <property name="user" ref="user" />
        </bean>
    
        <bean id="userService" class="me.spring.service.UserService" >
            <!-- 注入UserInit -->
            <property name="userInit" >
                <ref bean="userInit"/>
            </property>
        </bean>
    </beans>

    spring.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">
         <!-- 引入beansx.xml,当有多个xml时这么写比较好 -->
        <import resource="beans.xml"/>
    </beans>

    UserServiceTest

    package me.spring.service;
    
    import static org.junit.Assert.*;
    
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.util.ConcurrencyThrottleSupport;
    
    import me.spring.MyConfig;
    import me.spring.service.UserService;
    
    public class UserServiceTest {
        static ApplicationContext context=null;
        @BeforeClass
        public static void B(){
            context=new ClassPathXmlApplicationContext("resources/spring.xml");
            //还可以用java类来管理bean之间的关系,MyConfig代码在下面
            //context=new AnnotationConfigApplicationContext(MyConfig.class);
        }
    
        @Test
        public void test() {
            UserService s=(UserService) context.getBean("userService");
            s.printUser();
        }
    
    }
    

    MyConfig

    package me.spring;
    
    import me.spring.init.UserInit;
    import me.spring.model.User;
    import me.spring.service.UserService;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MyConfig {
    
        @Bean(name="user")
        public User user(){
            return new User();
        }
    
        @Bean(name="userInit")
        public UserInit userInit(){
            UserInit userInit=new UserInit();
            userInit.setUser(user());
            return userInit;
        }
    
        @Bean(name="userService")
        public UserService userService(){
            UserService userService=new UserService();
            userService.setUserInit(userInit());
            return userService;
        }
    
    }
    

    结果

    这里写图片描述

  • 相关阅读:
    线程池的扩展 beforeExecute() afterExecute() terminaerd()
    信号量semaphore 读写锁ReadWriteLock 倒计时器CountDownLatch 循环栅栏 CyclicBarrier 线程阻塞工具类LockSupport
    ReenTrantLock可重入锁 和synchronized 内部所锁的
    integer.valueof和integer.parseint
    守护线程
    notify wait sleep join yield yield
    Thread.stop()不要随意调用
    iterate使用了parallel() 反而消耗了更多的时间
    Stream 分支/合并 框架的实际例子
    区分Collection、Collector和collect Collectors类的静态工厂方法
  • 原文地址:https://www.cnblogs.com/A-yes/p/9894179.html
Copyright © 2020-2023  润新知