當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot笔记(二)
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot笔记(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
@ConfigurationProperties注解
作用:將application.properties文件中定義的屬性映射到所被注解的類上。
注意:被該注解注解的類,還應(yīng)該添加**@Component**注解,就是讓該類稱為spring的的一個(gè)組件才行。
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; (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;}public String toString() {return "Person{" +"lastName='" + lastName + '\'' +", age=" + age +", birth=" + birth +", boss=" + boss +", maps=" + maps +", lists=" + lists +", dog=" + dog +'}';} } 復(fù)制代碼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;} } 復(fù)制代碼person.lastName=獨(dú)生 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 復(fù)制代碼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;(SpringRunner.class) public class Demo1ApplicationTests {private Person person;public void contextLoads() {System.out.printf("Person"+person);}} 復(fù)制代碼@PropertySource注解
作用:指定一個(gè)properties文件中的屬性來映射到所被注解的類上。
注意:還要使用@Component,@ConfigurationProperties這兩個(gè)注解。
將上面的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的一個(gè)組件 //讀取配置文件中的頭,來分辨要映射的屬性 (prefix = "person") //指定配置文件 (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;}public String toString() {return "Person{" +"lastName='" + lastName + '\'' +", age=" + age +", birth=" + birth +", boss=" + boss +", maps=" + maps +", lists=" + lists +", dog=" + dog +'}';} } 復(fù)制代碼@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; //locations = {"classpath:beans.xml"}:spring的配置文件路徑 (locations = {"classpath:beans.xml"}) public class Demo1Application {public static void main(String[] args) {SpringApplication.run(Demo1Application.class, args);} } 復(fù)制代碼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;(SpringRunner.class) public class Demo1ApplicationTests {private ApplicationContext ioc;public void test(){System.out.println("DATA:"+ioc.containsBean("helloService"));}}復(fù)制代碼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,使用該注解說明該類是一個(gè)配置類*/ public class MyConfig {/*@Bean,定義一個(gè)Bean*/public HelloService helloService(){return new HelloService();} } 復(fù)制代碼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;(SpringRunner.class) public class Demo1ApplicationTests {private ApplicationContext applicationContext;public void contextLoads() {/*通過applicationContext的containsBean方法,查看bean是否在容器中*/boolean b = applicationContext.containsBean("helloService");System.out.println(b);} } 復(fù)制代碼總結(jié)
以上是生活随笔為你收集整理的SpringBoot笔记(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elastic-job 的简单使用
- 下一篇: ngrx心得体会总结