SpringBoot笔记(二)

212 阅读2分钟

@ConfigurationProperties注解

作用:将application.properties文件中定义的属性映射到所被注解的类上。

注意:被该注解注解的类,还应该添加**@Component**注解,就是让该类称为spring的的一个组件才行。

package com.zgy.springboot.demo1.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private int age;
    private Date birth;
    private boolean boss;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public boolean isBoss() {
        return boss;
    }

    public void setBoss(boolean boss) {
        this.boss = boss;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                ", boss=" + boss +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
package com.zgy.springboot.demo1.bean;

public class Dog {
    private String name;
    private int age;

    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;
    }
}
person.lastName=独生
person.age=22
person.birth=1996/11/18
person.boss=true
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=yy
person.dog.age=1
package com.zgy.springboot.demo1;

import com.zgy.springboot.demo1.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo1ApplicationTests {

	@Autowired
	private Person person;
	@Test
	public void contextLoads() {

		System.out.printf("Person"+person);
	}

}

@PropertySource注解

作用:指定一个properties文件中的属性来映射到所被注解的类上。

注意:还要使用@Component,@ConfigurationProperties这两个注解。

将上面的Person代码修改如下

package com.zgy.springboot.demo1.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

// 将其作为spring的一个组件
@Component
//读取配置文件中的头,来分辨要映射的属性
@ConfigurationProperties(prefix = "person")
//指定配置文件
@PropertySource(value = {"classpath:person.properties"})
public class Person {

    private String lastName;
    private int age;
    private Date birth;
    private boolean boss;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public boolean isBoss() {
        return boss;
    }

    public void setBoss(boolean boss) {
        this.boss = boss;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                ", boss=" + boss +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

@ImportResource注解

作用:将spring的配置文件,比如bean.xml加载到springboot中

package com.zgy.springboot.demo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
//locations = {"classpath:beans.xml"}:spring的配置文件路径
@ImportResource(locations = {"classpath:beans.xml"})
public class Demo1Application {

	public static void main(String[] args) {
		SpringApplication.run(Demo1Application.class, args);
	}
}
package com.zgy.springboot.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo1ApplicationTests {

	@Autowired
	private ApplicationContext ioc;

	@Test
    public void test(){
        System.out.println("DATA:"+ioc.containsBean("helloService"));
    }

}

SpringBoot推荐的配置方法

就是是用@Configuration注解

package com.zgy.springboot.demo1.config;

import com.zgy.springboot.demo1.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
 @Configuration,使用该注解说明该类是一个配置类
 */
@Configuration
public class MyConfig {

    /*
    @Bean,定义一个Bean
     */
    @Bean
    public HelloService helloService(){
        return new HelloService();
    }
}
package com.zgy.springboot.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo1ApplicationTests {

    @Autowired
    private ApplicationContext applicationContext;

	@Test
	public void contextLoads() {
        /*
        通过applicationContext的containsBean方法,查看bean是否在容器中
         */
        boolean b = applicationContext.containsBean("helloService");
        System.out.println(b);
    }
}