100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java 注解加载配置文件_Spring的Java配置方式和读取properties配置文件

java 注解加载配置文件_Spring的Java配置方式和读取properties配置文件

时间:2021-02-18 19:11:53

相关推荐

java 注解加载配置文件_Spring的Java配置方式和读取properties配置文件

java配置是spring4.x推荐的配置方式,可以完全替代xml配置。

1、注解 @Configuration 和 @Bean

spring的java配置方式是通过@Configuration和@Bean这两个注解来实现的。

@Configuration作用在类上,相当于一个xml配置文件;@Bean作用在方法上,相当于xml配置中的

UserServiceImpl

packagecom.oy.service.impl;importcom.oy.service.UserService;public class UserServiceImpl implementsUserService {

@Overridepublic voidgetUser(Integer id) {

System.out.println("id = " +id);

}

}

JavaConfig

packagecom.oy;importorg.springframework.context.annotation.Bean;importorg.springframework.ponentScan;importorg.springframework.context.annotation.Configuration;importcom.oy.service.UserService;importcom.oy.service.impl.UserServiceImpl;

@Configuration//通过该注解来表明该类是一个spring的配置类,相当与一个xml文件

//@ComponentScan(basePackages = {"com.oy.service.impl"}) //配置扫描包

public classJavaConfig {

@BeanpublicUserService userService() {return newUserServiceImpl();

}

}

TestDemo

public classTestDemo {public static voidmain(String[] args) {

AnnotationConfigApplicationContext context=

new AnnotationConfigApplicationContext(JavaConfig.class);

UserService userService= context.getBean(UserService.class);

userService.getUser(111);

context.close();

}

}

2、读取外部的配置文件

依赖

com.jolbox

bonecp-spring

0.8.0.RELEASE

数据库连接信息db.properties

db.url=jdbc:mysql://127.0.0.1:3306/security_test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8

db.driverClassName=com.mysql.jdbc.Driver

db.username=root

db.password=

DBConfig

packagecom.oy;importjavax.sql.DataSource;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.PropertySource;importcom.jolbox.bonecp.BoneCPDataSource;

@Configuration

@PropertySource(value= {"classpath:db.properties", "xxx"},ignoreResourceNotFound=true)public classDBConfig {

@Value("${db.url}")privateString url;

@Value("${db.driverClassName}")privateString driverClassName;

@Value("${db.username}")privateString username;

@Value("${db.password}")privateString password;

@Bean(destroyMethod="close")publicDataSource dataSource () {

System.out.println("======url=" +url);

BoneCPDataSource dataSource= newBoneCPDataSource();

dataSource.setDriverClass(driverClassName);

dataSource.setJdbcUrl(url);

dataSource.setUsername(username);

dataSource.setPassword(password);//检查数据库连接池中空闲连接的间隔时间,单位分,默认240,如果要取消设置为0

dataSource.setIdleConnectionTestPeriodInMinutes(60);//连接池中未使用的连接最大存活时间,单位分,默认60,如果要永远存活设置为0

dataSource.setIdleMaxAgeInMinutes(30);//每个分区最大连接数

dataSource.setMaxConnectionsPerPartition(100);//每个分区最小连接数

dataSource.setMinConnectionsPerPartition(5);returndataSource;

}

}

测试数据源是否配置成功

public classTestDemo {public static voidmain(String[] args) {

AnnotationConfigApplicationContext context=

new AnnotationConfigApplicationContext(DBConfig.class);

DataSource dataSource= context.getBean(DataSource.class);

System.out.println(dataSource);

context.close();

}

}

==============以上使用的是javaConfig配置==============

==============xml配置参考==============

---

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。