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配置參考==============

 

---


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM