栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring5.0 零配置开发Ioc

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring5.0 零配置开发Ioc

POM.XML




  4.0.0

  org.xxx
  Project
  1.0-SNAPSHOT

  Project
  
  http://www.example.com

  
    UTF-8
    1.8
    1.8
  

  
    
      junit
      junit
      4.11
      test
    
    
    //配置spring-context

    
      org.springframework
      spring-context
      5.1.5.RELEASE
    


    
      org.apache.maven
      maven-settings
      2.0.9
    

  

  
    
      

        //配置maven编辑器和文件编码
        
        
          org.apache.maven.plugins
          maven-compiler-plugin
          2.3.2
          
            1.8
            1.8
            utf-8
          
        
      
    
  

新建UserDao 类:

package org.xxx.Dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDao {

    public void test(){
        System.out.println("UserDao Test ...");
    }
}

新建UserService类

package org.xxx.Service;

import org.springframework.stereotype.Service;
import org.xxx.Dao.UserDao;

import javax.annotation.Resource;

@Service
public class UserService {

    @Resource
    private UserDao userDao;

    public void test(){
        System.out.println("UserDao输出测试...");
        this.userDao.test();
        System.out.println("UserService输出测试...");
        System.out.println("UserService Test...");
    }
}

新建配置类IocConfig:

package org.xxx.Config;

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

@Configuration
@ComponentScan("org.xxx")
public class IocConfig {
}

新建测试类,测试:

package org.xxx;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.xxx.Config.IocConfig;
import org.xxx.Service.UserService;

public class Starter {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
                IocConfig.class
        );
        UserService userService = applicationContext.getBean(UserService.class);
        userService.test();
    }
}

测试输出结果: 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号