SpringBoot01 java的配置方式

1、添加依赖 pom.xml

<?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>springboot</groupId>
    <artifactId>springboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、创建一个实体类

package com.lgz.springboot.config;
public class User {
    private String username;
    private String password;
    private Integer age;
    //此处省略get set方法
}

3、模拟Dao

package com.lgz.springboot.config;
import java.util.ArrayList;
import java.util.List;
/***
 * 模拟数据库操作
 */
public class UserDao {
    public List<User> getUserList() {
        List<User> result = new ArrayList<User>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setAge(i);
            user.setPassword("password" + i);
            user.setUsername("name" + i);
            result.add(user);
        }
        return result;
    }
}

4、模拟service

package com.lgz.springboot.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    public List<User> getUserList() {
        return userDao.getUserList();
    }
}

5、创建java配置文件

package com.lgz.springboot.config;

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

@Configuration //表名该类是spring的配置,相当于xml文件
@ComponentScan(basePackages = "com.lgz.springboot.config") //配置扫描包
public class SpringConfig {

    @Bean //说明这是一个Bean对象,相当于xml中的<bean>
    public UserDao getUserDao() {
        return new UserDao();
    }
}

6、测试

package com.lgz.springboot.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        //通过java配置来实例化Spring容器
        AnnotationConfigApplicationContext context = 
         new AnnotationConfigApplicationContext(SpringConfig.class);

        //获取对象
        UserService userService = context.getBean(UserService.class);

        List<User> list = userService.getUserList();
        for (User user : list) {
            System.out.println("name=" + user.getUsername() + " psw=" + user.getPassword() + " age=" + user.getAge());
        }

        context.destroy();
    }
}

测试结果打印

name=name0 psw=password0 age=0
name=name1 psw=password1 age=1
name=name2 psw=password2 age=2
name=name3 psw=password3 age=3
name=name4 psw=password4 age=4
name=name5 psw=password5 age=5
name=name6 psw=password6 age=6
name=name7 psw=password7 age=7
name=name8 psw=password8 age=8
name=name9 psw=password9 age=9
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值