木心

毕竟几人真得鹿,不知终日梦为鱼

导航

Spring的Java配置方式和读取properties配置文件

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

1、注解 @Configuration 和 @Bean

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

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

   UserServiceImpl

package com.oy.service.impl;

import com.oy.service.UserService;

public class UserServiceImpl implements UserService {

    @Override
    public void getUser(Integer id) {
        System.out.println("id = " + id);
    }
    
}

  JavaConfig

package com.oy;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.oy.service.UserService;
import com.oy.service.impl.UserServiceImpl;

@Configuration // 通过该注解来表明该类是一个spring的配置类,相当与一个xml文件
//@ComponentScan(basePackages = {"com.oy.service.impl"}) // 配置扫描包
public class JavaConfig {

    @Bean
    public UserService userService() {
        return  new UserServiceImpl();
    }
}

  TestDemo

public class TestDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = 
                new AnnotationConfigApplicationContext(JavaConfig.class);
        
        UserService userService = context.getBean(UserService.class);
        userService.getUser(111);
        
        context.close();
    }
}

 

2、读取外部的配置文件

  依赖

<!-- 连接池 -->
<dependency>
    <groupId>com.jolbox</groupId>
    <artifactId>bonecp-spring</artifactId>
    <version>0.8.0.RELEASE</version>
</dependency>

  数据库连接信息 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

package com.oy;

import javax.sql.DataSource;

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 com.jolbox.bonecp.BoneCPDataSource;

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

    @Value("${db.url}")
    private String url;
    @Value("${db.driverClassName}")
    private String driverClassName;
    @Value("${db.username}")
    private String username;
    @Value("${db.password}")
    private String password;
    
    @Bean(destroyMethod="close")
    public DataSource dataSource () {
        System.out.println("======url=" + url);
        BoneCPDataSource dataSource = new BoneCPDataSource();
        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);
        return dataSource;
    }
}

  测试数据源是否配置成功

public class TestDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = 
                new AnnotationConfigApplicationContext(DBConfig.class);
        
        DataSource dataSource = context.getBean(DataSource.class);
        System.out.println(dataSource);
        
        context.close();
    }
}

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

 

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

 

---

posted on 2020-11-19 13:38  wenbin_ouyang  阅读(958)  评论(0编辑  收藏  举报