spring小例子(java配置)

spring发展史

  • spring1.X 使用的xml配置
  • Spring2.X 使用的注解
  • Spring3.X、Spring4.X 使用java配置

spring的java配置方式

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

1.configuration作用在类上,相当于一个xml配置问及那

2.Bean作用于方法上,相当于xml配置中的<bean>

实例

使用java配置方式,并实现了spring IOC功能

步骤

1. pom中导入maven依赖

 

<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>cn.itcast.springboot</groupId>
    <artifactId>itcast-springboot</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.20.RELEASE</version>
        </dependency>
        
    </dependencies>
    
</project>
View Code

 

2. 创建bean对象

package cn.itcast.spring;

public class User {

    private String userName;
    private String passwd;
    private Integer age;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
    
}
View Code

3.编辑DAO层用于模拟与数据库层交互

package cn.itcast.spring;

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

public class UserDAO {
    
    
    public List<User> queryUser()
    {
        List<User> userList = new ArrayList<User>();
        for (int  i = 0; i < 3; i++)
        {
            User user = new User();
            
            user.setAge(i + 2);
            user.setPasswd("passwd" + i);
            user.setUserName("username" + i);
            userList.add(user);
        }
        return userList;
    }

}
View Code

4.编写Service层用于实现与bean对象的业务逻辑

 

package cn.itcast.spring;

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<User> queryUser()
    {
        // 调用userDAO中的方法
        return userDAO.queryUser();
    }
}
View Code

 

5. 编写配置类,用于实例化spring容器

package cn.itcast.spring;

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

@Configuration //表示该类为Spring的配置文件(相当于xml文件)
@ComponentScan(basePackages = "cn.itcast.spring") // 扫描配置包
public class SpringConfig 
{
    @Bean // 表明是一个bean对象
    public UserDAO getUserDAO()
    {
        return new UserDAO();
    }
}
SpringConfig

6. 测试类,启动spring容器

package cn.itcast.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class SpringApplication 
{
    public static void main(String[] args) {
        // 通过java配置来实例化Spring容器
        AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext(SpringConfig.class);
        //在spring容器中获取bean对象
        UserService userService = context.getBean(UserService.class);
        
        for (User user : userService.queryUser())
        {
            System.out.println(user.getUserName());
        }
        
        //销毁容器
        context.destroy();
    }
}
SpringApplication

 

posted @ 2019-09-03 23:31  红萝卜头  阅读(295)  评论(0编辑  收藏  举报