Spring boot 简单例子

[b]Spring boot 简单例子[/b]


application.properties //默认为application.properties
#Server
server.port=8090
#需要加的URL前缀
server.context-path=/v1

#LOGGING
logging.pattern.level=INFO

Test.id=8090
Test.name=xing



package com.cesmart;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.cesmart.entity.TestBean;
//@SpringBootApplication注解等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan
//@Configuration //
@EnableAutoConfiguration
//@ComponentScan(basePackages = "com.cesmart.config") //扫描那些包得到bean
@ComponentScan(basePackages = "com.cesmart") //扫描那些包得到bean,@ComponentScan({"com.teradata.notification","com.teradata.dal"})
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(Application.class, args);

TestBean testBean = (TestBean) applicationContext.getBean("testBean");
System.out.println("TestBean == " + testBean.toString());
}
}



package com.cesmart.entity;

public class TestBean {
private String id;
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "TestBean [id=" + id + ", name=" + name + "]";
}

public TestBean(String id, String name) {
super();
this.id = id;
this.name = name;
}

}



package com.cesmart.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cesmart.entity.TestBean;

@Configuration // 相当于定义XML文件
@ConfigurationProperties(prefix = "Test") // 所需字段以什么开头的
public class TestConfig {
private String id; // 需要它的set方法才可以进行Properties文件内容的引入
private String name;

@Bean(name = "testBean") // 相当于XML里配置bean
public TestBean testBean() {

TestBean client = new TestBean(id, name);
return client;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}



package com.cesmart.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //返回的是body的数据,不是视图名称,//相当于@ResponseBody + @Controller合在一起的作用。
public class WebTest {
@RequestMapping("/webTest") //http://localhost:8090/webTest访问到它
public String webTest(){
System.out.println("webTest");
return "webTest";
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值