栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

day1 springBoot学习笔记--动力节点

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

day1 springBoot学习笔记--动力节点

其中@Configuration相当于xml配置文件。

来写个例子试验一下呗,先创建一个空的项目,选maven但不选模板,然后看看pom.xml啥亚子噻。



    4.0.0

    org.example
    001-springboot-pre
    1.0-SNAPSHOT

    
        
            org.springframework
            spring-context
            5.3.1
        

        
            junit
            junit
            4.12
        

    
    
        
            
            
                maven-compiler-plugin
                
                3.8.0
                
                
                    1.8
                    1.8
                    
                    UTF-8
                
            
        
    




再在创建的模块下面创建一个类com.rll.vo.Student

package com.rll.vo;

public class Student {
    private String name;
    private  Integer age;
    private String sex;

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", sex='" + sex + ''' +
                '}';
    }
}

 创建beans.xml

 创建一个测试类:

 

package com.rll;

import com.rll.vo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test01(){
        String config="beans.xml";
        //创建容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ctx.getBean("myStudent");
        System.out.println("容器中的对象:"+student);


    }
}

运行结果:

 

二、现在用javaconfig这种方式用一个类来代替这个beans.xml

原本文件目录结构:

 

 用这种方式,首先你得先有自定义的类来代替这个beans.xml配置文件的作用,类上加@Configuration注解,之后还要使用@Bean。

1、创建类config.SpringConfig

 

package com.rll.config;

import com.rll.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class SpringConfig {

    

    @Bean
    public Student createStudent(){
        Student s1= new Student();
        s1.setAge(90);
        s1.setName("zyy");
        s1.setSex("man");
        return s1;
    }
}

 测试一哈呗:

package com.rll;

import com.rll.config.SpringConfig;
import com.rll.vo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test01(){
        String config="beans.xml";
        //创建容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ctx.getBean("myStudent");
        System.out.println("容器中的对象:"+student);
    }


    
    @Test
    public void test02(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student=(Student)ctx.getBean("createStudent");
        System.out.println("使用JavaConfig来创建的bean对象:"+student);
    }
}

 执行test02的方法结果:

 

@Bean,不指定对象的名称,默认是方法名是 id
* 指定对象在容器中的名称(等价于:指定的id属性)
*@Bean的name属性,指定对象的名称(id)

 

package com.rll.config;

import com.rll.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class SpringConfig {

    

    //不指定对象的名称,默认的方法名是id
    @Bean
    public Student createStudent(){
        Student s1= new Student();
        s1.setAge(90);
        s1.setName("zyy");
        s1.setSex("man");
        return s1;
    }

    
    @Bean(name = "ListStudent")
    public Student makeStudent(){
        Student s1= new Student();
        s1.setAge(90);
        s1.setName("lisi");
        s1.setSex("man");
        return s1;
    }
}

 

package com.rll;

import com.rll.config.SpringConfig;
import com.rll.vo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test01(){
        String config="beans.xml";
        //创建容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        Student student = (Student) ctx.getBean("myStudent");
        System.out.println("容器中的对象:"+student);
    }


    
    @Test
    public void test02(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student=(Student)ctx.getBean("createStudent");
        System.out.println("使用JavaConfig来创建的bean对象:"+student);
    }

    @Test
    public void test03(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student=(Student)ctx.getBean("ListStudent");
        System.out.println("使用JavaConfig来创建的bean对象:"+student);
    }
}

运行test03结果:

 

 现在我们来演示一下:

1.新建一个Cat类;

 

package com.rll.vo;

public class Cat {
    private String catId;
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Cat{" +
                "catId='" + catId + ''' +
                ", name='" + name + ''' +
                ", age=" + age +
                '}';
    }

    public String getCatId() {
        return catId;
    }

    public void setCatId(String catId) {
        this.catId = catId;
    }

    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;
    }
}

2.新建一个applicationContext.xml文件

 




    
        
        
        
    

3.注入配置文件

 

4.写test04方法并运行

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/973753.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号