zoukankan      html  css  js  c++  java
  • Spring-02 Java配置实现IOC

    Java配置

    • Spring4推荐使用java配置实现IOC
    • Spring boot也推荐采用java配置实现IOC
    • 在实际项目中,一般采用注解配置业务bean,全局配置使用Java配置。

    Java配置使用的注解

    • @Configuration:声明当前类为配置类,配置类等效于spring配置xml文件。
    • @Bean:声明在方法上,方法返回值为Bean。

    示例代码

    特点:dao和service层不再使用注解

    dao的代码

    package com.etc.dao;
    
    import org.springframework.stereotype.Repository;
    
    public class EntityDao {
    	
    	public String getData(){
    		return "get data from database";
    	}
    }
    

      

    service的代码

    package com.etc.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.etc.dao.EntityDao;
    
    public class EntityService {
    	
    	private EntityDao entityDao;
    	
    	public String getData(){
    		return entityDao.getData();
    	}
    
    }
    

     

    Java配置类的代码

    package com.etc.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.etc.dao.EntityDao;
    import com.etc.service.EntityService;
    
    
    /** 声明当前类是一个配置类 ,在该类中定义bean*/
    @Configuration
    public class JavaConfig {
    	
    	/**获取dao的bean*/
    	@Bean
    	public EntityDao entityDao(){
    		return new EntityDao();
    	}
    	
    	/**获取service的bean*/
    	// @Bean
    	// public EntityService entityService(){
    	// EntityService entityService=new EntityService();
    	// entityService.setEntityDao(entityDao());
    	// return entityService;
    	// }
    	
    	/**获取serveice的bean时,通过方法参数注入bean*/
    	@Bean
    	public EntityService entityService(EntityDao entityDao){
    		EntityService entityService=new EntityService();
    		entityService.setEntityDao(entityDao);
    		return entityService;
    	}
    	
    }
    

      

    测试类代码

    package com.etc.test;
    
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import com.etc.config.DiConfig;
    import com.etc.config.JavaConfig;
    import com.etc.service.EntityService;
    
    public class TestClass {
    
    	/**测试使用注解实现IOC*/
    	public void test1() {
    		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
    				DiConfig.class);
    		EntityService es = context.getBean(EntityService.class);
    		System.out.println(es.getData());
    		context.close();
    	}
    	
    	/**测试使用Java配置实现IOC*/
    	@Test
    	public void test2(){
    		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
    				JavaConfig.class);
    		EntityService es = context.getBean(EntityService.class);
    		System.out.println(es.getData());
    	}
    
    }
    

      

    测试结果

    get data from database
    

      



    本博客文章未经许可,禁止转载和商业用途!

    如有疑问,请联系: 2083967667@qq.com


  • 相关阅读:
    win7下如何配置ODBC数据源
    串口小票打印机调试命令
    如何测试一个网页登陆界面
    Cookie是否是httponly
    XSS攻击 (安全方面)和传统防御技术
    Linux下查看文件和文件夹大小
    linux停止和查看启动服务的命令使用方法
    查看Linux下系统资源占用常用命令(top、free、uptime)
    三种经典iPhone上网络抓包方法详解
    如何用Fiddler对Android应用进行抓包
  • 原文地址:https://www.cnblogs.com/rask/p/8287206.html
Copyright © 2011-2022 走看看