1 Spring的Java配置方式

1 简介

Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置

Spring的Java配置方式是通过 @Configuration 和 @Bean 这两个注解实现的:
	1:@Configuration 作用于类上,相当于一个xml配置文件;
	2:@Bean 作用于方法上,相当于xml配置中的<bean>;

项目源码:
	https://github.com/3748/SpringBoot.git

2 Spring使用Java配置实例

2.1 工程创建
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.springboot</groupId>
    <artifactId>springboot-01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
    </dependencies>
</project>
2.2 创建实体类
package com.gp6.springboot01.bean;

/**
 * 用户实体类
 *
 * @author gp6
 * @date 2018-11-15
 */
public class User {
    private String username;

    public String getUsername() {
        return username;
    }

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

}
2.3 编写UserMapper用于模拟与数据库的交互
package com.gp6.springboot01.mapper;

import com.gp6.springboot01.bean.User;

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

/**
 * 模拟Mapper,查询出数据
 *
 * @author gp6
 * @date 2018-11-15
 */
public class UserMapper {
    public List<User> queryUserList() {
        List<User> result = new ArrayList<User>();
        // 模拟数据库的查询
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUsername("username_" + i);
            result.add(user);
        }
        return result;
    }
}

2.4 编写UserService 用于实现User数据操作业务逻辑
package com.gp6.springboot01.service;

import com.gp6.springboot01.bean.User;
import com.gp6.springboot01.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    // 注入Spring容器中的bean对象
    @Autowired
    private UserMapper userMapper;

    public List<User> queryUserList() {
        // 调用userMapper的方法进行查询
        return userMapper.queryUserList();
    }
}

2.5 编写SpringConfig 用于实例化Spring容器
package com.gp6.springboot01.config;

import com.gp6.springboot01.mapper.UserMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

/**
* 配置文件
*
* @author gp6
* @date 2018-11-15
*/
// 通过@ComponentScan注解来表明该类是一个Spring的配置,相当于一个xml文件
// 配置扫描包
@ComponentScan(basePackages = "com.gp6.springboot01")
public class SpringConfig {

    // 通过@Bean注解来表明是一个Bean对象,相当于xml中的<bean>
    @Bean
    public UserMapper getUserMapper(){
        // 直接new对象做演示
        return new UserMapper();
    }
}
2.6 测试
package com.gp6.springboot01.test;

import com.gp6.springboot01.bean.User;
import com.gp6.springboot01.config.SpringConfig;
import com.gp6.springboot01.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;

/**
* 测试方法
*
* @author gp6
* @date 2018-11-15
*/
public class Main {
    public static void main(String[] args) {
        // 通过Java配置来实例化Spring容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

        // 在Spring容器中获取Bean对象,SpringConfig中的扫描包必须包括UserService
        UserService userService = context.getBean(UserService.class);

        // 调用对象中的方法
        List<User> list = userService.queryUserList();
        for (User user : list) {
            System.out.println(user.getUsername());
        }

        // 销毁容器
        context.destroy();
    }
}

2.7 项目目录结构

项目目录结构

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值