SpringBoot中的配置类(@Configuration)

网友投稿 497 2022-07-24


目录SpringBoot基于java类的配置第一步第二步第三步第四步测试SpringBoot自定义配置类1.方式一2.方式二

SpringBoot基于java类的配置

java配置主要靠java类和一些注解来达到和xml配置一样的效果,比较常用的注解有:

@Configuration:声明一个类作为配置类,代替xml文件@Bean:声明在方法上,将方法的返回值加入Bean容器,代替标签@Value:属性注入@PropertySource:指定外部属性文件(properties)。@ConfigurationProperties(prefix = “xx”) (指定配置属性的前缀)

提示: @Configuration也附带了@Component的功能。所以理论上内部也可以使用@Autowared功能的

我们接下来用Java类配置来尝试实现连接池配置

第一步

在pom.xml导入

com.alibaba

druid

1.1.12

第二步

添加druid.properties文件到resources包里

driverClassName=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/order?characterEncoding=utf-8

name=root

pass=root

initialSize=1

minIdle=1

maxActive=20

maxWait=3

第三步

创建配置文件 JdbcConfig

package cn.boke.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Configuration //声明配置类

@PropertySource("classpath:druid.properties") //http://导入配置文件

public class JdbcConfig {

//将配置文件内的 信息 放入变量中

@Value("${driverClassName}")

private String driverClassName; //JDBC驱动

@Value("${url}")

private String url; //连接地址

@Value("${name}")

private String name; //数据库用户

@Value("${pass}")

private String pass; //数据库密码

@Value("${initialSize}")

private int initialSize; //初始化连接数量

@Value("${minIdle}")

private int minIdle; //最小连接数量

@Value("${maxActive}")

private int maxActive; //最大连接数量

@Value("${maxWait}")

private int maxWait; //最大等待时间

@Bean("dataSource") //将配置信息 存入Spring 容器中 名称为 dataSource

public DataSource dataSource() {gTDuhLDg

DruidDataSource dataSource = new DruidDataSource();

dataSource.setUrl(url);

dataSource.setDriverClassName(driverClassName);

gTDuhLDg dataSource.setUsername( name);

dataSource.setPassword(pass);

dataSource.setInitialSize(initialSize);

dataSource.setMaxActive(maxActive);

dataSource.setMaxWait(maxWait);

return dataSource;

}

}

第四步测试

连接池内的数据是否 添加成功

打个断点

运行TestAplliaction 这个类的启动引导

然后在游览器中输入 http://localhost:8080/hello

回到IDEA控制台中点击

能在显示的内容中找到 对应的信息 就ok了

A控制台中点击

能在显示的内容中找到 对应的信息 就ok了

SpringBoot自定义配置类

1.方式一

配置类标注:

@ConfigurationProperties注解,可以设置前缀名@Component 将该类放入spring容器中

必须设定get/set方法

配置类:

@Component //将该Bean放入spring容器中

@ConfigurationProperties(prefix = "com.example")//指定该类为配置获取类,并指定前缀

public class MyConfig {

private String name;

private Integer age;

//必须设置get set方法

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;

}

}

application.properties中配置

com.example.age=20

com.example.name=xxx

使用:

在其他Bean中可以直接注入该Bean

@RestController

public class DemoController {

@Autowired

private MyConfig config;

@GetMapping("/demo2")

public String m2(){

return config.getName();

}

}

2.方式二

配置类:

不使用 @Component 将该类放入spring容器中,而是使用@EnableConfigurationProperties(MyConfig.class)在根配置类中引入

@ConfigurationProperties(prefix = "com.example")//指定该类为配置获取类,并指定前缀

public class MyConfig {

private String name;

private Integer age;

//必须设置get set方法

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;

}

}

@SpringBootApplication

@EnableConfigurationProperties(MyConfig.class) //引入自定义配置类

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class,args);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java定义画板类的方法
下一篇:Spring利用注解整合Mybatis的方法详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~