2-02 Spring的Java配置方式

阅读更多

 

1) 在Eclipse中新建一个Maven项目。

2) 复制粘贴Maven的配置文件的内容到pom.xml


	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	
	cn.itcast.springboot
	itcast-springboot
	1.0.0-SNAPSHOT
	war

	
		
			org.springframework
			spring-webmvc
			4.3.7.RELEASE
		
		
		
			com.jolbox
			bonecp-spring
			0.8.0.RELEASE
		
		
			org.springframework.boot
			spring-boot-starter-web
		
	
	
		${project.artifactId}
		
			
			
				org.apache.maven.plugins
				maven-resources-plugin
				
					UTF-8
				
			
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.7
					1.7
					UTF-8
				
			
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
		
			
				
				
					org.apache.tomcat.maven
					tomcat7-maven-plugin
					2.2
				
			
		
	

 3) 在Eclipse中新建包层次cn.itcast.springboot.javaconfig

 4) 创建属于POJO的User类

package cn.itcast.springboot.javaconfig;

public class User {

    private String username;

    private String password;

    private Integer age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

  5) 创建模拟数据库查询的UserDAO类

package cn.itcast.springboot.javaconfig;

import java.util.ArrayList;
import java.util.List;

public class UserDAO {

    public List queryUserList() {
        List result = new ArrayList();
        // 模拟数据库的查询
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUsername("username_" + i);
            user.setPassword("password_" + i);
            user.setAge(i + 1);
            result.add(user);
        }
        return result;
    }

}

 5) 创建UserService类,用到了@Service 和 @Autowired 注解

package cn.itcast.springboot.javaconfig;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    // 注入Spring容器中的bean对象
    private UserDAO userDAO;

    public List queryUserList() {
        // 调用userDAO中的方法进行查询
        return this.userDAO.queryUserList();
    }

}

 6) 创建SpringConfig类,主要用到了@Configuration 和 @Bean 注解

package cn.itcast.springboot.javaconfig;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.jolbox.bonecp.BoneCPDataSource;

@Configuration
// 通过该注解来表明该类是一个Spring的配置,相当于一个xml文件
@ComponentScan(basePackages = "cn.itcast.springboot.javaconfig")
// 配置扫描包
@PropertySource(value = { "classpath:jdbc.properties" }, ignoreResourceNotFound = true)
public class SpringConfig {

    @Bean
    // 通过该注解来表明是一个Bean对象,相当于xml中的
    public UserDAO getUserDAO() {
        return new UserDAO(); // 直接new对象做演示
    }

    @Value("${jdbc.url}")
    private String jdbcUrl;

    @Value("${jdbc.driverClassName}")
    private String jdbcDriverClassName;

    @Value("${jdbc.username}")
    private String jdbcUsername;

    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
        // 数据库驱动
        boneCPDataSource.setDriverClass(jdbcDriverClassName);
        // 相应驱动的jdbcUrl
        boneCPDataSource.setJdbcUrl(jdbcUrl);
        // 数据库的用户名
        boneCPDataSource.setUsername(jdbcUsername);
        // 数据库的密码
        boneCPDataSource.setPassword(jdbcUsername);
        // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
        boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
        // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0
        boneCPDataSource.setIdleMaxAgeInMinutes(30);
        // 每个分区最大的连接数
        boneCPDataSource.setMaxConnectionsPerPartition(100);
        // 每个分区最小的连接数
        boneCPDataSource.setMinConnectionsPerPartition(5);
        return boneCPDataSource;
    }

}

 7) 创建Main.java

package cn.itcast.springboot.javaconfig;

import java.util.List;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    
    public static void main(String[] args) {
        // 通过Java配置来实例化Spring容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        
        // 在Spring容器中获取Bean对象
        UserService userService = context.getBean(UserService.class);
        
        // 调用对象中的方法
        List list = userService.queryUserList();
        for (User user : list) {
            System.out.println(user.getUsername() + ", " + user.getPassword() + ", " + user.getAge());
        }
        
        // 销毁该容器
        context.destroy();
    }

}

 8) 在Eclipse中,在Main.java上,右键,Run As Java Application

username_0, password_0, 1
username_1, password_1, 2
username_2, password_2, 3
username_3, password_3, 4
username_4, password_4, 5
username_5, password_5, 6
username_6, password_6, 7
username_7, password_7, 8
username_8, password_8, 9
username_9, password_9, 10

 

 

  • itcast-springboot.zip (63.1 KB)
  • 下载次数: 0

你可能感兴趣的:(springboot,javaconfig)