日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot笔记(二)

發(fā)布時(shí)間:2024/4/17 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;@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;}@Overridepublic 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;@RunWith(SpringRunner.class) @SpringBootTest public class Demo1ApplicationTests {@Autowiredprivate Person person;@Testpublic 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è)組件 @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;}@Overridepublic 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;@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);} } 復(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;@RunWith(SpringRunner.class) @SpringBootTest public class Demo1ApplicationTests {@Autowiredprivate ApplicationContext ioc;@Testpublic 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è)配置類*/ @Configuration public class MyConfig {/*@Bean,定義一個(gè)Bean*/@Beanpublic 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;@RunWith(SpringRunner.class) @SpringBootTest public class Demo1ApplicationTests {@Autowiredprivate ApplicationContext applicationContext;@Testpublic void contextLoads() {/*通過applicationContext的containsBean方法,查看bean是否在容器中*/boolean b = applicationContext.containsBean("helloService");System.out.println(b);} } 復(fù)制代碼

總結(jié)

以上是生活随笔為你收集整理的SpringBoot笔记(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。